Feb 17, 2003 #1 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
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
Feb 17, 2003 1 #2 osjohnm Technical User Apr 4, 2002 473 0 0 ZA 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 Upvote 0 Downvote
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
Feb 18, 2003 1 #3 grahams Programmer Dec 3, 2002 40 0 0 GB You can use a WHILE loop in SQL Server - check it out in BOL: http://msdn.microsoft.com/library/en-us/tsqlref/ts_wa-wz_6oyt.asp?frame=true Cheers, Graham Upvote 0 Downvote
You can use a WHILE loop in SQL Server - check it out in BOL: http://msdn.microsoft.com/library/en-us/tsqlref/ts_wa-wz_6oyt.asp?frame=true Cheers, Graham
Feb 18, 2003 #5 cheyney Programmer Jul 16, 2002 335 0 0 CA 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 Upvote 0 Downvote
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