简单的代码。非常奇怪的行为,三次检查。@filePath和@path是NVARCHAR(260),@exists是INT。dbo.WriteToFile将文件写入磁盘,在本例中为空文件。
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时,它会进入if块。文件是按预期创建的。证实了这一点。但是RAISERROR in,if块是而不是调用的。相反,只调用if块之外的RAISERROR。
但是,如果我将第一个RAISERROR ('A')替换为PRINT 'blabla',则按预期调用print语句。
如果我将@exists = 0替换为1=1,而将保持第一个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服务器配置不正确?
如果您感兴趣,我也会发布WriteToFile过程,也许会有所帮助,我已经从网上获得了这个过程,但是它工作得很好,但可能会产生RAISERROR行为:
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编辑:运行Server 2008 R2
发布于 2012-08-25 07:11:10
我不知道您是否真的需要"20“错误级别,但请尝试:
RAISERROR('A', 16, -1) WITH loghttps://stackoverflow.com/questions/12096393
复制相似问题