Solved

How to use Print & RaiseError in TSQL

Posted on 2012-04-03
10
241 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
[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
  • 4
  • 4
  • 2
10 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
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:Scott Pletcher
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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:Scott Pletcher
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
 

Author Comment

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

Expert Comment

by:Scott Pletcher
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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