How to use Print & RaiseError in TSQL

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
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).
crazywolf2010Author Commented:
Anyone who can send me an TSQL examples?
Scott PletcherSenior DBACommented:

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.
CEOs need to know what they should worry about

Nearly every week during the past few years has featured a headline about the latest data breach, malware attack, ransomware demand, or unrecoverable corporate data loss. Those stories are frequently followed by news that the CEOs at those companies were forced to resign.

crazywolf2010Author Commented:
I ran above code but there were no ouput at MS SQL server studio. Where does print will print?
Scott PletcherSenior DBACommented:
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.
crazywolf2010Author Commented:
Please see attached snap here.
Scott PletcherSenior DBACommented:
Click on the "Results" tab to move the focus off the "Messages" tab.
crazywolf2010Author Commented:
There is no results tab displayed. I do see results for other queries but not for PRINT queries.
Anthony PerkinsCommented:
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
Anthony PerkinsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.