Solved

How to use Print & RaiseError in TSQL

Posted on 2012-04-03
10
235 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
ID: 37803442
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
ID: 37827369
Anyone who can send me an TSQL examples?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37827883
--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
ID: 37827980
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
ID: 37828032
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:crazywolf2010
ID: 37828693
Please see attached snap here.
MSSM.gif
0
 
LVL 69

Expert Comment

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

Author Comment

by:crazywolf2010
ID: 37831394
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
ID: 37832174
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
ID: 37832190
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

12 Experts available now in Live!

Get 1:1 Help Now