Re: HowTo: Append data to a table stored in another database?

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 11/02/04


Date: Mon, 1 Nov 2004 23:40:35 -0600

You can use NOT EXISTS to exclude those rows that already exist in the
target table based on your primary key:

INSERT INTO dbnameB..tableb(PKcol, col1,col2,col3)
SELECT col1,col2,col3
FROM dbnameA..tableA a
WHERE NOT EXISTS
    (
    SELECT *
    FROM dbnameB..tableb b
    WHERE b.PKcol = a.PKcol
    )

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"Lidschi" <Lidschi@discussions.microsoft.com> wrote in message 
news:61D7D680-9DDC-41C8-897A-2BDFEF913626@microsoft.com...
> Hi Hari Prasad,
>
> Problem is that I only want to insert new data from table a to table b, 
> not
> all the data, since the "old" data is already stored in table b.
>
> "Hari Prasad" wrote:
>
>> Hi,
>>
>> Specify column list in your Insert statement.
>>
>> Insert into dbnameB.tableb(col1,col2,col3) select col1,col2,col3 from
>> dbnameA.tableA
>>
>>
>> -- 
>> Thanks
>> Hari
>> SQL Server MVP
>>
>>
>> "Lidschi" <Lidschi@discussions.microsoft.com> wrote in message
>> news:C69E1D99-B692-4E08-B9A3-04947CD3DAA7@microsoft.com...
>> > Hi Newsgroup,
>> >
>> > I have the following scenario:
>> > I have database A with table a and I have database b. I created a 
>> > stored
>> > procedure that transfers table a to database b (now called table b). I
>> > inserted two more columns in table b. Now I want to create a stored
>> > procedure
>> > that appends new data from table a to table b.
>> >
>> > How can I do this? I tried setting up criterias but nothing did work.
>> > Please
>> > help me.
>> >
>> > Thanks and Cheers
>> > Lidschi
>> >
>>
>>
>> 


Relevant Pages

  • Re: HowTo: Append data to a table stored in another database?
    ... SQL Server MVP ... "Lidschi" wrote in message ... > Hi Newsgroup, ... > I have database A with table a and I have database b. ...
    (microsoft.public.sqlserver.programming)
  • Re: Database recovery with data file only
    ... Basically using that command breaks your business logic as there's no ... "Dan Guzman" wrote in message ... >> database Backup as well as ... >>> SQL Server MVP ...
    (microsoft.public.sqlserver.server)
  • Re: How critical is the transaction log file?
    ... Tibor Karaszi, SQL Server MVP ... file when you have only one file (no log file). ... circumstances it is supported to attach only an mdf file. ... > This development database that we're building does not have any 'user' ...
    (microsoft.public.sqlserver.tools)
  • Re: Inserting new rows that use divided values
    ... Mary Bray [SQL Server MVP] ... > it to an existing database. ... > REFERENCES order_cost; ... > correct syntax. ...
    (microsoft.public.sqlserver.programming)
  • Re: "Do Not Allow Null" fields suddenly accept Nulls
    ... SQL Server MVP ... > to "Not Null" are now allowing nulls. ... > and the database saves it just fine. ... > message saying the field does not accept null data. ...
    (microsoft.public.sqlserver.server)