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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2001-04-06 : 15:31:40
|
Valter Borges writes "I have two variables with column names as such
@column_names = 'attr1, attr2' @column_names2 = 'param1, param2'
Is there a simpler, cleaner way to create the following
'attr1 = param1, attr2 = param2'
Currently I'm using a While loop as such.
DECLARE @substr nvarchar(4000) DECLARE @substr2 nvarchar(4000) DECLARE @str_set nvarchar(4000) DECLARE @index int DECLARE @index2 int DECLARE @start int DECLARE @start2 int
SELECT @index = 1 SELECT @start = 1 SELECT @index2 = 1 SELECT @start2 = 1 SELECT @str_set = ''
WHILE LEN(@column_names) > 0 AND @index > 0 BEGIN SELECT @index = CHARINDEX(',', @column_names , @index) SELECT @index2 = CHARINDEX(',',@column_names2, @index2) IF @index <> 0 BEGIN SELECT @substr = SUBSTRING(@column_names,@start,@index - 1) SELECT @substr2 = SUBSTRING(@column_names2,@start2,@index2 -1) SELECT @column_names = SUBSTRING(@column_names,@index+2,4000) SELECT @column_names2 = SUBSTRING(@column_names2,@index2+2,4000) SELECT @str_set = @str_set + ', ' + @substr + ' = ' + @substr2 END END
SELECT @str_set = @str_set + ', ' + @column_names + ' = ' + @column_names2
SELECT @str_set = SUBSTRING(@str_set,3,4000)" |
|
|
|
|
|
|
|