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)
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
if the create statement was inside the try, it may have some use
Use EXISTS
statement, IF
tables exists then only drop table. Otherwise create table directly:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.