Solved

How to update the sql column with value from another table

Posted on 2007-12-01
9
257 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dataset not reading table data 12 46
SQL Select * from 6 36
Why is the output of this function is like this? 4 29
Caste datetime 2 52
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now