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

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.

Related

Determine UniqueIdentifier id of newly created record in remote table

When I create a new record in a table that has a primary key of type INT and I want to retrieve the value of its ID, I use something like this:
INSERT INTO MyTable (Fields)
VALUES (MyValues);
SELECT SCOPE_IDENTITY();
However, this does not work with primary keys of type UNIQUEIDENTIFIER. So the solution is:
INSERT INTO MyTable (Fields)
OUTPUT Inserted.MyPrimaryField
VALUES (MyValues);
Unfortunetly, this does not work with remote tables from linked servers, since I get an error reading:
A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.
Are there any other options?

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)

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

Error creating entity Microsoft SQL server IDENTITY_INSERT

I'm using JHipster 4. I've create a simple entity just with a "name" property and when I try to create a entity from UI I get the folowing error. (I'm using Microsoft SQL Server).
In think the important part of the error is :
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'tenant' when IDENTITY_INSERT is set to OFF.
Direct answer appears to be that you just need to configure jhipster to have identity_insert on, per this link: https://jhipster.github.io/tips/004_tip_using_ms_sql_server.html
Adding the identityInsertEnabled="true" is the same as wrapping your Inserts with IDENTITY_INSERT ON and IDENTITY_INSERT OFF which will allow you to insert the project autogenerated identities directly. This is why we are using the MS SQL Server Liquibase for.
The error tells you everything you need to know. You are trying to insert a value in to an identity column. This is not allowed, as the column is supposed to be automatically populated unless you explicitly turn it off temporarily.
Documentation on what identity_insert is here: https://msdn.microsoft.com/en-us/library/ms188059.aspx
You can turn it off using the command set identity_insert SchemaName.TableName off though you had better be very confident you are inserting the correct data.
I would recommend you do some investigation with whoever manages your database as to why that column is an identity column and whether or not you should be inserting into it at all.
Assume a table:
create table test(ID int identity(1,1)
,Name nvarchar(100)
)
ALL of these insert statements will throw an error:
insert into test(ID,Name)
select ID
,Name
from OtherTable
insert into test(ID,Name)
select null as ID
,'TestName' as Name
insert into test(ID,Name)
values(99
,'Name'
)
Whereas these will work, automatically generating a new, unique value for the ID column:
insert into test(Name)
select Name
from OtherTable
insert into test(Name)
select 'TestName' as Name
insert into test(Name)
values('Name')
In short, you need to insert nothing into the identity column. Not a blank string, not a null value, but absolutely nothing at all.

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

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

Resources