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

前端 未结 10 1759
心在旅途
心在旅途 2020-12-25 09:37

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

Example:

PRINT 
    SELECT SUM(Amount) FROM Expense
10条回答
  •  心在旅途
    2020-12-25 10:10

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

提交回复
热议问题