SELECT *
FROM table1 X, table2 C, table3 M, table4 XSDT
WHERE X.CATID= C.CATID
AND M.MEMID= X.MEMID
AND XSDT.SHIPDISC= X.SHIPDISC;
Say I want to run this query on the HOST db (external) and get its data and copy it to a local DB2 database.
Is there a way to do so in DB2?
I know teradata has fastload... but I'm not sure about db2 or how I would go about doing so.
Please keep in mind I do not have dba-level privileges.
Solution to this: http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0002079.htm
If you want to do this with SQL, then you would use something like the following SQL:
create table schema2.table1;
insert into schema2.table1
select * from schema1.table1;
Since you're joining tables, you would have to define the local table in your CREATE TABLE SQL and list the columns in your INSERT as well as your SELECT.
You can do a DB2 backup of the tables, and restore them to your local schema.
You can do a DB2 export of the tables, and use DB2 import to create them on your local schema.
You can use the DB2 db2move utility.
Related
I need a query to import data of a table into a different table and different database. Then using the new table I need to update another table in same database
As you tagged SSMS, I'm assuming you're using SQL Server. This answer may apply to other databases, but you'd need to check.
How to refer to other databases
Assuming your database is on the same server, and the table is in the 'dbo' schema, then you can refer to the other database/table as follows
SELECT *
FROM [db1].[dbo].[t1]
If it's on a different server (but has been set up as a linked server) you can do something similar
SELECT *
FROM [servername].[db1].[dbo].[t1]
Making a copy of the data
To save it as a new table within your current database, you could either create a copy of the table's structure in the current database and use an INSERT INTO command, or instead let SQL Server make the table for you by using the 'INTO' clause in the SELECT e.g.,
SELECT *
INTO [db2].[dbo].[t2] -- Or just simply [t2] if you're running this from that database and are happy with [dbo]
FROM [db1].[dbo].[t1]
The above copies the data from the t1 table in db1 to a new table t2 in db2.
You can then use the data in t2 as you normally would use a table.
If you're running this from within d1, you will always need to refer to the tables in d2 properly (and/or alias them) as below.
UPDATE t3
SET xfield = t2.zfield
FROM [db2].[dbo].[t3] AS t3
INNER JOIN [db2].[dbo].[t2] AS t2 ON t3.id = t2.id
how to copy only data from one database table to another database existing table in sql server query?
Copy one database existing table to another database existing table.
anyone know, Please tell me Sql query
Open image
insert into <target table name>(columns)
select columns
from <source table name>
Try this ...
INSERT INTO DataBase2.dbo.table2
SELECT * FROM DataBase1.dbo.table1
Generate scripts in SQL Server Management Studio from one database's table and run the generated script in another database.
Broadly speaking:
INSERT INTO [databaseName].[schemaName].[table2]
SELECT * FROM [databaseName].[schemaName].[table1]
If you are more specific in your question, I can provide a more detailed answer.
I suppose you want to copy data from all columns keeping the same order in two tables.
If both tables exist in your database use
insert into targetTable
select * from sourceTable
If not, use
select * into targetTable
from sourceTable
In the second case, the targetTable will inherit the datatypes from the sourceTable (not the keys, indexes, etc..)
How can I create alias name to my database in SQL Server 2005?
For example: DB1 and its alias DB2, it's same DB, but with two names.
Or can I do replication, mirroring, syncing or anything other inside server from one DB to another?
You can do replication from one database to another database on the same machine. You can also copy data directly without having to create an alias. For instance if you had a table named Users in DB2 and a Users table in DB1 and they are the same schema you could easily just do
INSERT INTO DB1..Users
select * from DB2..Users
Now, a synonym would allow you to use a table from DB2 as if it was a table in DB1 so for instance if you have a table named Products in DB2 you could do
use DB1
GO
CREATE SYNONYM [dbo].[Products] FOR [DB2].[dbo].[Products]
GO
-- Now the following would give you the same result
select * from DB2..Products
select * from Products
For more information on synonyms see here
I have two database in the same schema. My db is in Postgres. I want to copy data of any table (i.e product) of my 1st db into the same table of the 2nd db.
Is it possible to do so using query?
Can't do it as a single SQL command (at least not without dblink), but the easiest way is probably to just use a pipe between two psql's - use COPY on both ends, one sending the data out in CSV format the other one receiving it.
try
insert into db1.table1 select * from db2.table2
It's not possible in vanilla PostgreSQL installation.
If you are able to install contrib modules, use dblink:
INSERT
INTO product
SELECT *
FROM dblink
(
'dbname=sourcedb',
'
SELECT *
FROM product
'
) AS p (id INT, column1 INT, column2 TEXT, …)
This should be run in the target database.
I want to create a DTS Package to pull data from an Oracle table into a SQL2K
table. How can I insert rows that are not already in the SQL2K table and
update rows that already exist in the SQL2K table?
I guess I could truncate and repopulate the entire table or create a
temporary table and then do updates/inserts from the temp table into the
destination table.
Is there any easier way using DTS?
Thanks,
Rokal
You can do that in a DTS package using two data driven query tasks: one for the inserts and one for the updates. The data driven query tasks are a bit of a pain to use, but they work. I've also done this (a "merge") in sql server 2000 with an AS/400 database using a dynamic t-sql. You'd write a t-sql script that outputs psql and runs it againt a linked server to the Oracle database.
UPDATE:
A DTS "data driven query task" will let you insert|update data from the sql server connection in DTS to an oracle server connection in DTS w/o a temp table or a linked server.
Update2; here's some more info on what I mean:
http://www.databasejournal.com/features/mssql/article.php/3315951
http://msdn.microsoft.com/en-us/library/aa933507(SQL.80).aspx
Are you keeping the same primary key values?
If you are you have a number of options, some versions of SQL support the MERGE statement which will update or insert just like you require.
Or you can write your own.
Something along the lines of loading all the rows into a staging table in your SQL database and row by row checking for the existence of your primary key in your main SQL table. If the key exists update the row and if not insert it.
Yes, the primary key values in the source and destination will match.
I was hoping to accomplish this task without the use of a temporary (staging) table.
Also, I am using sql server 2000 so the MERGE statement is not available.
Try:
DELETE FROM dbo.WhateverTable WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
It might be pretty slow, use join instead:
Delete a
from firstTable a join secondTable b on a.id = b.id
There's no way with TSQL to do a INSERT or UPDATE in the same statement, but you could very easily do it in two statements (as you mentioned above).
Statement 1:
DELETE FROM dbo.WhateverTable
WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
Statement 2:
INSERT INTO dbo.WhateverTable
SELECT * FROM MySource
Also, is there any reason why you don't want to use a temp table?