Solved

How to print from a stored procedure

Posted on 2012-04-13
7
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:vasto
ID: 37844934
"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
ID: 37845186
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
ID: 37846447
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
ID: 37846492
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
ID: 37846702
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
ID: 37846793
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
ID: 37848713
Thanks for the input.

I suspect the problem was due to some if logic.

Dave
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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