我有一个正确执行的存储过程。现在,我尝试添加TRY CATCH
T-SQL 语句。
添加后
BEGIN TRY
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
SET XACT_ABORT ON
GO
/****** add new column Accountid to Metainformation table ******/
IF NOT EXISTS (SELECT * FROM SYSCOLUMNS WHERE NAME = 'Accountid' AND ID = OBJECT_ID('[GSF].[dbo].[Metainformation]'))
BEGIN
ALTER TABLE [GSF].[dbo].[Metainformation] ADD Accountid int
END
GO
IF EXISTS (SELECT * FROM SYSCOLUMNS WHERE NAME = 'Accountid' AND ID = OBJECT_ID('[GSF].[dbo].[Metainformation]'))
BEGIN
UPDATE [GSF].[dbo].[Metainformation]
SET MP.Accountid = AD.Accountid
FROM [GSF].[dbo].[Metainformation] MI, [GSF].[dbo].[AccountDetails] AD
WHERE MI.DetailID= AD.DetailID
END
GO
我的GO
语句出现错误,在 GO 附近显示错误说语法不正确。
任何指针或替代使用?
更新代码:
USE GSF
GO
/****** add new column AccountId to MetaInformation table ******/
IF NOT EXISTS (SELECT * FROM **SYS.COLUMNS** WHERE NAME = 'AccountId' AND ID = OBJECT_ID('[GSF].[dbo].[MetaInformation]'))
ALTER TABLE [GSF].[dbo].[MetaInformation] ADD AccountId uniqueidentifier
BEGIN TRANSACTION;
BEGIN TRY
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
/****** If the column AccountId exists, Update all the AccountId values in MetaInformation from AccountDetails ******/
IF EXISTS (SELECT * FROM **SYS.COLUMNS** WHERE NAME = 'AccountId' AND ID = OBJECT_ID('[GSF].[dbo].[MetaInformation]'))
BEGIN
UPDATE MP
SET MP.AccountId = AD.AccountId
FROM [GSF].[dbo].[MetaInformation] MP
**INNER JOIN**
[GSF].[dbo].[AccountDetails] AD
ON MP.AllocationDetailId = AD.AllocationDetailId
END
/****** Drop AccountId column from AccountDetails ******/
IF EXISTS (SELECT * FROM SYS.COLUMNS WHERE NAME = 'AccountId' AND ID = OBJECT_ID('[GSF].[dbo].[AccountDetails]'))
ALTER TABLE [GSF].[dbo].[AccountDetails] DROP COLUMN AccountId
/****** add two new PStage values to [PStageToCategory] table ******/
INSERT INTO [GSF].[dbo].[PStageToCategory]
(PStage, PStageToCategoryName)
VALUES(19,1)
INSERT INTO [GSF].[dbo].[PStageToCategory]
(PStage, PStageToCategoryName)
VALUES(21,1)
/****** Drop and create new ViewName view to remove reference of AccountId ******/
USE GSF
IF EXISTS ( SELECT * FROM sys.views WHERE name = 'ViewName')
DROP VIEW ViewName
DECLARE @sql_view NVARCHAR(MAX);
SET @sql_view = '<VIEW DEFINITION>';
EXEC sp_executesql @sql_view;
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
我正在使用这个脚本来更改生产中的架构。所以,我包括 try catch 只是为了知道错误和事务,如果在运行中脚本中有错误(只有一个它将运行),则回滚。你觉得有道理吗?
另外,我根据您的评论对我的脚本进行了更改。你怎么看 ?