SQL select print out results of stored procedure

前端 未结 2 1302
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 13:00

My businesses application supports only reporting with selected data from SQL server.In one business process I have very complicated stored procedure which using others stor

相关标签:
2条回答
  • 2020-12-21 13:15

    There is also one rough and probably BAD way to get selected data from print commands inside stored procedure.

    Command xp_cmdshell and sqlcmd can do the JOB. Xp_cmdshell is mostly disabled and not allowed to use at most of SQL servers because of security reasons.

    Here is code:

    CREATE TABLE #temp
    (OUTPUT VARCHAR(MAX));
    
        declare @cmd varchar(800);
        set  @cmd = 'sqlcmd -d RobotTest -Q "exec sp_test_print_out"';
    
        INSERT INTO #TEMP
        exec xp_cmdshell @cmd  ;
    
    select output from #temp;
    drop table #temp;
    
    0 讨论(0)
  • 2020-12-21 13:40

    You can try setting the values in output parameter

    create procedure sp_test_print_out
    @printMessages varchar(max) output
    as
    begin
    set @printMessages='Test'
    Print 'Test';
    
    set @printMessages= @printMessages + CHAR(10)
    set @printMessages= @printMessages + 'Test 1'
    print 'Test 1';
    end
    go
    
    
    create procedure sp_test_print_out_to_select
    as 
    begin
    declare @printOut varchar(max)
    exec sp_test_print_out @printOut output -- can be achieved using output parameter ?
    select @printOut
    end
    
    go
    
    exec sp_test_print_out_to_select
    
    0 讨论(0)
提交回复
热议问题