Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to update the sql column with value from another table

Posted on 2007-12-01
9
Medium Priority
?
293 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
  • 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 your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

972 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