The code below works and is quite precise, but is it OK to do it like this as against the other 'standard' ways ?
下面的代码是有效的,并且非常精确,但是这样做是否与其他“标准”方法相反?
--Drop table if exists
begin try
drop table #temp
end try
begin catch
print 'table does not exist'
end catch
--Create table
create table #temp(a int, b int)
2 个解决方案
#1
4
It is better to use
最好使用
If Object_Id('Tempdb..#temp') Is Not Null
Drop Table #temp
create table #temp
As you intend to create a #temp Table ultimately which does not require try catch to give a error message that #temp Table does not exists
当您打算创建一个#temp表时,它不需要尝试捕获来给出一个错误消息,即#temp表不存在。
if the create statement was inside the try, it may have some use
如果create语句在try中,它可能会有一些使用。
#2
0
Use EXISTS
statement, IF
tables exists then only drop table. Otherwise create table directly:
使用exist语句,如果表存在,则只删除表。否则直接创建表:
BEGIN TRY
IF Object_Id('Tempdb..#temp') Is Not Null
DROP Table #temp
CREATE table #temp
END try
BEGIN CATCH
PRINT 'table does not exist'
END CATCH
#1
4
It is better to use
最好使用
If Object_Id('Tempdb..#temp') Is Not Null
Drop Table #temp
create table #temp
As you intend to create a #temp Table ultimately which does not require try catch to give a error message that #temp Table does not exists
当您打算创建一个#temp表时,它不需要尝试捕获来给出一个错误消息,即#temp表不存在。
if the create statement was inside the try, it may have some use
如果create语句在try中,它可能会有一些使用。
#2
0
Use EXISTS
statement, IF
tables exists then only drop table. Otherwise create table directly:
使用exist语句,如果表存在,则只删除表。否则直接创建表:
BEGIN TRY
IF Object_Id('Tempdb..#temp') Is Not Null
DROP Table #temp
CREATE table #temp
END try
BEGIN CATCH
PRINT 'table does not exist'
END CATCH