這裏是我試圖通過sqlcmd(SQL Server 2005)運行的一些Transact-SQL。問題與改變,然後嘗試使用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語句引入了新列尚未提交,以便更新失敗。
我的問題是我如何得到這個工作呢?我希望這可以作爲單個事務運行,如果出現問題,我可以回滾。這很重要,因爲我還有更多的alter語句可供使用,而且我有點沮喪,我無法克服這一點。
任何援助將不勝感激!當代碼被編譯
羅布 :)
我在這裏寫了這篇文章:[在批量Transact-SQL中分離DDL和DML](http://robertmarkbramprogrammer.blogspot.com/2011/ 09 /分離-DDL-和DML-在-的Transact-sql.html)。 –