Check a Table Available in a Particular Database in Sql Server [duplicate] - sql

This question already has answers here:
Check if table exists and if it doesn't exist, create it in SQL Server 2008
(8 answers)
Closed 2 years ago.
I have two Databases db1 and db2. In those two databases I have a table name "Asset_table" with same columns and same table structure. But In Some client's db2 does not have the "Asset_table".
First I need to check "Asset_table" is available in the client database and if it is available then insert data from db1 to that client DB table.
Here is my query.
IF EXISTS (--here I need to check if the table is available in the db2---)
BEGIN
INSERT INTO db2.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.Asset_table
END
Can any one sugest me an script for this?

Is this what you need?
IF NOT EXISTS(Select 1 from db2.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo' AND TABLE_NAME='Asset_table')
BEGIN
CREATE TABLE db2.dbo.Asset_table(asset_id int, name nvarchar(50),qty int,description nvarchar(150));
END
INSERT INTO db2.dbo.Asset_table( asset_id, name,qty,description)
SELECT asset_id, name,qty,description
FROM db1.dbo.Asset_table;

Related

How can I create a SQL table from another table without copying any values from the old table [duplicate]

This question already has answers here:
How can I create a copy of an Oracle table without copying the data?
(17 answers)
Closed 9 years ago.
How to create table with existing table structure without iterate row by row like this in Oracle? Thanks in Advance.
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
If you are worried about iterating through the table:
CREATE TABLE new_table
AS (SELECT *
FROM (select * old_table where rownum = 1) t
WHERE 1=2
);
I have already read about this.. Hope it gives a Detailed explanation to you..
What we ended up doing in this clients case was to replace the “WHERE 1=2” with a clause that equated the primary key of the table with an impossible value for that key, in this case the ID was being passed in as a GUID (a hexadecimal value) so we use a “WHERE KEY=HEX(00)” and got a low cost unique index lookup instead of a costly full table scan.
http://www.dba-oracle.com/oracle_tips_ault_where_1_equals_2_parallel_.htm
Thanks to Burleson Consulting
I'm not sure on the exact Oracle syntax but in virtually any SQL if you open up the other table using a GUI tool there are options both to generate a create script statement for the table and to backup the table without data.
Either of those will do what you need.

PostgreSQL database - only insert if the record doesn't exist [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cannot SELECT from UPDATE RETURNING clause in postgres
I am using a PostgreSQL database which has a "company" table and a "country" table which each have a uniqueID.
I want a setup where if I try to add a company to the database, it only adds the company to the database with a new unique ID if it doesn't already exist.
insert into company (unique_id, company_name)
select 42, 'New Company Name'
from company
where not exists (select 1 from company where unique_id = 42);
See also here for a more general solution: Insert, on duplicate update in PostgreSQL?

Creating new table with SELECT INTO in SQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SELECT INTO using Oracle
I have came across SQL SELECT INTO statement for creating new table and also dumping old table records into new table in single SQL statement as
SELECT * INTO NEW_TABLE FROM OLD_TABLE;
But when i am trying the above query it is giving error as ORA-00905: missing keyword
Is it possible to do that in ORACLE ?
Thanks.
The syntax for creating a new table is
CREATE TABLE new_table
AS
SELECT *
FROM old_table
This will create a new table named new_table with whatever columns are in old_table and copy the data over. It will not replicate the constraints on the table, it won't replicate the storage attributes, and it won't replicate any triggers defined on the table.
SELECT INTO is used in PL/SQL when you want to fetch data from a table into a local variable in your PL/SQL block.

How do you use SQL to see whether a table exists or not? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL Server: Check if table exists
Oracle: If Table Exists
I'm trying to create a table and insert some values, but before I do I must make sure that the table doesn't already exist. How do you check for this?
You can try simply SELECTing from it and capturing the error, otherwise you will need to write DB specific SQL to query their respective metadata tables and look there.
Assuming SQL Server...Query sysobjects:
select * from sysobjects where xtype='U' and name ='tablename'
In MySQL, you can use the CREATE TABLE IF NOT EXISTS construct and run it before your INSERT query. This will create the table if it doesn't exist and will do nothing if the table is there.
CREATE TABLE IF NOT EXISTS myTable (
....
)
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
http://docs.oracle.com/cd/E17952_01/refman-5.1-en/create-table.html
IF MySQL:
select count(*) from my_tables where table_name='table_1';
If count>0 then ...
This simple query gives you details about the particular table which are created by users in oracle. Try it.
select * from user_tables where table_name = 'tablename';

How to insert table values from one database to another database? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want a query to insert records from one table to another table in a different database if the destination table already exists, it should append the records at the end of the table.
How about this:
USE TargetDatabase
GO
INSERT INTO dbo.TargetTable(field1, field2, field3)
SELECT field1, field2, field3
FROM SourceDatabase.dbo.SourceTable
WHERE (some condition)
How to insert table values from one server/database to another database?
1 Creating Linked Servers {if needs} (SQL server 2008 R2 - 2012)
http://technet.microsoft.com/en-us/library/ff772782.aspx#SSMSProcedure
2 configure the linked server to use Credentials
a) http://technet.microsoft.com/es-es/library/ms189811(v=sql.105).aspx
EXEC sp_addlinkedsrvlogin 'NAMEOFLINKEDSERVER', 'false', null, 'REMOTEUSERNAME', 'REMOTEUSERPASSWORD'
-- CHECK SERVERS
SELECT * FROM sys.servers
-- TEST LINKED SERVERS
EXEC sp_testlinkedserver N'NAMEOFLINKEDSERVER'
INSERT INTO NEW LOCAL TABLE
SELECT * INTO NEWTABLE
FROM [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE
OR
INSERT AS NEW VALUES IN REMOTE TABLE
INSERT
INTO [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE
SELECT *
FROM localTABLE
INSERT AS NEW LOCAL TABLE VALUES
INSERT
INTO localTABLE
SELECT *
FROM [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE
--Code for same server
USE [mydb1]
GO
INSERT INTO dbo.mytable1 (
column1
,column2
,column3
,column4
)
SELECT column1
,column2
,column3
,column4
FROM [mydb2].dbo.mytable2 --WHERE any condition
/*
steps-
1- [mydb1] means our opend connection database
2- mytable1 the table in mydb1 database where we want insert record
3- mydb2 another database.
4- mytable2 is database table where u fetch record from it.
*/
--Code for different server
USE [mydb1]
SELECT *
INTO mytable1
FROM OPENDATASOURCE (
'SQLNCLI'
,'Data Source=XXX.XX.XX.XXX;Initial Catalog=mydb2;User ID=XXX;Password=XXXX'
).[mydb2].dbo.mytable2
/* steps -
1- [mydb1] means our opend connection database
2- mytable1 means create copy table in mydb1 database where we want
insert record
3- XXX.XX.XX.XXX - another server name.
4- mydb2 another server database.
5- write User id and Password of another server credential
6- mytable2 is another server table where u fetch record from it. */
Here's a quick and easy method:
CREATE TABLE database1.employees
AS
SELECT * FROM database2.employees;
You can try
Insert into your_table_in_db1 select * from your_table_in_db2#db2SID
db2SID is the sid of other DB. It will be present in tnsnames.ora file
INSERT
INTO remotedblink.remotedatabase.remoteschema.remotetable
SELECT *
FROM mytable
There is no such thing as "the end of the table" in relational databases.
Just Do it.....
( It will create same table structure as from table as to table with same data )
create table toDatabaseName.toTableName as select * from fromDatabaseName.fromTableName;
Mostly we need this type of query in migration script
INSERT INTO db1.table1(col1,col2,col3,col4)
SELECT col5,col6,col7,col8
FROM db1.table2
In this query column count must be same in both table
If both the tables have the same schema then use this query:
insert into database_name.table_name select * from new_database_name.new_table_name where='condition'
Replace database_name with the name of your 1st database and table_name with the name of table you want to copy from
also replace new_database_name with the name of your other database where you wanna copy and new_table_name is the name of the table.
For SQL Server, you can use tool Import Data from another Database, It's easier to configuration mapping columns.