-2

Example from pic.

http://pic.free.in.th/id/d56133ad2238308e979aa3dbea94436e

i want to insert data into Database A table A and Database B Table B in same time but some column from table A to Table B

EX.

table A have column ID,Name,Address,tel.I want just insert data ID,Name into table B. (insert data to table B automaticly when insert data to table A ) if you have any idea please let me know.

Chatdao
  • 3
  • 5

2 Answers2

0

You can do an insert with a SELECT.

INSERT INTO DataBaseB.dbo.TableB (ID, Name)
SELECT ID, Name from DatabaseA.dbo.TableA

See here for more details:

http://www.w3schools.com/sql/sql_insert_into_select.asp

This assumes both databases are on the same server, if not, you could always export/import the data?

Daniel Dawes
  • 975
  • 5
  • 16
  • your answer it ok but i want to insert data to table B automaticly when insert data to table A .You have any idea? – Chatdao Oct 17 '13 at 08:35
  • Yes, you need to use a trigger with AFTER INSERT and then code to insert into TableB, google the use and look here: http://technet.microsoft.com/en-us/library/ms189799.aspx – Daniel Dawes Oct 17 '13 at 08:42
  • I create trigger like this ALTER TRIGGER [dbo].[ticky2] ON [dbo].[data] AFTER INSERT AS INSERT INTO [testDatabase].[dbo].[Sheet1$] ([ID] ,[Name] ,[salary] ,[date]) SELECT ID, Name,salary,date from deconc.dbo.data after i execute Sheet1$ => (10 row(s) affected) ----i just want one row not all row from table data. data => (1 row(s) affected). please Recommend. – Chatdao Oct 18 '13 at 02:48
0
ALTER TRIGGER [dbo].[ticky2] 
   ON  [dbo].[data]
   AFTER INSERT
AS 
INSERT INTO [testDatabase].[dbo].[pool]
           ([ID]
           ,[Name]
           ,[salary]
           ,[date])
     SELECT ID, Name,salary,date from deconc.dbo.data
     where ID not in 
     (

     select ID
     from [testDatabase].[dbo].[pool]

     )
Chatdao
  • 3
  • 5
  • For this code up there. I don't sure about performence.When transection Increase more and more. – Chatdao Oct 18 '13 at 06:32
  • Ah yes, you don't want to do that really, you can access the 'row' that was inserted from the trigger, you shouldn't need to select from the table. The key is that bit about selecting from Inserted. See here: http://stackoverflow.com/questions/1355921/sql-server-2008-help-writing-simple-insert-trigger/1355953#1355953 – Daniel Dawes Oct 18 '13 at 08:25