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
 General SQL Server Forums
 New to SQL Server Programming
 Update column problem!

Author  Topic 

maevr
Posting Yak Master

169 Posts

Posted - 2008-01-13 : 17:32:18
I have a table (table1) which I need to update with values based on another table (table2).
------------------------------------------------
table2 looks like this:
id, columnName, value_true, value_false.

value_true and value_false can contain values like NULL, and strings.

I need to write a dynamic store procedure that updates all columns in table1 that exists in table2 with the values in table2.value_true or table2.value_false if the column in table1 is either true or false.

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2008-01-13 : 18:38:24
You don't need dynamic SQL for this


Update Table1
SET [Value_True] = [Table2].Value_True
FROM [Table1] INNER JOIN [Table2] on [Table1].[ID] = [Table2].[ID]



Repeat for Table1.Value_False as needed.

OR


Update Table1
SET [Value_True] = Case WHen [Table2].Value_True is not null Then [Table2].Value_True ELSE NULL END
,[Value_False] = Case When [Table2].Value_False is not null THEN [Table2].Value_False else NULL end

FROM [Table1] LEFT JOIN [Table2] on [Table1].[ID] = [Table2].[ID]



If your columns are set up like that...might need to rething...you can have true/false or other indicators stored more efficiently.





Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2008-01-14 : 01:51:14
Thanks for your reply.

But, The thing is that the column names is not known in table1, I created a temporary table containing all column names in table1. I want to use the temporary table to get be able to update table1 column with values from table2 if it is true or false.

Sorry for being unclear.
Go to Top of Page
   

- Advertisement -