Solved

How to use Print & RaiseError in TSQL

Posted on 2012-04-03
10
234 Views
Last Modified: 2012-06-27
Hi,
I am writing some TSQL procedures to extract data.. I wish to know progress within my procedure  so using print stmt. I have used dbms_output.print_line under Oracle SQL. I though print here does the same job but I am not receiving any output at SSMS.

Do I need to use raiseerror? Please explain with examples.

Thanks
0
Comment
Question by:crazywolf2010
  • 4
  • 4
  • 2
10 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
In general PRINT will work.

Keep in mind, if you PRINT null, it will look like a blank line (you won't actually see any output).
0
 

Author Comment

by:crazywolf2010
Comment Utility
Anyone who can send me an TSQL examples?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
--Example:

DECLARE @varchar_var varchar(40)
DECLARE @varchar_var2 varchar(40)
DECLARE @int_var int

PRINT @int_var
PRINT @varchar_var + ' / ' + @varchar_var2
PRINT @varchar_var + ' / ' + CAST(@int_var AS varchar(10))


You can directly PRINT any single variable.

You can directly PRINT a concatenation of varchar variables.

If you want to concatenate numeric values for PRINTing, you must explicitly convert them to [var]char.
0
 

Author Comment

by:crazywolf2010
Comment Utility
Hi,
I ran above code but there were no ouput at MS SQL server studio. Where does print will print?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Shows in the window below the query.  When there's output, the output/results window should be shown, unless it's hidden.  You can press Ctrl-R to toggle the results window view on/off.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:crazywolf2010
Comment Utility
Please see attached snap here.
MSSM.gif
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Click on the "Results" tab to move the focus off the "Messages" tab.
0
 

Author Comment

by:crazywolf2010
Comment Utility
There is no results tab displayed. I do see results for other queries but not for PRINT queries.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
1. The PRINT statements show on the Messages tab and not the Results tab.
2. The reason you are not getting any output is because all three values result in NULLs
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
See for yourself (fix the obvious typo in xDECLARE):
xDECLARE @varchar_var varchar(40)
xDECLARE @varchar_var2 varchar(40)
xDECLARE @int_var int

SET @varchar_var = 'Test String'
PRINT '@int_var: ' + ISNULL(CAST(@int_var AS varchar(10)), 'NULL')
PRINT '@varchar_var +  /  + @varchar_var2: ' + ISNULL(@varchar_var + ' / ' + @varchar_var2, 'NULL')
PRINT '@varchar_var +  /  + CAST(@int_var AS varchar(10)): ' + ISNULL(@varchar_var + ' / ' + CAST(@int_var AS varchar(10)), 'NULL')

Open in new window

0

Featured Post

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.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

17 Experts available now in Live!

Get 1:1 Help Now