+1  A: 

Debugging this way the only way you'll get output is

select * from #temp_table;

Alternatively, look into the debugging features built into SQL Server Management Studio. For example, this web page may help you

SQL Server Performance . com

CResults
Will that print to the console though? It does if this is the only statement that I am executing. In fact I just tried it - it does not print anything. The `print @sql;` statement worked though.
Hamish Grubijan
An SP will output every select that isn't destined elsewhere, for example as part of an insert or @variable update
CResults
@CResults, would you please elaborate? I am not sure I understood this comment.
Hamish Grubijan
For examplecreate proc MyProcasSelect * from MyTable;Select * from MyTable;the SP will output two record sets to you.print @sql will work as @sql is a string. #temp_table isn't, it is a record set.
CResults
@CResults: The dynamic sql has a problem. It returns: Msg 4104, Level 16, State 1, Line 1The multi-part identifier "X.Y" could not be bound. I am trying to debug a nasty mess of a join. It's a Catch-22 - the proc needs to execute correctly before I can debug it :)I need to make a small fix in 400 lines of SQL, so it is not that easy to strip out the parts that cause the error and use binary search / divide-n-conquer. What would really help is the ability to turn a table into a string and print it.
Hamish Grubijan
Do you have access to the management studio - this is the best place to debug your SQL. Alternatively, edit your question to include the SQL and I'll try and help debug it.
CResults
Sorry missed the bit about the **400** lines of SQL, maybe don't add it to your question :). However you REALLY REALLY need to use management studio to help you debug this as relying on PRINT and the console won't help you.
CResults
Thanks, I do have the management Studio. I see the point now. I had to use print because the query was too long to be displayed in the window properly. Thanks again.
Hamish Grubijan
+1  A: 

You can print Variables, but not tables. You can, however, SELECT from the #table.

Now, if the table is created, filled up and modified in a single statement that is executed, then you can view the state of the table as it was before being modified, but the data will have changed since.

of course, as soon as the dynamic sql finishes, the #table is no longer available so you're stuck!

To counter that, you can insert into a ##Table (note the double hash marks) in your dynamic SQL along with the #table and then query that ##table at the end of execution of the dynamic sql.

Raj More
@Raj, it is actually a hybrid of static and dynamic sql, so the dynamic table is created before the dynamic sql that uses it is patched up together. My question still: how do I print the results of that select to the console, to a file, etc.?
Hamish Grubijan
@Hamish, if you do a `SELECT` that will print to the console.
Raj More
+1  A: 

for as much as I hate cursors, give this a try:

SET NOCOUNT ON
CREATE TABLE #TempTable1
(ColumnInt      int        
,ColumnVarchar  varchar(50)
,ColumnDatetime datetime   
)
INSERT INTO #TempTable1 VALUES (1,'A',GETDATE())
INSERT INTO #TempTable1 VALUES (12345,'abcdefghijklmnop','1/1/2010')
INSERT INTO #TempTable1 VALUES (null,null,null)
INSERT INTO #TempTable1 VALUES (445454,null,getdate())
SET NOCOUNT OFF

DECLARE @F_ColumnInt      int
       ,@F_ColumnVarchar  varchar(50)
       ,@F_ColumnDatetime datetime

DECLARE CursorTempTable1 CURSOR FOR
    SELECT
        ColumnInt, ColumnVarchar, ColumnDatetime
    FROM #TempTable1
    ORDER BY ColumnInt
    FOR READ ONLY

--populate and allocate resources to the cursor
OPEN CursorTempTable1

PRINT '#TempTable1 contents:'
PRINT '    '+REPLICATE('-',20)
       +'  '+REPLICATE('-',50)
       +'  '+REPLICATE('-',23)

--process each row
WHILE 1=1
BEGIN

    FETCH NEXT FROM CursorTempTable1
        INTO @F_ColumnInt, @F_ColumnVarchar, @F_ColumnDatetime

    --finished fetching all rows?
    IF @@FETCH_STATUS <> 0
    BEGIN --YES, all done fetching
        --exith the loop
        BREAK
    END --IF finished fetching

    PRINT '    '+RIGHT(   REPLICATE(' ',20)   +   COALESCE(CONVERT(varchar(20),@F_ColumnInt),'null')                               ,20)
           +'  '+LEFT(                            COALESCE(@F_ColumnVarchar,'null')                        +   REPLICATE(' ',50)   ,50)
           +'  '+LEFT(                            COALESCE(CONVERT(char(23),@F_ColumnDatetime,121),'null') +   REPLICATE(' ',23)   ,23)

END --WHILE

--close and free the cursor's resources
CLOSE CursorTempTable1
DEALLOCATE CursorTempTable1

OUTPUT:

#TempTable1 contents:
--------------------  --------------------------------------------------  -----------------------
                null  null                                                null                   
                   1  A                                                   2010-03-18 13:28:24.260
               12345  abcdefghijklmnop                                    2010-01-01 00:00:00.000
              445454  null                                                2010-03-18 13:28:24.260

If I knew that your temp table had a PK, I'd give a cursor free loop example.

KM
@KM, if you are going to create a cursor at all, it would be a much better idea to create a #Cursor
Raj More
@Raj More, If I knew the PK structure of the table in question I could have written a cursor free loop, like here: http://stackoverflow.com/questions/2478908/how-can-i-set-default-seed-for-all-identities-within-a-sql-server-database/2479298#2479298 but this cursor is generic enough for the OP to get the code working. what is a `#Cursor`?
KM
@KM A #cursor is just like a #temp table. Just available for the current connection.
Raj More
+1  A: 

When we use dynamic SQl we start by having a debug input variable in the sp (make it the last one and give it a default value of 0 (to indicate not in debug mode that way it won't break existing code calling the proc).

Now when you run it in debug mode, you print instead of execute or you print and execute but you always rollback at the end. If you need to see the data at various stages, the best thing to do is the second. Then before you rollback you put the data you want to see into a a table varaiable (This is important it can't be a temp table). after the rollback, select from the table variable (which did not go out of scope with the rollback) and run your print tstatments to see the queries which were run.

HLGEM