0

What the title says, I want to insert data from a database to another database. The first database "DB1" is the source and the second database "DB2" is the target.

The relevant tables are defined as follows:

DB1 => tbl_Target

==================================
Id | Date | Name | Age | Num_Aucts
==================================

DB2 => tbl_Source

======================
Name | Age | Num_Aucts
======================

Well, tbl_Source contains 40 rows of data. I need to transfer these rows into tbl_Target. But how you can see tbl_Target has two additional columns Idand Date. Id will set automatically. The important column is Date. In this column I want to set the currently date. In this case from today. How can I define this in a trigger frunction in SQL Server with T-SQL?

I have begun in this direction:

USE DB1
GO

CREATE TRIGGER trg_Insert_tblSource ON tbl_Source
FOR INSERT AS
BEGIN
    INSERT INTO DB2.dbo.tbl_Target ([Date], [Name], [Age], [Num_Aucts])
        SELECT ??? // How to get the current date?

Can anyone help me? Do I need a stored procedure?

2
  • you can use GetDate() to get current date of server Commented Apr 22, 2016 at 9:34
  • @syedmohsin But where I have to define it? Commented Apr 22, 2016 at 9:34

2 Answers 2

2

I don't know if I fully understood your question. But if you just want to transfer the data from one table to another, you don't need a trigger. You could achieve this with a simple INSERT...SELECT:

INSERT INTO DB2.dbo.tbl_Target (
   [Date],
   [Name],
   [Age],
   [Num_Aucts]
)
SELECT GETDATE(), [Name], [Age], [Num_Aucts]
FROM yourDB.dbo.tbl_Source

If you want to use a trigger, write the following in the BEGIN block:

INSERT INTO DB2.dbo.tbl_Target (
   [Date],
   [Name],
   [Age],
   [Num_Aucts]
)
SELECT GETDATE(), [Name], [Age], [Num_Aucts]
FROM inserted

The trigger gets fired each time you insert data into tbl_Source. But the existing data doesn't get inserted into tbl_Target with your trigger.

Sign up to request clarification or add additional context in comments.

5 Comments

I know but how can I define it in my case?
I've tried your solution, but it doesn't insert the data in DB2 table.
I see I have created the trigger. Well, how can I use this trigger to execute the code?
As soon as you insert data into tbl_Source, your trigger fires and inserts the data into tbl_Target. I think you should first learn about triggers and how they are used.
Ok.. in my target table is the inserted data. But it's curious. The data is more than one time inserted.
0

you can use GetDate() to get current date of server

USE DB1
GO

CREATE TRIGGER trg_Insert_tblSource ON tbl_Source
FOR INSERT AS
BEGIN
    INSERT INTO DB2.dbo.tbl_Target (
       [Date],
       [Name],
       [Age],
       [Num_Aucts]
    )
    SELECT GETDATE(), [Name], [Age], [Num_Aucts]
    FROM inserted;
END
GO

1 Comment

I've tried also your post, but the table in DB2 doesn't filled with the data from DB1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.