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.
Related
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
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.
This is probably a multipart question:
When I issue the statment
IF OBJECT_ID('temp..#tablename) IS NOT NULL --drop the table here
it does not drop the temp table. When I go look at the temp db, the name is something totally different...
#tablename___________________________________________00000001069F
Obviously, the drop statement wont work with this. How can I make sure that the temp table gets dropped with the above statement.
Also, if I use the "USE dbName" before the create temp table statement, does the temp table still get created in tempdb or the the dbName database? Is the default always tempDb?
Thanks,
RV.
use should use
OBJECT_ID('tempDB..#tablename')
not
OBJECT_ID('temp..#tablename')
does the temp table still get created in tempdb or the the dbName database? Is the default always tempDb?
YES
Temp Tables always gets created in TEMPDB..
Reason why you are seeing name like _____00000001069F is due to the fact that Temp tables are session specific and SQL takes care of assigning names to them,so that names won't conflict,even when they are used in parallel sessions with same names
Look out this answer on DBA.SE for more info,specifically look out Temp Tables section :
What's the difference between a temp table and table variable in SQL Server?
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.