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 2000 Forums
 SQL Server Development (2000)
 IF Condition or Data Transform within Update/FROM

Author  Topic 

charford
Starting Member

8 Posts

Posted - 2011-10-14 : 04:22:34
How can I use IF/BEGIN/END within the update statement?
I'm, working on a stored procedure to update a table from another using UPDATE...FROM
I need to check an incoming varchar value from my source table and convert it to a numerical value for the update

e.g.
UPDATE dbo.Table2

IF UD.code = 'S'
BEGIN
TagetVal = 20
END
ELSE
BEGIN
TargetVal = 0
END

FROM dbo.Table1 UD
WHERE Table2.ID = Table1.ID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-14 : 04:36:36
[code]
UPDATE t2
SET t2.TagetVal = CASE WHEN UD.code = 'S' THEN 20 ELSE 0 END
FROM dbo.Table2 t2
JOIN dbo.Table1 UD
ON UD.ID = t2.ID
[/code]


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

Go to Top of Page

charford
Starting Member

8 Posts

Posted - 2011-10-14 : 05:08:22
Many thanks to visakh16

UPDATE t2
SET t2.TagetVal = CASE WHEN UD.code = 'S' THEN 20 ELSE 0 END
FROM dbo.Table2 t2
JOIN dbo.Table1 UD
ON UD.ID = t2.ID

This works perfectly for me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-14 : 05:14:36
welcome

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

Go to Top of Page
   

- Advertisement -