Solved

How to print from a stored procedure

Posted on 2012-04-13
7
139 Views
Last Modified: 2012-04-15
I am trying to debug a stored procedure. It would be helpful to be able to display the output at points.

I tried using PRINT sometext

but do not see it in either the output window or the Messages window. The messages window shows the messge "Command(s) completed successfully"

Is there some sort of setup I need to do to display messages from PRINT statements?

I know this is trivial, but I  have searched for a solution.

Thanks
0
Comment
Question by:KWDave
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:vasto
Comment Utility
"print" will not print anything in case the variable is null. also don't forget to convert the value to varchar, char etc.

You can actually debug a stored procedure in SSMS. Just use the  button  on the right of "Execute" button ( the one with with the green "play" like icon)
0
 

Author Comment

by:KWDave
Comment Utility
Thanks for your response.

I was trying to output text as a test - I used PRINT N'Test Print' as the print string.

I also tried using RaiseError, with the same (non) result.

I thought the print function was supposed to print to the console in either the Messages section, or the Output.

The debug function puts it's results in the Output window.

I was wondering if the problem was one of setup or an option to direct the PRINT output.

I figure it is something stupid I didn't do on setup, but can't find out what.



I may be able to function with the DEBUG, but would like to have the functionality of the PRINT
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I thought the print function was supposed to print to the console in either the Messages
section, or the Output.

You can easily verify that in SSMS PRINT outputs the result to the Messages window.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
Comment Utility
I am not sure if you are talking about SSMS or Visual Studio.
Message window in SSMS may show data printed by you, but also status messages, so you may need to scroll up to find your data.
0
 

Author Closing Comment

by:KWDave
Comment Utility
When i isolated the Print N'Test' it worked.

I will have to figure out how to get it to function within the code.

It is a large stored procedure and it would be great to be able to see intermediate states. I will work with the debug function as suggested to see if that does what i need.

Thanks,
Dave
0
 
LVL 18

Expert Comment

by:vasto
Comment Utility
Dave, there are 2 possible reasons to not see anything printed :
1. your code is not going trough the line because of a "if" logic
2. you are trying to print a variable which is null or empty string.  Let's say your variable name is  @SampleText; try this:

print '@SampleText value = ' + isnull(@SampleText,'<<string is empty>>')

if @SampleText has value 'some text' you will see
@SampleText value = some text

if @SampleText is empty but not null you will see
@SampleText value =

if @SampleText is null you will see
@SampleText value = <<string is empty>>
0
 

Author Comment

by:KWDave
Comment Utility
Thanks for the input.

I suspect the problem was due to some if logic.

Dave
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now