Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server PRINT SELECT (Print a select query result)?

I am trying to print a selected value, is this possible?

Example:

PRINT      SELECT SUM(Amount) FROM Expense 
like image 580
Shimmy Weitzhandler Avatar asked Dec 22 '09 02:12

Shimmy Weitzhandler


People also ask

How do I print a selected result 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).

How do I print query results?

To print query results on your default printer: Select Reporting Tools > Query > Query Manager. Click the Search button, and then click either the HTML or Excel links. Click the Print button or select File, Print.

How do I print a string 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

You know, there might be an easier way but the first thing that pops to mind is:

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).

like image 189
Mark Brittingham Avatar answered Sep 20 '22 17:09

Mark Brittingham