Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building a Variable?

Status
Not open for further replies.

strangeBrew12

Programmer
Apr 5, 2006
18
US
I am using a cursor to manipulate my data from individual records to one long string (for reporting purposes). I am using the following:

Exec('DECLARE PM CURSOR FOR SELECT * FROM '+@UserTable99+'')

OPEN PM

FETCH PM INTO @PM8sub
WHILE @@FETCH_STATUS=0
BEGIN
SET @JJ = 'SELECT PopDesc FROM PopUps WHERE Pop# = 30 AND PopChar = '''+@PM8sub+''''
Exec(@JJ)
FETCH NEXT FROM PM INTO @PM8sub
END

CLOSE PM
DEALLOCATE PM

How can I take the single record output by 'Exec(@JJ)' concatinate that with the variable. Basically @JJ + ', ' + @JJ

Thanks
JJ
 
You don't need a cursor if you want a comma delimited string. Take a look at this code, you may be able to adapt it to your purposes.

Code:
[green]-- Setup the data.[/green]
Declare @Test Table(Id Integer, Data VarChar(10))

Insert Into @Test Values(1, 'This')
Insert Into @Test Values(1, 'is')
Insert Into @Test Values(1, 'some')
Insert Into @Test Values(1, 'text')

[green]-- Example of comma delimited string[/green]
Declare @Output VarChar(8000)

Set @Output = ''

Select @Output = @Output + Data + ','
From   @Test

Select @Output = Left(@Output, Len(@Output)-1)

Select @Output

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top