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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop and print 2

Status
Not open for further replies.

mimi2

Technical User
Apr 2, 2002
407
0
0
CA
hello,
is there a way i could use a loop to print this:
var1 ='first line'
var2 ='second line'
var3 ='third line'

while @i <= 3
begin
set @new_var = var(i)
print @new_var
end

result:
first line
second line
third line
 
Hi

You can use a cursor although there might be another way to do this but I can't think of one.

DECLARE PRINT_CURSOR CURSOR FOR
SELECT var from yourtable

OPEN PRINT_CURSOR
declare @new_var varchar(50)
declare @var varchar(50)

fetch PRINT_CURSOR INTO @var
WHILE @@FETCH_STATUS = 0
begin

set @new_var = @var
print @new_var

FETCH PRINT_CURSOR INTO @var
end

CLOSE PRINT_CURSOR
DEALLOCATE PRINT_CURSOR


Hope this helps
John
 
If you want to do this with a single select statement:

DECLARE @bigstring = varchar(1000)

SELECT @bigstring = COALESCE(@bigstring + char(13), '') + columnName
FROM tableName
WHERE blah blah

print @bigstring
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top