Try Catch Can't handle alter table

邮差的信 提交于 2019-12-13 08:46:38

问题


Why I this can't handle the alter table?

Begin Try
alter table nyork add [Qtr] varchar(20)
End Try
Begin Catch
Print 'Column already exist'
End Catch'

回答1:


because one of them is a transact sql command (the try catch) and the other is a DDL statement. You'd probably do better off querying to see if the column exists before doing the alter statement. To do this with MSSQL, see How to check if a column exists in SQL Server table




回答2:


You cannot do such a thing. TRY...CATCH can only handle runtime errors. Your script will run as long as the column does not exist but not when it is already there. The name resolution of the objects is done at compile time. Therefore SQL Server will always recognize the missing column before it starts any execution. For that reason, you can't also do such a thing with dynamic SQL.



来源:https://stackoverflow.com/questions/24561547/try-catch-cant-handle-alter-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!