Advertisement

12.31.2007 at 05:42AM PST, ID: 23050647
[x]
Attachment Details

T-SQL Loop through records of a query

Asked by Bonnie_K in SQL Query Syntax, MS SQL Server

Tags: Mic, SQL

Hi,

I have a query:

select name from Archive_man_live.dbo.syscolumns where id = (select id from Archive_man_live.dbo.sysobjects where name = 'JOBS'

And I want to loop through all of the records to create a variable containing the 'name' value from each row seperated by a comma.  I think my syntax is right for creating the growing variable, but I can't figure out how to loop through all records in T-SQL until the end of the recordset.

So,
DECLARE @String NVARCHAR(1000)
SELECT @String= ''

/* WHILE or FOR statement or DO UNTIL???? */

BEGIN
SELECT @String = @String & ',' & name
END

Thanks for any assistance,
Bonnie
Start Free Trial
[+][-]12.31.2007 at 05:58AM PST, ID: 20554674

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.31.2007 at 07:44AM PST, ID: 20555495

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.31.2007 at 07:55AM PST, ID: 20555602

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12.31.2007 at 10:13AM PST, ID: 20556547

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, MS SQL Server
Tags: Mic, SQL
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
[+][-]12.31.2007 at 12:20PM PST, ID: 20557191

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628