使用 Transact-SQL 在 try catch 中使用 tran 进行 alter then update 时出现问题
这是我尝试通过 sqlcmd (SQL Server 2005) 运行的一些 Transact-SQL。
USE PUK;
GO
BEGIN TRANSACTION;
BEGIN TRY
-- - Modify RETRIEVAL_STAT
alter table dbo.RETRIEVAL_STAT add
SOURCE nvarchar(10) NULL,
ACCOUNTNUMBER nvarchar(50) NULL,
PUK nvarchar(20) NULL;
-- transform logic.
update dbo.RETRIEVAL_STAT set
SOURCE = 'XX',
ACCOUNTNUMBER = 'XX',
PUK = 'XX';
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;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
GO
我收到以下错误:
(0 rows affected)
Changed database context to 'PUK'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'SOURCE'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'ACCOUNTNUMBER'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'PUK'.
我猜测这是因为alter语句引入的新列尚未提交,因此更新失败。
我的问题是我该如何让它发挥作用?我希望它作为单个事务运行,如果出现问题,我可以回滚。这很重要,因为我还有更多的更改语句要包含,并且对我无法超越这一点感到有点沮丧。
任何帮助将不胜感激!
抢 :)
Here is some Transact-SQL I am trying to run via sqlcmd (SQL Server 2005).
USE PUK;
GO
BEGIN TRANSACTION;
BEGIN TRY
-- - Modify RETRIEVAL_STAT
alter table dbo.RETRIEVAL_STAT add
SOURCE nvarchar(10) NULL,
ACCOUNTNUMBER nvarchar(50) NULL,
PUK nvarchar(20) NULL;
-- transform logic.
update dbo.RETRIEVAL_STAT set
SOURCE = 'XX',
ACCOUNTNUMBER = 'XX',
PUK = 'XX';
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;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
GO
I am getting the following error:
(0 rows affected)
Changed database context to 'PUK'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'SOURCE'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'ACCOUNTNUMBER'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'PUK'.
I am guessing that this is because the new columns introduced by the alter statement have not yet been committed, so that the update fails.
My question is how do I get this to work then? I want this to run as a single transaction that I can rollback if something goes wrong.. This is important because I have more alter statements to include yet, and am a bit frustrated that I can't get past this point.
Any assistance would be most appreciated!
Rob
:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管我正在写自己的答案 - 所有功劳都归功于@Mikael Eriksson,他建议我需要用 GO 分隔不同的批次 - 这样更改表的代码不会与使用更改表的代码发生冲突。谢谢迈克尔!
Even though I am writing my own answer - all credit goes to @Mikael Eriksson, who suggested that I need to separate different batches with a GO - so that the code that alters the table does not conflict with code that uses the altered table. Thanks Mikael!
SQL Server 在编译代码时检查列。您可以使用动态 sql 来解决该问题。
SQL Server checks the columns when the code is compiled. You could use dynamic sql to get around that problem.