Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Swapping Data between two columns (Unorthadox)

Author  Topic 

pandawatch410
Starting Member

1 Post

Posted - 2010-03-29 : 16:40:06
Hi guys and gals,

I have a uniqueidentifier column and an nvarchar column. The names in the nvarchar column obviously have a uniqueidentifier. Those two columns reside in a table called contacts. The other table involved is called accessor and this outlines which id represents which user in the table.

Normally a dataswap between two columns is simple, but I am running into trouble on this one because I need to join with accessor from table to get the uniqueidentifier for the name that is in the nvarchar column and that will replace the uniqueidentifier column, while then I need to go back to the accessor table to get the name from the first uniqueidentifier to place in the nvarchar column that moved to the uniqueidentifier column.

Was that complex enough for you? haha, sorry it was so confusing. anwyays, I tried the following:
declare @switch nvarchar(50)
declare @switch2 uniqueidentifier
update tbl_contact set @switch = (select name from tbl_contact join tbl_Accessor on
accessorid=manageuserid), @switch2 = (select accessorid from tbl_contact join tbl_accessor on name = cust_salesperson_063927859), cust_salesperson_063927859 = @switch, manageuserid = @switch2


This did not work obviously because there are multiple records returned from the subqueries...

HELP!

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-03-29 : 16:49:03
Dump the data from the 2 columns out to a temporary table. Then join onto the temporary table in order to update the original.

There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

Buzzard724
Yak Posting Veteran

66 Posts

Posted - 2010-03-29 : 16:50:13
How about cross (or outer) apply to select just the first record that returns the unique Identifier? I am not sure that I can follow your fields names in which table - so in principle

Update Table1 SET Field1 = CA.ID FROM Table1
CROSS APPLY (select TOP 1 ID from Table2 WHERE Table2.UID = Table1.UID) CA

Use Outer Apply if you cannot guarantee that there will always be a match
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-30 : 14:02:18
quote:
Originally posted by Buzzard724

How about cross (or outer) apply to select just the first record that returns the unique Identifier? I am not sure that I can follow your fields names in which table - so in principle

Update Table1 SET Field1 = CA.ID FROM Table1
CROSS APPLY (select TOP 1 ID from Table2 WHERE Table2.UID = Table1.UID) CA

Use Outer Apply if you cannot guarantee that there will always be a match


will cause a NULL to be updated in case of no match

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -