How can I insert into two tables in different SQL Server 2008 databases - sql-server

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.

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?

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]
)

Related

T-SQL OUTPUT clause to update a temp table

I have a utility script that is used to insert data into tables in my database. The script has a number of temp table in it that stores the new data to be inserted and a lot of it is related.
So, for example I have tables like so
DECLARE #Table1 TABLE
(
Table1ID INT
Table1Description VARCHAR(50)
Table1Code VARCHAR(5)
)
DECLARE #Table2 TABLE
(
Table2ID INT
Table2Description VARCHAR(50)
Table2Code VARCHAR(5)
)
DECLARE #Relationships TABLE
(
Table1Code VARCHAR(5)
Table2Code VARCHAR(5)
)
So the script populates the data in #Table1 and #Table2, but doesn't populate the ID fields. Once the data has been MERGEd into the database tables, I update the Table1ID and Table2ID fields in a separate statement as they are auto incrementing fields. Then when I use the #Relationships table to populate the database table, I can join to #Table1 and #Table2 to get the actual ID values.
I'm updating the script and I'm wondering if I can MERGE the data from #Table1/#Table2 into the database and update the ID fields in the temp table as part of the MERGE statement using the OUTPUT clause all in one statement?
I think the answer is no as I can't find anything mentioning updating an existing table with the OUTPUT clause, only inserting into a table.
I am still able to do what I need to do, so I'm not after alternatives. I just wondering if it is possible using the OUTPUT Clause
Thanks in advance

How to insert data from one server table to another server table using a trigger on first server table

I need to directly insert the data from one server table to another server table with an after insert trigger on server 1 table. I have used the following code to do the work. Tables on both the servers have identical columns, table name and schema.
CREATE TRIGGER trigger_name ON [server1].db.schema.table1
FOR INSERT
AS
INSERT INTO [server_2].db2.[schema].[table1](
PLANTID
,PLANTNAME
,PLANTLOCATION
,COMPANYNAME
,DISPLAY_STATUS
,VERSION
,DEPARTMENT
,PURPOSE
,PERFORMEDBY
)
SELECT
PLANTID
,PLANTNAME
,PLANTLOCATION
,COMPANYNAME
,DISPLAY_STATUS
,VERSION
,DEPARTMENT
,PURPOSE
,PERFORMEDBY
FROM INSERTED
order by plmstrauid
However, I get the following error. Can anyone please help me with this?
Error:
The object name '[server1].db.schema.table1' contains more than the
maximum number of prefixes. The maximum is 2.

In SQL Server / Liquibase, does INSERT query create a table, automatically without CREATE schema, if it does not exist?

I am using Liquibase for managing SQL Server scripts (create, update, delete, alters etc.).
My requirement was to create a backup table (say old_table_a) before I could drop two columns (column_1, column_2) from the original table (table_a).
The new backup table does not need a primary key, so it will just have two columns as shown below
old_table_a
column_1 (from original table_a)
column_2 (from original table_a)
If I just write INSERT query as shown below, without having a CREATE TABLE old_table_a
INSERT INTO old_table_a (column_1, column_2)
SELECT column_1, column_2
FROM table_a
I had read this somewhere on some blog, but cannot find this.
Please provide some information if this is possible.
Otherwise I know that the usual way to do this is to create the new backup table and then populate the new table with values from the original.
This can be done with SELECT * INTO:
SELECT * INTO [NEWTABLE] FROM [OLDTABLE]
INSERT tableName1 (ColumName)
(select (ColumName ) from TableName2)

Can I grab the inserted IDs when doing multiple inserts?

In my head this sounds improbable, but I'd like to know if I can do it:
INSERT INTO MyTable (Name)
VALUES ('First'),
('Second'),
('Third'),
('Fourth'),
('Fifth');
SELECT INSERTED Name, ID FROM TheAboveQuery
Where ID is an auto-indexed column?
Just to clarify, I want to select ONLY the newly inserted rows.
Starting with SQL Server 2008 you can use OUTPUT clause with INSERT statement
DECLARE #T TABLE (ID INT, Name NVARCHAR(100))
INSERT INTO MyTable (Name)
OUTPUT INSERTED.ID, INSERTED.Name INTO #T
VALUES
('First'),
('Second'),
('Third'),
('Fourth'),
('Fifth');
SELECT Name, ID FROM #T;
UPDATE: if table have no triggers
INSERT INTO MyTable (Name)
OUTPUT INSERTED.ID, INSERTED.Name
VALUES
('First'),
('Second'),
('Third'),
('Fourth'),
('Fifth');
Sure, you can use an IDENTITY property on your ID field, and create the CLUSTERED INDEX on it
ONLINE DEMO
create table MyTable ( ID int identity(1,1),
[Name] varchar(64),
constraint [PK_MyTable] primary key clustered (ID asc) on [Primary]
)
--suppose this data already existed...
INSERT INTO MyTable (Name)
VALUES
('First'),
('Second'),
('Third'),
('Fourth'),
('Fifth');
--now we insert some more... and then only return these rows
INSERT INTO MyTable (Name)
VALUES
('Sixth'),
('Seventh')
select top (##ROWCOUNT)
ID,
Name
from MyTable
order by ID desc
##ROWCOUNT returns the number of rows affected by the last statement executed. You can always see this in the messages tab of SQL Server Management Studio. Thus, we are getting the number of rows inserted and combining it with TOP which limits the rows returned in a query to the specified number of rows (or percentage if you use [PERCENT]). It is important that you use ORDER BY when using TOP otherwise your results aren't guaranteed to be the same
From my previous edited answer...
If you are trying to see what values were inserted, then I assume you are inserting them a different way and this is usually handled with an OUTPUT clause, TRIGGER if you are trying to do something with these records after the insert, etc... more information would be needed.

Copying records from one table to another of different SQL Servers

I am using SQL Server 2012. I have same table on both server with 15 columns.first table has less records than second. I want to copy remaining records from second table to first. Both tables are on different SQL server. So I have created linked server. I was thinking about IF EXISTS. But i want to copy more records. I want to create script for this task. and I will use this frequently using task scheduler.(Note: I don't want to use Replication)
You Can Use Except Operator
For Example
Drop table #aa
Create table #aa (Id Int)
Insert into #aa
Select 1
Drop table #bb
Create table #bb (Id Int)
Insert into #bb
Select 1
Union all
Select 2
Insert into #aa
Select * from #bb
Except
Select * from #aa

Resources