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

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

Related

How to insert value into DB2 from SQL Server using openquery which are not already inserted?

Is there any option to use not exists in openquery from SQL Server and prevent insert of values which already exist in DB2, from table.
I want to insert data from a table located in SQL Server into a table located in an IBM DB2 server, but I want to check if the value already exists or not.
This works for me actually,
if not exists (select * from openquery(puskarkc, 'select * from puskarlib.castatus'))
begin
insert openquery (puskarkc, 'select * from puskarlib.castatus')
select * from castatus
end

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.

Don't know temp tables when use Execute in SQL Server

I have below Query :
if OBJECT_ID('tempdb..#tmpTables') is not null
drop table #tmpTables
Execute('select TABLE_NAME into #tmpTables from '+#dbName+'.INFORMATION_SCHEMA.TABLES')
while (select COUNT(*) from #tmpTables)>0
begin
//here is my statement
end
When I execute this Query, I am getting this error :
Invalid object name '#tmpTables'.
But when the query is changed to this :
if OBJECT_ID('tempdb..#tmpTables') is not null
drop table #tmpTables
select TABLE_NAME into #tmpTables from INFORMATION_SCHEMA.TABLES
while (select COUNT(*) from #tmpTables)>0
begin
//here is my code
end
It works.
How can I do this ?
Table names prefixed with a single number sign (#) are 'Local temporary table' names.
Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced.
Local temporary tables are deleted after the user disconnects from the instance of SQL Server.
And when you create a Local Temporary Table by EXEC() command the creator will not be you and it will disconnected after finishing the statement, And as finishing the connection temp table dropped.
You can use a table variable like this:
DECLARE #tmpTables TABLE(TABLE_NAME nvarchar(max))
insert into #tmpTables
(select TABLE_NAME from INFORMATION_SCHEMA.TABLES)

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

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)

SQL Server query - copy all tables from query into database

I have question about copy tables from a query into other database.
I use this query in SQL Server:
SELECT * FROM information_schema.tables WHERE TABLENAME = '2000'
This query returns tables. And I would like to copy all the returned tables into my other database.
Thank you in advance.
have a look at sp_MSforeachtable and feed your table names into it from information_schema.tables
there is a good example here to get you started sp_MSforeachtable example
Another example that can be modified is here
This could be amended to suit your needs:
Exec sp_MSforeachtable
#command1 = "SELECT COUNT(*) AS [?] FROM ?",
#whereand = "and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'dbo')
and o.name LIKE 'IIN%'"
Replace #command1 with your copy code (something like SELECT * INTO ... and replace #whereand with your filter for your tables or an IN statement if you have a list.