Solved

How to update the sql column with value from another table

Posted on 2007-12-01
9
288 Views
Last Modified: 2012-05-05
I have a table NOTIFYDEALS_CJ_PRODUCT_CATEGORY that contains list of categories defined by column NAME.

Now i wish to update a transaction table NOTIFYDEALS_CJ_ADVERTISER_LIST's column CATEGORY_ID with the value retrieved from NOTIFYDEALS_CJ_PRODUCT_CATEGORY . The column name is KEYWORD_ID


Here is a sample
NOTIFYDEALS_CJ_PRODUCT_CATEGORY
===================================
KEYWORD_ID, NAME
1,'Accessories'
2 ,'Astrology'
3,'Auction'

NOTIFYDEALS_CJ_ADVERTISER_LIST's
====================================
CATEGORY,CATEGORY_ID
'Accessories',NULL
'Astrology',NULL
'Auction',NULL

The join column is CATEGORY of NOTIFYDEALS_CJ_ADVERTISER_LIST's  with NAME column of NOTIFYDEALS_CJ_PRODUCT_CATEGORY




0
Comment
Question by:TECH_NET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 

Author Comment

by:TECH_NET
ID: 20390354
I am using the following script

UPDATE NOTIFYDEALS_CJ_ADVERTISER_LIST
SET CATEGORY_ID=(SELECT KEYWORD_ID FROM NOTIFYDEALS_CJ_PRODUCT_CATEGORY WHERE
NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY=NOTIFYDEALS_CJ_PRODUCT_CATEGORY.CATEGORY
0
 

Author Comment

by:TECH_NET
ID: 20390358
UPDATE NOTIFYDEALS_CJ_ADVERTISER_LIST
SET CATEGORY_ID =(SELECT KEYWORD_ID FROM NOTIFYDEALS_CJ_PRODUCT_CATEGORY WHERE
NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY=NOTIFYDEALS_CJ_PRODUCT_CATEGORY.NAME)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390492
try with:

UPDATE
      NOTIFYDEALS_CJ_ADVERTISER_LIST, NOTIFYDEALS_CJ_PRODUCT_CATEGORY
SET
      NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY_ID =  NOTIFYDEALS_CJ_PRODUCT_CATEGORY.KEYWORD_ID
WHERE
      NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY = NOTIFYDEALS_CJ_PRODUCT_CATEGORY.NAME
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20390504
Hmm, I think this won't work with T-SQL but work with other SQLs.
Better you try with:

UPDATE
      NOTIFYDEALS_CJ_ADVERTISER_LIST
SET
      NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY_ID =  NOTIFYDEALS_CJ_PRODUCT_CATEGORY.KEYWORD_ID
FROM
      NOTIFYDEALS_CJ_ADVERTISER_LIST
INNER JOIN
      NOTIFYDEALS_CJ_PRODUCT_CATEGORY
ON
      NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY = NOTIFYDEALS_CJ_PRODUCT_CATEGORY.NAME

Also would be incredibly helpful if you reduce the names of your tables, even can affect the performance of your database transactions
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20390594
your 2nd SQL looks perfect to me. why do you need to change it?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20391057
my first sql works with mysql and others,
the second sql works with ms sql
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20392326
In case anyone's confused, I was commenting on the question itself. The SQL looks perfect for SQL Server (2005), which is where this question is. What was the question?
0
 

Author Comment

by:TECH_NET
ID: 20393284
I am sure what is the issue with my second sql, I changed it to the following and it worked.

UPDATE NOTIFYDEALS_CJ_ADVERTISER_LIST
SET CATEGORY_ID =(SELECT KEYWORD_ID FROM NOTIFYDEALS_CJ_PRODUCT_CATEGORY  PROD_CAT WHERE
NOTIFYDEALS_CJ_ADVERTISER_LIST.CATEGORY=PROD_CAT .NAME)
0
 
LVL 25

Accepted Solution

by:
imitchie earned 250 total points
ID: 20393295
That's exactly the same as your 2nd comment, except you aliased the inner table name... which shouldn't make any difference. Maybe there's a misspelling in the long table name on the last line (2nd comment)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question