When I create a temporary table, I usually make sure that if they exist, I drop them.
IF OBJECT_ID(N'tempdb..#tempTable') IS NOT NULL
DROP TABLE #tempTable
I recently realized that the following method does the same:
DROP TABLE IF EXISTS #tempTable
Is there one way better than the other ?
Explanation
They do the same thing just different syntax, and the later method in your question is newer. The IF EXISTS clause has been supported with DROP TABLE since SQL Server 2016 13.x up through the current version as of writting this, SQL Server 2019 (15.x).
Documentation
The IF EXISTS functionality is documented in the arguments section here: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-table-transact-sql?view=sql-server-ver15#arguments
Related
I am using SQL Server 2012. When I want to create a temporary table named #TBL1, or rerun my code, I get this error:
There is already an object named '#TBL1' in the database
So I added this code to my query:
IF OBJECT_ID('dbo.#TBL1', 'U') IS NOT NULL
DROP TABLE dbo.#TBL;
But it shows the same error message
Please give me a clue is to what is wrong
You need to make sure OBJECT_ID looks in the right place. Temporary tables live in tempdb:
IF OBJECT_ID('tempdb.dbo.#TBL1', 'U') IS NOT NULL
BEGIN
DROP TABLE dbo.#TBL;
END
Also seems there is a typo (#TBL1 vs. #TBL).
And while I am normally am a big fan of schema prefixes, for #temp tables it’s not necessary and actually hampers readability IMHO.
scrp.Options.ScriptForAlter -- I suppose it scripts an ALTER TABLE instead of a CREATE TABLE
scrp.Options.ScriptForCreateDrop -- I'd guess it scripts a DROP TABLE and only afterwards a CREATE TABLE ?
My DB is fairly large and I'm still using Management Studio for script generation, so I'd like to know if someone else already found out the meaning of those options.
I am trying to get this command that is on auto run to have an automatic delete incase I have to re-run the application. I am using visual studio and sql server 2012.
Here is what I have. The Create table works but its the IF EXISTS that I am having trouble with.
IF EXISTS (DROP TABLE ST_BANLIST)
CREATE TABLE ST_BANLIST
(BAN VARCHAR (9).
CALL_ACTIVITY CHAR(1).
BAN_STATUS CHAR(1))
Thanks for any help
Your syntax was incorrect:
IF OBJECT_ID('dbo.ST_BANLIST', 'U') IS NOT NULL
DROP TABLE dbo.ST_BANLIST
SQL Server 2016 makes this a lot easier (what took so long Microsoft?):
DROP TABLE IF EXISTS dbo.ST_BANLIST
if exists(select * from sys.objects where name ='MytableName' and type='U')
Drop table dbo.MytableName
This question already has answers here:
Oracle: If Table Exists
(16 answers)
Closed 8 years ago.
I have to use an SQL statement to drop a table, it will crash if the table doesn't exist. Is it possible to use IF statement to drop the table
s.executeUpdate("DROP TABLE employee");
Oracle does not support a construct like drop table if exists my_table, which is apparently legal syntax in MySQL (and possibly other RDBMSs).
In a .SQL script, where you're running DDL to DROP and/or CREATE various objects, the Oracle standard is to drop the object, and ignore the error in cases where the object does not exist. If you wish, you can write code to check if the object exists (see DBA_OBJECTS view) to only drop if it exists.
from the s.executeUpdate, I gather that you're doing this in Java? If it was me, I'd just do the drop and ignore any not exists error.
assuming you have the right permissions, you can do something like the below
declare var_count int;
select count(*) INTO var_count
from all_tables where OWNER = [schema] and table_name = "EMPLOYEE";
if var_count > 0 then
begin
drop table employee;
end
adapt accordingly if you are doing this in front-end code instead of a pl/sql procedure.
Traditionally in pl/sql you can specify an exception section in the block, and catch the exception if the table is not there to drop.
See Oracle errors handling
This will resolve your problem, when the table doesn't exist no error will be thrown.
BEGIN
DROP TABLE employee;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
I would use the following code:
s.executeUpdate("DROP TABLE IF EXISTS employee")
But depending on your verions, you could also use this:
IF OBJECT_ID('dbo.employee', 'U') IS NOT NULL
DROP TABLE dbo.employee
Answer taken from: here, also this seems like it might be a duplicate of the same post?
I would highly, highly recommend reading the SQL Documentation, and trying to do a bit more research before posting, but sense I see you are new, be sure to read the rules of posting here on StackOverFlow.
i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.
How do i do this in SQL server?
in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.
Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.
You can check for the existence of a specific Trigger like so.
IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN
--Add your Trigger create code here
END
I find this to be a more compact SQL Server equivalent to MySQL's DROP TRIGGER IF EXISTS syntax:
IF OBJECT_ID('XXXX', 'TR') IS NOT NULL
DROP TRIGGER XXXX
I'd use something like:
IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]
replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).
Alternatively, you could just use
ALTER TRIGGER dbo.xxx [etc]
Since version 2016 this syntax is also supported by Microsoft SQL Server:
DROP TRIGGER IF EXISTS trigger_name