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 statement Help!!!!!!!!!!

Author  Topic 

smithani
Starting Member

42 Posts

Posted - 2007-09-05 : 10:46:41
Hi,
I want to update the values of two collumns in a table with different data for the same condition , (plesea see below) Is there any other way of doing this without repeating the condition twice.


declare @selected int
set @selected = 2007

update Process set season=
case when year(nextdate) >@selected then year(nextdate) end,
set program=
case when year(nextdate) >@selected then nextdate end
end

Kristen
Test

22859 Posts

Posted - 2007-09-05 : 10:49:28
DECLARE @TempVar int
update Process
set
@TempVar = case when year(nextdate) >@selected THEN 1 ELSE 0 end,
season=
case when @TempVar = 1 then year(nextdate) end,
set program=
case when @TempVar = 1 then nextdate end
end

but it won't save much, if any, CPU time unless you have a more complicated algorithm. It might save a maintenance snafu in the future though ...

Kristen
Go to Top of Page

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2007-09-05 : 20:57:13
season and program will be NULL when year(nextdate) <= @selected.
Why tow conditions are same?
Go to Top of Page

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2007-09-05 : 21:44:33
sorry, misunderstood your question

UPDATE Process
SET seson = YEAR(nextdate), program = nextdate
WHERE YEAR(nextdate) > @selected
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-06 : 03:52:31
Ah, Koji is probably right. I though you had just posted a snippet of your total code!
Go to Top of Page
   

- Advertisement -