2013-03-04 4 views
0

플랫폼 : SQL Server 2012저장 프로 시저 메시지 파일에 출력

두 개의 저장 프로 시저가 등록 번호를 가져 와서 해당 등록 번호를 기반으로 HTML 및 XML 묶음을 인쇄합니다. proc에는 SSMS에서 실행될 때 Messages 영역에 침을 뱉어 낸 일련의 인쇄 문이 있습니다.

정말 멋지다. 나는 이런 식으로 설계된 것을별로 좋아하지 않지만 상속 받고 싶다.

이 절차를 실행하고 메시지 내용을 파일로 추출하려고합니다. 이상적으로 파일을 실행 한 후 파일의 이름을 등록 번호로 변경합니다.

간단히 말해서, SQL 문 메시지 부분을 파일로 출력 할 수 있습니다.

SSIS? 아니면 간단한 BCP 저장 프로 시저 명령? 아무도 이것에 빠지지 않니?

답변

0

은 SELECT 문 모든 PRINT 문을 대체 한 다음 테이블에 각 SELECT 문을 보냈다. 그런 다음 SSIS를 사용하여 HTM 파일을 제대로 작성하기 위해 해당 테이블을 가져옵니다.

0

execute spWriteStringToFile을 사용할 수 있습니다. 이 여기 찾았 그것에 대해 https://www.simple-talk.com/code/WorkingWithFiles/spWriteStringTofile.txt

ALTER PROCEDURE spWriteStringToFile 
(
@String Varchar(max), --8000 in SQL Server 2000 
@Path VARCHAR(255), 
@Filename VARCHAR(100) 

-- 
) 
AS 
DECLARE @objFileSystem int 
     ,@objTextStream int, 
     @objErrorObject int, 
     @strErrorMessage Varchar(1000), 
     @Command varchar(1000), 
     @hr int, 
     @fileAndPath varchar(80) 

set nocount on 

select @strErrorMessage='opening the File System Object' 
EXECUTE @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUT 

Select @[email protected]+'\'[email protected] 
if @HR=0 Select @[email protected] , @strErrorMessage='Creating file "'[email protected]+'"' 
if @HR=0 execute @hr = sp_OAMethod @objFileSystem , 'CreateTextFile' 
    , @objTextStream OUT, @FileAndPath,2,True 

if @HR=0 Select @[email protected], 
    @strErrorMessage='writing to the file "'[email protected]+'"' 
if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Write', Null, @String 

if @HR=0 Select @[email protected], @strErrorMessage='closing the file "'[email protected]+'"' 
if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Close' 

if @hr<>0 
    begin 
    Declare 
     @Source varchar(255), 
     @Description Varchar(255), 
     @Helpfile Varchar(255), 
     @HelpID int 

    EXECUTE sp_OAGetErrorInfo @objErrorObject, 
     @source output,@Description output,@Helpfile output,@HelpID output 
    Select @strErrorMessage='Error whilst ' 
      +coalesce(@strErrorMessage,'doing something') 
      +', '+coalesce(@Description,'') 
    raiserror (@strErrorMessage,16,1) 
    end 
EXECUTE sp_OADestroy @objTextStream 
EXECUTE sp_OADestroy @objTextStream 

전체 기사 : https://www.simple-talk.com/sql/t-sql-programming/reading-and-writing-files-in-sql-server-using-t-sql/