SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Processing multiple rows & display with SELECT
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rock_query
Yak Posting Veteran

52 Posts

Posted - 05/22/2013 :  14:33:56  Show Profile  Reply with Quote
I am processing through 5 rows, one row at a time, then displaying the row number and ProductID with the PRINT statement. This works fine:

DECLARE @counter INT, @rowValue INT
SELECT TOP 1 @rowValue=ProductID FROM Production.WorkOrder
SET @counter = 0
WHILE @counter < 5
BEGIN
SET @counter = @counter + 1
PRINT CAST(@counter AS CHAR(5)) + ' ' + CAST(@rowValue AS CHAR(5))
SELECT TOP 1 @rowValue=ProductID FROM Production.WorkOrder WHERE ProductID > @rowValue
END

Now I want to display the same data, only now I want to list column names. I figured I need a SELECT statement with two aliases (RowNumber and ProductID).

DECLARE @counter INT, @rowValue INT
SELECT TOP 1 @rowValue=ProductID FROM Production.WorkOrder
SET @counter = 0
WHILE @counter < 5
BEGIN
SET @counter = @counter + 1
SELECT CAST(@counter AS CHAR(5)) AS RowNumber, CAST(@rowValue AS CHAR(5)) AS ProductID
SELECT TOP 1 @rowValue=ProductID FROM Production.WorkOrder WHERE ProductID > @rowValue
END

This does work, but each row is listed as a separate instance because the SELECT statement appears inside of a loop. Is there a way to get all five values to display together under one instance, instead of having 5 instances listed out in my results window pane? Here

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 05/22/2013 :  14:36:22  Show Profile  Reply with Quote
can you post sample data in consumable format along with the desired ouput??

Cheers
MIK
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3451 Posts

Posted - 05/22/2013 :  15:28:03  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
there are a few potential problems.

1) you do a SELECT TOP 1 with no ORDER BY Clause. The order of rows returned is not guaranteed! it will usually be in CLUSTERED INDEX order (PK usually) but it isn't guaranteed.

If you must do things this way (row by row processing). Then create a temp table or table variable and insert the processed rows into that. Then do a final select at the end.

But whatever you are doing, there is probably a better way than row by bloody row.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000