How to drop a table in SQL Server 2008 only if exists - sql

Our company is migrating one of its products from SQL Server 2005 to 2008, and in the logs I've noticed excess of errors about deleting tables if it does not exist. In SQL Server 2005 we had this to delete tables
IF OBJECT_ID('dbo.Units', 'U') IS NOT NULL
DROP TABLE dbo.Units
which doesn't seem to work anymore.
What would be the proper way of deleting a table only if it exists in SQL Server 2008?

This should do it!
IF EXISTS
(SELECT
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TableToDrop')
DROP TABLE TableToDrop

This code works on my SQL Server 2012:
IF OBJECT_ID('t', 'U') IS NOT NULL DROP TABLE t
CREATE TABLE t(id int)
Maybe your name is wrong (e.g. case sensitive collation on new installation?). What kind of error you are getting?
Note that in SQL Server 2016 you can use DROP IF EXISTS:
DROP TABLE IF EXISTS t
CREATE TABLE t(id int)

Related

How to copy a table from a linked server into the main database only on the condition that the table does not already exist?

I am using SQL Server 2012 and I have the following T-SQL query:
USE MyDatabase
INSERT INTO [Table1]
SELECT *
FROM [xxx.xx.x.xx].[xxx].[dbo].[Table1]
I would like to modify this query so that it copies Table1 into MyDatabase only if that table does not already exist in MyDatabase.
I've had a look here but I can't figure out how to migrate the solutions into my problem: Check if table exists in SQL Server
How can I achieve this?
This should do it:
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Table1'))
BEGIN
INSERT INTO [Table1]
SELECT *
FROM [xxx.xx.x.xx].[xxx].[dbo].[Table1]
END
More details and approaches you can read here:
Check if table exists in SQL Server

Drop and create table in one command

do you know why below code is wrong on ms sql server?
DROP TABLE [IF EXISTS] database.table1,
create table table1 (...)
That's SQL Server 2016 syntax.
For earlier versions, you can use the EXISTS function to check whether your table exists in the sys.tables list and drop it if it does. Then create a new table.
Somewhat like this:
IF EXISTS (SELECT * FROM sys.tables WHERE name = 'table1' AND type = 'U') DROP TABLE table1;
CREATE TABLE table1
(
...
...
);
This syntax is only valid on SQL Server 2016, which is not released yet.
Are you sure that you are using that version?
Otherwise you could use the IF Exists that #pradeep-kumar suggests.

drop table #temp vs drop myTable if it's not null

So far i was using
IF OBJECT_ID(''tempdb..#tempTable'') IS NOT NULL -- dropping the temp table
DROP TABLE #tempTable
Is there a way in which I could use the same statement for a table which is not a temp one?
Tried like this and it didn't work:
IF OBJECT_ID(''myOwnDb.dbo.myTable'') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Strings in MS SQL server should be enclosed in single quotes.
So neither OBJECT_ID(''myOwnDb.dbo.myTable'') nor OBJECT_ID("myOwnDb.dbo.myTable") will work.
But OBJECT_ID('myOwnDb.dbo.myTable') will work perfectly.
In addition to what other users have suggested wrt Object_ID which is fine, you can explore below method to detect if table exist or not using INFORMATION_SCHEMA
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'Your Table Name')
BEGIN
Drop table <tablename>
END
The reason it did not work is because you have the extra quotes instead of single quotes.
i.e. You should be doing this:
IF OBJECT_ID('myOwnDb.dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
However, note that when you actually drop the table. You aren't even referencing the database. So you can just do:
IF OBJECT_ID('dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Unless you are calling this command from another database. Then you will need to include the database name in the DROP TABLE command as well.

How to get ID of a newly created SQL Server database

Say, I run the following statement to create a new SQL Server database:
CREATE DATABASE [testdb1] COLLATE SQL_Latin1_General_CP1_CI_AS;
How do I get it's ID?
SELECT database_id FROM sys.databases WHERE name = N'testdb1';
select DB_ID (N'testdb1')
Source

SQL Server 2012 temp table OBJECT_ID issue

We have an issue upgrading to SQL Server 2012. I am using the following script to create temporary tables that used to work fine on SQL Server 2008 R2 but now it is generating an error with 2012:
if (OBJECT_ID( 'tempdb..#idstable') > 0)
truncate table #idstable
else
create table #idstable (id int not null)
the error thrown is
There is already an object named '#idstable' in the database.
This is obviously not thrown the first time I use the script (in the same transaction).
Any idea? Thank you!
In SQL Server 2012, #temp tables are created with a negative object_id, so your script won't work as is. The safest way is:
IF OBJECT_ID( 'tempdb..#idstable') IS NOT NULL
(I blogged about this here, and knew it would catch someone.)
Though your script is bound for failure anyway, if it is part of a single batch. The parser will not let you try and create the same #temp table twice.
Try this:
IF OBJECT_ID (N'tempdb..#idstable', N'U') IS NOT NULL
truncate table #idstable
else
create table #idstable (id int not null)
My dears,
This issue is due to the truncate statement. Truncate is used to delete all records preserving the table. Use drop table instead of truncate table and this will works fine ;-)