Simple code. Very strange behaviour, triple checked this. @filePath
and @path
are NVARCHAR(260)
and @exists
is INT
. dbo.WriteToFile
writes a file to disk, in this case an empty file.
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
When I run this code for the first time and @exists
is 0, it goes into the if block. File is created as expected. Confirmed that. But RAISERROR
in the if block is not called. Instead only the RAISERROR
outside the if block is called.
But if I replace the first RAISERROR ('A')
with PRINT 'blabla'
the print statement is called as expected.
If I replace @exists = 0
with 1=1 and keep the first RAISERROR ('A')
as is, again everything behaves correctly and the first RAISERROR ('A')
is called.
Can someone explain this?
Oh yeah, and why does
print 'bla'
raiserror('bla', 20, -1) with log
in a brand new query window get me this:
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.
This is 2 times the same message. Maybe my SQL server is configured incorrectly?
I will post the WriteToFile
procedure too if you are interested, maybe it helps, I have this procedure from the web, but it works perfectly, but maybe it produces the RAISERROR
behaviour:
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 @FileAndPath=@path+'\'+@filename
if @HR=0 Select @objErrorObject=@objFileSystem , @strErrorMessage='Creating file "'+@FileAndPath+'"'
if @HR=0 execute @hr = sp_OAMethod @objFileSystem , 'CreateTextFile'
, @objTextStream OUT, @FileAndPath,2,True
if @HR=0 Select @objErrorObject=@objTextStream,
@strErrorMessage='writing to the file "'+@FileAndPath+'"'
if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Write', Null, @String
if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='closing the file "'+@FileAndPath+'"'
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
edit: running SQL Server 2008 R2
I dont know if you really need a "20" error level, but please try with:
RAISERROR('A', 16, -1) WITH log