55

I am trying to debug stored procedures in SQL Server Management Studio 2008. I want to insert some print statements to test some IF-statements that I know are wrong.

In order to do debugging, I tried using the PRINT '5' command.

I tried using the RAISERROR like 'RAISERROR (N'Start',10,1) WITH NOWAIT'.

But these did not show any printouts, only the result set. The message just says 1 row affected.

I attempted to write the code like so (perhaps this is an incorrect approach):

SET NOCOUNT ON         
RAISERROR (N'Start',10,1) WITH NOWAIT    
DECLARE @DocHandle INT        
DECLARE @PageSize INT, @PageIndex INT, @TOTL_CONT NUMERIC(5,0), @Paging BIT        
DECLARE @Type INT, @Search varchar(20) , @ORDE nVARCHAR(50), @SORT_ID nVARCHAR(50) 
DECLARE @CreatedOn varchar(25), @SystemGenerate bit   

What is the best way to use print statements to debug a stored procedure?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Art F
  • 3,992
  • 10
  • 49
  • 81
  • 1
    Can you show us the if statement? If it didn't print, then presumably the test failed and it didn't enter the conditional block. – Bridge Mar 14 '13 at 15:34
  • Is the `1 row affected` when you update your stored procedure, or when you actually execute it? There's no reason why the code you've given wouldn't have printed something (to the messages window, not to the results). – David T. Macknet Mar 14 '13 at 16:09

5 Answers5

45

If you're using Microsoft SQL Server Management Studio (SSMS), print statements will print out under the Messages tab, not under the Results tab.

enter image description here

Print statements will appear there.

Pang
  • 9,564
  • 146
  • 81
  • 122
Kevin Kunderman
  • 2,036
  • 1
  • 19
  • 30
  • 3
    mm, as mentioned, the only thing that I see there is `1 row affected`. – Art F Mar 14 '13 at 15:24
  • 2
    Have you tried putting a print statement as the first line of the procedure just to see if it prints. It could be that the if statements are not being entered into like you expect. – Kevin Kunderman Mar 14 '13 at 15:29
  • I can confirm that the current SQL Server Management Studio doesn't show those messages (the Server runs on Amazon's Cloud). – Marc Guillot Feb 21 '18 at 17:01
26

Here is an example of print statement use. They should appear under the messages tab as a previous person indicated.

Declare @TestVar int = 5;

print 'this is a test message';
print @TestVar;
print 'test-' + Convert(varchar(50), @TestVar);

Print Messages

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
cmartin
  • 2,819
  • 1
  • 26
  • 31
21

Before I get to my reiterated answer; I am confessing that the only answer I would accept here is this one by KM. above. I down voted the other answers because none of them actually answered the question asked or they were not adequate. PRINT output does indeed show up in the Message window, but that is not what was asked at all.

Why doesn't the PRINT statement output show during my Stored Procedure execution?
The short version of this answer is that you are sending your sproc's execution over to the SQL server and it isn't going to respond until it is finished with the whole transaction. Here is a better answer located at this external link.

  • For even more opinions/observations focus your attention on this SO post here.
  • Specifically look at this answer of the same post by Phil_factor (Ha ha! Love the SQL humor)
  • Regarding the suggestion of using RAISERROR WITH NOWAIT look at this answer of the same post by JimCarden

Don't do these things

  1. Some people are under the impression that they can just use a GO statement after their PRINT statement, but you CANNOT use the GO statement INSIDE of a sproc. So that solution is out.
  2. I don't recommend SELECT-ing your print statements because it is just going to muddy your result set with nonsense and if your sproc is supposed to be consumed by a program later, then you will have to know which result sets to skip when looping through the results from your data reader. This is just a bad idea, so don't do it.
  3. Another problem with SELECT-ING your print statements is that they don't always show up immediately. I have had different experiences with this for different executions, so don't expect any kind of consistency with this methodology.

Alternative to PRINT inside of a Stored Procedure
Really this is kind of an icky work around in my opinion because the syntax is confusing in the context that it is being used in, but who knows maybe it will be updated in the future by Microsoft. I just don't like the idea of raising an error for the sole purpose of printing out debug info...

It seems like the only way around this issue is to use, as has been explained numerous times already RAISERROR WITH NOWAIT. I am providing an example and pointing out a small problem with this approach:

ALTER
--CREATE 
    PROCEDURE [dbo].[PrintVsRaiseErrorSprocExample]
AS
BEGIN
    SET NOCOUNT ON;

    -- This will print immediately
    RAISERROR ('RE Start', 0, 1) WITH NOWAIT
    SELECT 1;

    -- Five second delay to simulate lengthy execution
    WAITFOR DELAY '00:00:05'

    -- This will print after the five second delay
    RAISERROR ('RE End', 0, 1) WITH NOWAIT
    SELECT 2;
END

GO

EXEC [dbo].[PrintVsRaiseErrorSprocExample]

Both SELECT statement results will only show after the execution is finished and the print statements will show in the order shown above.

Potential problem with this approach
Let's say you have both your PRINT statement and RAISERROR statement one after the other, then they both print. I'm sure this has something to do with buffering, but just be aware that this can happen.

ALTER
--CREATE 
    PROCEDURE [dbo].[PrintVsRaiseErrorSprocExample2]
AS
BEGIN
    SET NOCOUNT ON;

    -- Both the PRINT and RAISERROR statements will show
    PRINT 'P Start';
    RAISERROR ('RE Start', 0, 1) WITH NOWAIT
    SELECT 1;

    WAITFOR DELAY '00:00:05'

    -- Both the PRINT and RAISERROR statements will show
    PRINT 'P End'
    RAISERROR ('RE End', 0, 1) WITH NOWAIT
    SELECT 2;
END

GO

EXEC [dbo].[PrintVsRaiseErrorSprocExample2]

Therefore the work around here is, don't use both PRINT and RAISERROR, just choose one over the other. If you want your output to show during the execution of a sproc then use RAISERROR WITH NOWAIT.

Community
  • 1
  • 1
dyslexicanaboko
  • 4,215
  • 2
  • 37
  • 43
  • 3
    External links are not frowned upon. Having an answer that solely relies on external links and has no substance itself is frowned upon. If you provide external links for people to get additional detail, that's actually a good thing. – mason Mar 09 '17 at 16:10
  • I hear you and that's how I have historically used them, only to be dinged or given criticism of using a link. The reasoning usually being "Links can go dead" with which I usually respond "I am not going to plagiarize someone else's work." which I have seen other people do because they are hunting for points. I just don't like those comments, so I was being preemptive about it this time. – dyslexicanaboko May 08 '17 at 14:51
  • 1
    You will only reasonable have someone complain if you use an external link and the body of the answer itself doesn't have enough information to answer the question itself. If you have enough information in your question, and just use external links as a supplement, you'll be fine. And if anyone complains, then just tell them what I've told you and don't worry about it. There's no need to apologize in your answer for including an external link either. – mason May 09 '17 at 14:30
  • 1
    Thank you for taking the time to answer your own question with the details that you uncovered. This has been most helpful. – Griffin May 24 '17 at 13:23
15

Try using:

RAISERROR('your message here!!!',0,1) WITH NOWAIT

You could also try switching to "Results to Text", which is just a few icons to the right of "Execute" on the default tool bar.

With both of the above in place, and if you still do not see the messages, make sure you are running the same server/database/owner version of the procedure that you are editing. Make sure you are hitting the RAISERROR command, make it the first command inside the procedure.

If all else fails, you could create a table:

create table temp_log (RowID int identity(1,1) primary key not null
                      , MessageValue varchar(255))

then:

INSERT INTO temp_log VALUES ('Your message here')

then after running the procedure (provided no rollbacks), just select the table.

Pang
  • 9,564
  • 146
  • 81
  • 122
KM.
  • 101,727
  • 34
  • 178
  • 212
0

Look at this Howto in the MSDN Documentation: Run the Transact-SQL Debugger - it's not with PRINT statements, but maybe it helps you anyway to debug your code.

This YouTube video: SQL Server 2008 T-SQL Debugger shows the use of the Debugger.

=> Stored procedures are written in Transact-SQL. This allows you to debug all Transact-SQL code and so it's like debugging in Visual Studio with defining breakpoints and watching the variables.

Adelphos
  • 83
  • 7