2012-08-23 2 views
2

간단한 코드입니다. 아주 이상한 행동, 트리플 체크. @filePath@pathNVARCHAR(260)이고 @existsINT입니다. dbo.WriteToFile은 파일을 디스크에 씁니다.이 경우 빈 파일입니다. SQL Server가 RAISERROR를 건너 뜁니다.

EXEC master..xp_fileexist @filePath, @exists OUTPUT 
print @exists 
IF @exists = 0 
BEGIN 
    EXEC dbo.WriteToFile N'', @path, @filename 
    RAISERROR('A', 20, -1) WITH log 
END 
RAISERROR('B', 20, -1) WITH log 

내가 처음으로이 코드를 실행

@exists는이 경우 블록으로 이동, 0입니다. 파일이 예상대로 작성됩니다. 그것을 확인했다. 그러나 RAISERROR 인 경우 if 블록은 이 아니고이라고합니다. 대신 if 블록 외부의 RAISERROR 만 호출됩니다.

그러나 첫 번째 RAISERROR ('A')PRINT 'blabla'으로 바꾸면 print 문이 예상대로 호출됩니다.

내가 1 = 1 @exists = 0을 교체하는 경우는 처음 RAISERROR ('A')가 그대로 다시 모든 것이 제대로 작동하고 처음 RAISERROR ('A')가 호출을 유지한다.

누군가 설명 할 수 있습니까?

오, 그래

, 그리고 새로운 쿼리 창에

print 'bla' 
raiserror('bla', 20, -1) with log 

을하지 왜 나에게이 얻을 :

bla 
Meldung 2745, Ebene 16, Status 2, Zeile 3 
Der Prozess mit der ID 54 hat den Benutzerfehler 50000, Schweregrad 20, ausgelöst. Dieser Prozess wird von SQL Server beendet. 
bla 
Meldung 2745, Ebene 16, Status 2, Zeile 3 
Der Prozess mit der ID 54 hat den Benutzerfehler 50000, Schweregrad 20, ausgelöst. Dieser Prozess wird von SQL Server beendet. 

이것은 2 번 같은 메시지를. 어쩌면 내 SQL 서버가 잘못 구성 되었습니까?

ALTER PROCEDURE dbo.WriteToFile 
(
@String Varchar(max), --8000 in SQL Server 2000 
@Path VARCHAR(255), 
@Filename VARCHAR(100) 
) 
AS 
BEGIN 
DECLARE @objFileSystem int 
       ,@objTextStream int, 
     @objErrorObject int, 
     @strErrorMessage Varchar(1000), 
      @Command varchar(1000), 
      @hr int, 
     @fileAndPath varchar(80) 

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 
END 

편집 : 당신이 관심이 있다면

나는 어쩌면 내가 웹에서이 절차를 가지고 있습니다,하지만 완벽하게 작동하지만, 어쩌면 그것은 RAISERROR 동작은 생산, 너무 WriteToFile 절차를 게시 할 예정입니다 : 실행 SQL 서버 2008 R2

답변

0

난 당신이 정말 "20"오류 수준을 필요로하는 경우 알고 있지만, 함께 해보시기 바랍니다 해달라고 :

RAISERROR('A', 16, -1) WITH log