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

前端 未结 10 1757
心在旅途
心在旅途 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:16

    If you want to print more than a single result, just select rows into a temporary table, then select from that temp table into a buffer, then print the buffer:

    drop table if exists #temp
    
    -- we just want to see our rows, not how many were inserted
    set nocount on
    
    select * into #temp from MyTable
    
    -- note: SSMS will only show 8000 chars
    declare @buffer varchar(MAX) = ''
    
    select @buffer = @buffer + Col1 + ' ' + Col2 + CHAR(10) from #temp
    
    print @buffer
    
    

提交回复
热议问题