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.
| Author |
Topic |
|
deadfish
Starting Member
38 Posts |
Posted - 2003-05-29 : 23:00:42
|
| I have to update several columns (col1, col2, col3, col4...) depending on the variable @no_of_col:if @no_of_col = 1--update col1if @no_of_col = 2-- update col, col2if @no_of_col = 3-- update col, col2, col3...Is it possible to reduce the code into a 'while' loop instead of several 'if' statements?? |
|
|
aiken
Aged Yak Warrior
525 Posts |
Posted - 2003-05-29 : 23:23:45
|
You could definitely do it with WHILE and dynamic SQL.However, I have to question why you're doing this. There may be a much better approach that involves changing you schema, or at least your query. For instance,CREATE PROCECURE p_UpdateSome (@i1 int=NULL,@i2 int=NULL,i3 int=NULL) ASupdate table set col1=IsNull(@i1,col1),col2=IsNull(@i2,col2),col3=IsNull(@i3,col3) ...and, depending on your actual problem, there's likely to be a better solution than passing the number of columns to update in a variable.Cheers-b |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-05-30 : 03:11:33
|
quote: I have to update several columns (col1, col2, col3, col4...) depending on the variable @no_of_col:if @no_of_col = 1--update col1if @no_of_col = 2-- update col, col2if @no_of_col = 3-- update col, col2, col3...Is it possible to reduce the code into a 'while' loop instead of several 'if' statements??
You could use one case statement instead, but the essence would remain the same. The thing to do is to post with your problem, rather than posting your solution and asking if it's improvable.-------Moo. |
 |
|
|
|
|
|
|
|