gpt4 book ai didi

sql - 使用 try-catch block 在 Sql-Server 中删除表

转载 作者:行者123 更新时间:2023-12-01 12:22:45 24 4
gpt4 key购买 nike

下面的代码可以工作并且非常精确,但是与其他“标准”方法相比,这样做是否可以?

--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)

最佳答案

最好用

If Object_Id('Tempdb..#temp') Is Not Null
Drop Table #temp
create table #temp

因为你打算最终创建一个#temp Table,它不需要try catch来给出#temp Table不存在的错误消息

如果 create 语句在 try 中,它可能会有一些用处

关于sql - 使用 try-catch block 在 Sql-Server 中删除表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42622818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com