user918967
user918967

Reputation: 2167

Is there a way in SQL Server Management Studio to number/identify query results?

When using SQL Server 2014 and SMSS 2014, is there some way to identify which query is which results when the SQL output is set to Results to Grid?

 DECLARE @foo TABLE (ID int, data int)
 DECLARE @bar TABLE (ID int, data varchar(1))

 INSERT INTO @foo VALUES (1,10),(2,11),(3,11),(4,11)
 INSERT INTO @bar VALUES (1,'a'),(2,'b'),(3,'c'),(4,'d')

 --Query 1
 SELECT * FROM @foo

 --Query 2
 SELECT * FROM @bar

Sometimes I string a several queries together so I can see the steps of my algorithm and I cannot always identify which results are from which query.

QueryResultsGrid

Upvotes: 1

Views: 59

Answers (1)

HLGEM
HLGEM

Reputation: 96552

 SELECT 'query 1', * FROM @foo

 SELECT 'query 2', * FROM @bar

Use this only for test queries. Of course I usually put something more useful about the query than just "query 1", like "budgets before adjustment" then "budgets after adjustment" for query 2.

I also tend to wrap these types of test queries in a test process where I have a a debug value as a parameter and then write the query:

IF @Debug = 1
BEGIN
  SELECT 'query 1', * FROM @foo
END

Upvotes: 2

Related Questions