- 编辑 -
我已回答了我的问题,但解决方法是
更多的解决方法。如果有人可以提供
真正的回答我会给他们一个解决方案。
----
我一直在寻找一种解决方案,以防止MS SQL在存储过程中使用print语句时生成警告,但没有成功。
我正在从ETL应用程序执行此过程,我需要退出存储过程而不会出现错误或警告。
FYI - ETL过程执行存储过程时不需要print语句。这些是在手动执行的时候。
我已尝试过SET ANSI WARNINGS OFF和Try / Catch,但我仍然在输出上收到警告。
这是存储过程:
CREATE PROCEDURE [dbo].[spBackupSomeTables]
AS
BEGIN TRY
SET ANSI_WARNINGS OFF
PRINT 'Copying SomeTable1 to Z_Backup_SomeTable1' + CHAR(10)
SET ANSI_WARNINGS ON
TRUNCATE TABLE Z_Backup_SomeTable1
INSERT INTO Z_Backup_SomeTable1
SELECT *
FROM SomeTable1
SET ANSI_WARNINGS OFF
PRINT 'Copying SomeTable2 to Z_Backup_SomeTable2' + CHAR(10)
SET ANSI_WARNINGS ON
TRUNCATE TABLE Z_Backup_SomeTable2
INSERT INTO Z_Backup_SomeTable2
SELECT *
FROM SomeTable2
SET ANSI_WARNINGS OFF
PRINT 'All Done' + CHAR(10)
SET ANSI_WARNINGS ON
END TRY
BEGIN CATCH
DECLARE
@ErrorNumber VARCHAR(1000),
@ErrorSeverity VARCHAR(1000),
@ErrorState VARCHAR(1000),
@ErrorProcedure VARCHAR(1000),
@ErrorLine VARCHAR(1000),
@ErrorMessage VARCHAR(1000)
SET @ErrorNumber = (SELECT ERROR_NUMBER() AS ErrorNumber)
SET @ErrorSeverity = (SELECT ERROR_SEVERITY() AS ErrorSeverity)
SET @ErrorState = (SELECT ERROR_STATE() AS ErrorState)
SET @ErrorProcedure = (SELECT ERROR_PROCEDURE() AS ErrorProcedure)
SET @ErrorLine = (SELECT ERROR_LINE() AS ErrorLine)
SET @ErrorMessage = (SELECT ERROR_MESSAGE() AS ErrorMessage)
PRINT 'ErrorNumber: ' + @ErrorNumber + CHAR(10)
PRINT 'ErrorSeverity: ' + @ErrorSeverity + CHAR(10)
PRINT 'ErrorState: ' + @ErrorState + CHAR(10)
PRINT 'ErrorProcedure: ' + @ErrorProcedure + CHAR(10)
PRINT 'ErrorLine: ' + @ErrorLine + CHAR(10)
PRINT 'ErrorMessage: ' + @ErrorMessage + CHAR(10)
END CATCH
GO
这是执行结果:
Warnings: ---> W (1): Copying SomeTable1 to Z_Backup_SomeTable1 W (2): Copying SomeTable2 to Z_Backup_SomeTable2 W (3): All Done
答案 0 :(得分:1)
在评论中使用建议/链接,这是我提出的解决方案..
CREATE PROCEDURE [dbo].[spBackupSomeTables] @Debug tinyint =1
AS
BEGIN
IF @Debug IS NULL
SET @Debug = 1;
IF @Debug > 0
RAISERROR( 'Copying SomeTable1 to Z_Backup_SomeTable1', 0, 1 ) WITH NOWAIT
TRUNCATE TABLE Z_Backup_SomeTable1
INSERT INTO Z_Backup_SomeTable1
SELECT *
FROM SomeTable1
IF @Debug > 0
RAISERROR( 'Copying SomeTable2 to Z_Backup_SomeTable2', 0, 1 ) WITH NOWAIT
TRUNCATE TABLE Z_Backup_SomeTable2
INSERT INTO Z_Backup_SomeTable2
SELECT *
FROM SomeTable2
IF @Debug > 0
RAISERROR( 'All Done', 0, 1 ) WITH NOWAIT
END
GO
@Debug参数默认值为1,这样人们就不必担心传递任何参数以查看打印的状态消息。 ETL作业将简单地为该参数传递值0以禁止打印语句,从而允许作业完成而不会发出任何(错误)警告。