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 |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-12 : 13:42:54
|
| Please correct me in getting the below output after executing the query:UPDATE Employees SET ST_NAME = UPPER('STNAME')UPDATE Employees SET ST_NAME = UPPER('STNAME'), COUNTYNAME = UPPER('COUNTYNAME')Below is my query:declare @TblName varchar(50)declare @Col1 varchar(50)declare @Col2 varchar(50)declare @Col3 varchar(50)declare @Col4 varchar(50)declare @numofcol intset @TblName ='Employees'set @Col1 ='STNAME'set @Col2 ='COUNTYNAME'set @Col3 =Nullset @Col4 =nullset @numofcol =2declare @vsSQL varchar(8000), @Col varchar(50), @icol int, @scol varchar(50)SET @icol = 1set @vsSQL = 'UPDATE ' + @TblName + ' SET ' + @Col1 +' = UPPER(''' +@Col1 + ''')'print @vsSQLWHILE @icol < @numofcol BEGIN set @icol = @icol + 1 select @scol = CONVERT(varchar(50), @icol) SET @Col = '@Col' + @scol set @vsSQL = @vsSQL + ', ' + @Col + ' = UPPER(''' +@Col + ''')'print @vsSQLENDThanks for your help in advance |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2007-02-13 : 07:54:04
|
| i think it build the same... but when u execute it use sp_executesql======================================Ask to your self before u ask someone |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-13 : 08:09:02
|
[code]set @vsSQL = 'UPDATE ' + @TblName + ' SET ' + @Col1 + ' = UPPER(''' + @Col1 + ''')'if @Col2 is not null set @vsSQL = @vsSQL + ',' + @Col2 + ' = UPPER(''' + @Col2 + ''')'if @Col3 is not null set @vsSQL = @vsSQL + ',' + @Col3 + ' = UPPER(''' + @Col3 + ''')'if @Col4 is not null set @vsSQL = @vsSQL + ',' + @Col4 + ' = UPPER(''' + @Col4 + ''')'print @vsSQLWHILE @icol < @numofcolBEGINset @icol = @icol + 1select @scol = CONVERT(varchar(50), @icol)SET @Col = '@Col' + @scolset @vsSQL = @vsSQL + ', ' + @Col + ' = UPPER(''' +@Col + ''')'print @vsSQLEND[/code] KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-13 : 08:52:39
|
What if @Col1 is NULL also? Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-13 : 09:05:51
|
then @vsSQL will be NULL and nothing will get executed.  KH |
 |
|
|
|
|
|
|
|