2010-09-30 63 views
2

我的企業應用程序僅支持從SQL服務器中選擇的數據進行報告。在一個業務流程中,我有非常複雜的存儲過程,它使用其他存儲過程,它被設計爲將結果打印爲日誌任務完成。我想要捕獲的打印出來,並選擇它作爲varchar(最大),所以我的應用程序可以處理該數據並顯示給用戶。
下面是示例場景TSQL代碼描述:SQL選擇打印出存儲過程的結果

create procedure sp_test_print_out 
as 
begin 
    Print 'Test'; 
    print 'Test 1'; 
end 
go 

create procedure sp_test_print_out_to_select 
as 
declare @printOut varchar(max) 
set @printOut = exec sp_test_print_out --How I can achieve this ? 
select @printOut 
end 

go 

exec sp_test_print_out_to_select 

回答

1

您可以嘗試在輸出參數設定值

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

感謝名單,這也是在我心中,我是箍,以便有其他更簡單的方法處理,然後輸出參數。所以我現在需要處理所有打印出來的數據,包括新行,error_messages和其他內容 – adopilot 2010-09-30 09:48:14

0

也有獲得選擇從打印命令數據的一個粗糙,很可能爛路內部存儲過程。

命令xp_cmdshellsqlcmd可以做JOB。由於安全原因,Xp_cmdshell通常處於禁用狀態並且不允許在大多數SQL服務器上使用。

這裏是代碼:

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;