Using SQL Server DTS Package to Conditionally Insert / Update Rows in Destination Table - sql

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?

Related

how to copy only data from one database table to another database existing table in sql server

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 to trace SQL statements for a specific table?

I'm currently working with DB2 v10.5 and I need to log all SQL Statements that occurr on a specific table (A). For instance, if an INSERT occur on table A, I need to "grab" that SQL Statement and log it into another table (A_LOGGER).
The solution I've reached was to create a TRIGGER (for each CRUD operation) over table A that looks into the table SYSIBMADM.SNAPDYN_SQL and try to save the last executed statement on table A.
Example for the INSERT Statement:
CREATE OR REPLACE TRIGGER OPERATIONS_INSERT_TRIGGER
AFTER INSERT ON REPLDEMO.OPERATIONS
REFERENCING NEW AS OBJ
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO REPLDEMO.OPERATIONS_LOGGER (LAST_SQL_STATEMENT)
SELECT STMT_TEXT FROM SYSIBMADM.SNAPDYN_SQL
WHERE 1=1
AND STMT_TEXT LIKE 'INSERT INTO REPLDEMO.OPERATIONS (%'
AND STMT_TEXT NOT LIKE '%?%';
END%
But looking at table SYSIBMADM.SNAPDYN_SQL is not the best solution because you cannot guarantee that you'll get the truly last SQL Statement executed on table A. Moreover, if there's a massive number of sql statements executed over table A in a very short period the TRIGGER will replicate many of the statements already saved on A_LOGGER.
So, my question is: Is there an effective and secure way to get the last SQL Statement executed over a table?
Thanks.

Copy tables from one server to another in DB2

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.

Using OUTPUT with joined tables

Why doesn't the following work?
INSERT INTO dbo.Pending_Break
(Pending_Pod_ID, Break_Date_Time, Break_Length, Booked_Length)
OUTPUT INSERTED.Pending_BH_ID -- This is the inserted identity
, INSERTED.Pending_Pod_ID
, INSERTED.Break_Name
, INSERTED.Break_Date_Time
, pb.PENDING_BH_ID -- complains on this one
INTO #InsertedPendingBreaks
SELECT ippod.Pending_Pod_ID,
pb.Break_Date_Time
pb.break_length,
0
FROM PendingBreak pb
JOIN InsertedPod ippod ON ...
Can I not use anything other than Inserted or Deleted in the OUTPUT clause?
Can I not use anything other than Inserted or Deleted in the OUTPUT
clause?
No you can't. At least not with an insert. In SQL Server 2008 you can convert your insert to a merge statement instead and there you can use values from the source table in the output clause.
Have a look at this question how to do that in SQL Server 2008. Using merge..output to get mapping between source.id and target.id
The inserted and deleted tables are available only in DML triggers. I'm not sure if you just pulled a code snippet out of a trigger, but if that is a standalone batch then it won't work.
Also, there is no updated table. An update is a delete and then an insert for this. deleted contains the old data and inserted contains the new data on an UPDATE.

SQL Server 2005, bulk UPDATE or INSERT

I'm looking for a solution to perform Insert, on duplicate key Update like operation in SQL Server 2005. This operation might Insert or Update large number of entries. SQL Server 2008 has a neat operation MERGE which would do it perfectly, the problem is we're stuck with SQL Server 2005.
I've looked into standard solutions, but all of them are not good, because they assume, that only one entry is updated/inserted.
Does anyone know a way to replicate MERGE behavior in older versions of SQL Server?
Alex Kuznetsov's blog contains a suggestion using the OUTPUT clause of an UPDATE statement. To paraphrase the example from that blog entry (untested):
DECLARE #updated_ids table(id int)
UPDATE table
SET ...
OUTPUT inserted.id INTO #updated_ids
FROM table INNER JOIN data-to-insert ON table.id = data-to-insert.id
INSERT INTO table
SELECT ...
FROM data-to-insert
WHERE id NOT IN (SELECT id FROM #updated_ids)