Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Single Update Query - Required


Single Update Query - Required

Author
Message
ManiDBLover
ManiDBLover
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 75
Table 1:


Sno   SID   Sname
1   Null   A
2   Null   B
3   Null   C
4   Null   D
5   Null   E




Table 2:


ano   aID   aName
1   55   AA
2   32   BB
3   53   CC
4   10   DD
5   10   EE




Requirement:
I need to update Table 1 - Column SID values with Table 2 - Column aID in a single update query

Output data should be:

 
Sno   SID   Sname
1   55   A
2   32   B
3   53   C
4   10   D
5   10   E


Kingston Dhasian
Kingston Dhasian
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2999 Visits: 4986
This seems quite simple like homework stuff
Can you let us know what you have tried?
If you are stuck somewhere, we are happy to help you.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2234 Visits: 7415
This should get you started...


IF OBJECT_ID('tempdb..#table1') IS NOT NULL
   DROP TABLE #table1;

IF OBJECT_ID('tempdb..#table2') IS NOT NULL
   DROP TABLE #table2;

CREATE TABLE #table1 (sno int unique NOT NULL, [sid] int NULL, sname varchar(2) unique NOT NULL);
CREATE TABLE #table2 (ano int unique NOT NULL, [aid] int NULL, aname varchar(2) unique NOT NULL);

INSERT INTO #table1
   SELECT 1,Null,'A' UNION ALL
   SELECT 2,Null,'B' UNION ALL
   SELECT 3,Null,'C' UNION ALL
   SELECT 4,Null,'D' UNION ALL
   SELECT 5,Null,'E';

INSERT INTO #table2
   SELECT 1,55,'AA' UNION ALL
   SELECT 2,32,'BB' UNION ALL
   SELECT 3,53,'CC' UNION ALL
   SELECT 4,10,'DD' UNION ALL
   SELECT 5,10,'EE';

-- Here's what we need #table 1 to look like:
SELECT   t1.sno,
      t2.aid,
      t1.sname
FROM #table1 t1
JOIN #table2 t2 ON t1.sno=t2.ano;

-- This will do it (Update based on a join)
UPDATE t1
SET t1.[sid]=t2.aid   
FROM #table1 t1
JOIN #table2 t2 ON t1.sno=t2.ano;

-- did it work?
SELECT * FROM #table1;

--cleanup
DROP TABLE #table1;
DROP TABLE #table2;
GO



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
robertd 77391
robertd 77391
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 7
A simple way to do this:



update #Table1
set #Table1.SID = #Table2.aID
from #Table2
where #Table1.Sno = #Table2.Sno ;



Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16512 Visits: 16985
robertd 77391 (3/12/2013)
A simple way to do this:



update #Table1
set #Table1.SID = #Table2.aID
from #Table2
where #Table1.Sno = #Table2.Sno ;




I can't for the life me remember the details but there is an issue when you do an UPDATE FROM and the table being updated is not in the query. It is better to use the join method as demonstrated by Alan above.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24146 Visits: 37920
Sean Lange (3/12/2013)
robertd 77391 (3/12/2013)
A simple way to do this:



update #Table1
set #Table1.SID = #Table2.aID
from #Table2
where #Table1.Sno = #Table2.Sno ;




I can't for the life me remember the details but there is an issue when you do an UPDATE FROM and the table being updated is not in the query. It is better to use the join method as demonstrated by Alan above.


I think what is needed for this would be this:



update #Table1
set SID = (SELECT #Table2.aID from #Table2 where #Table1.Sno = #Table2.Sno) ;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
ManiDBLover
ManiDBLover
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 75
Hi All,

Thanks for your help.

All your solutions worked!


Regards,
Mani
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search