Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use Print & RaiseError in TSQL

Posted on 2012-04-03
10
Medium Priority
?
255 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 70

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 70

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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 70

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 70

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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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