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
 Transact-SQL (2000)
 How to make dynamic string of string values

Author  Topic 

ikhuram
Starting Member

28 Posts

Posted - 2004-05-26 : 06:22:42
Hiw I can make a dynamic string of string values e.g.,

value1 = '001'
value2 = '002'
value3 = '003'
value4 = '004'

declare @output varchar(100)
select @output = ????
HOW TO PUT ALL THESE VALUES IN @output like this
@output = '001', '002', '003', '004'

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-05-26 : 06:31:09
Are value1, value2, value3, value4 columns in a table?

select '''' + value1 + '''' + ',' + '''' + value2 + '''' + ',' + '''' + value3 + '''' + ',' + '''' + value4 + ''''
from mytable



Duane.
Go to Top of Page

ikhuram
Starting Member

28 Posts

Posted - 2004-05-26 : 06:36:25
No, These are the values of one column
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-05-26 : 06:48:15
well you'll probably need a dreaded cursor for this.

btw - why do you want to do this?


Duane.
Go to Top of Page

Pethron
Starting Member

10 Posts

Posted - 2004-05-26 : 07:10:18
Maybe this would be the sollution:

declare @output varchar(50)
declare curex cursor for select myCollumn from MyTable
declare @fetch int, @value varchar (10)
set @fetch = 0

open curex
while @fetch =0
begin
fetch next from curex
into @value
set @fetch = @@fetch_Status
select @output = isnull(@output,'') + @value
end
close curex
deallocate curex

select @output


@Duane:
What is a dreaded cursor?
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-05-26 : 07:17:32
well I mentioned dreaded cursor because cursors are very cpu expensive - good practice is to try and find a set based solution before trying to use cursors.

which is why i asked why this string must be created like ikhuram requested - maybe what he/she is trying to do can be achived by using a simple but clever set based method.


Duane.
Go to Top of Page
   

- Advertisement -