Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PRINT statement in T-SQL

Why does the PRINT statement in T-SQL seem to only sometimes work? What are the constraints on using it? It seems sometimes if a result set is generated, it becomes a null function, I assumed to prevent corrupting the resultset, but could it's output not go out in another result set, such as the row count?

like image 696
ProfK Avatar asked Nov 06 '08 11:11

ProfK


People also ask

How do you write a print statement in SQL?

Declare @SumVal int; Select @SumVal=Sum(Amount) From Expense; Print @SumVal; You can, of course, print any number of fields from the table in this way. Of course, if you want to print all of the results from a query that returns multiple rows, you'd just direct your output appropriately (e.g. to Text).

Is there a print function in SQL?

It helps to track the query progress. Usually, we use the SQL PRINT statement to print corresponding messages or track the variable values while query progress. We also use interactions or multiple loops in a query with a while or for a loop. We can also use the SQL PRINT statement to track the iteration.

What is print in SQL Server?

In Sql Server PRINT statement can be used to return message to the client. It takes string expression as input and returns string as a message to the application.


1 Answers

So, if you have a statement something like the following, you're saying that you get no 'print' result?

select * from sysobjects PRINT 'Just selected * from sysobjects'

If you're using SQL Query Analyzer, you'll see that there are two tabs down at the bottom, one of which is "Messages" and that's where the 'print' statements will show up.
If you're concerned about the timing of seeing the print statements, you may want to try using something like

raiserror ('My Print Statement', 10,1) with nowait

This will give you the message immediately as the statement is reached, rather than buffering the output, as the Query Analyzer will do under most conditions.

like image 141
David T. Macknet Avatar answered Sep 22 '22 19:09

David T. Macknet