How to insert a row into a linked server table? [closed]
Asked Answered
S

6

13

I have a server SourceServer I am connected to which has a linked server TargetServer.

How should an insert statement looks like (I need to reference Linked server, database, namespace, table):

//Connected to [SourceServer]

USE [SourceDatabase]

DECLARE @HelloWorld NVARCHAR(255)

SELECT @HelloWorld = Name From dbo.Names where Id = 1

INSERT INTO [TargetServer].[TestDatabase].dbo.TestTable (Name)   VALUES (@HelloWorld)

This statement executes with an exception:

Too many prefixes.

Update: The syntax as above works fine, the problem was expired password for the sql user used to connect to the linked server :)

Sixpenny answered 22/6, 2012 at 8:45 Comment(3)
Are you sure that your table or schema names doesn't has a '.' in it? If so, you will need to use [] again.Conservatoire
there is no syntax error, please check the value of TargetServer valueSining
The names a fully ok in my real query and fully fake here, I explicitly build a query so that the insert statement has less places which can cause an exception.Sixpenny
S
24
INSERT INTO [TargetServer].[TestDatabase].[dbo].TestTable (Name)
SELECT Name From [SourceServer].[SourceDatabase].[dbo].[Names] where Id = 1
Stortz answered 22/6, 2012 at 9:0 Comment(3)
You don't need to rewrite a fake sql query. Does your answer mean that [TargetServer].[TestDatabase].[dbo].TestTable is acceptable? Why do I get a too many prefixes exception?Sixpenny
Maybe this will help: social.msdn.microsoft.com/Forums/en/transactsql/thread/…Stortz
Looking at the view statement the syntax at least should be ok, hmm.Sixpenny
F
4

For those using openquery here's how to do it:

INSERT INTO OPENQUERY ([LINKEDSERVERNAME], 'SELECT idjob, salarylocal, salarydollars, calification FROM employee')
SELECT 1, 666, 668, 10
Fireplug answered 18/6, 2022 at 20:5 Comment(0)
Q
0
select * into [TargetServer].[TestDatabase].[dbo].TestTable
 From [SourceServer].[SourceDatabase].[dbo].[Names]
Queenie answered 3/8, 2016 at 18:26 Comment(2)
the question was about insertTillich
This errors with The object name '[TargetServer].[TestDatabase].[dbo].TestTable' contains more than the maximum number of prefixes. The maximum is 2. Insert into rather than select * into does not error....but obvs will add to rows of existing table so truncate/delete as appropriate.Mission
A
0

If the target table schema already there in Target use below code

INSERT INTO [TargetLinkedServerName].[TestDatabase].[dbo].[TargetTestTable] 
SELECT * From [SourceLinkedServerName].[SourceDatabase].[dbo].[SourceTestTable]

If the target table schema already not there in Target use below code(This code will create new table in Target as like source table)

select * into [TargetLinkedServerName].[TestDatabase].[dbo].[TargetTestTable] 
 From [SourceLinkedServerName].[SourceDatabase].[dbo].[SourceTestTable]
Acidforming answered 13/2, 2020 at 8:28 Comment(1)
I know this a very long time since this post was made but I have never found an answer. The Select into returns this error. Msg 117, Level 15, State 1, Line 41 The object name 'TargetLinkedServerName.TestDatabase.dbo.TestTable' contains more than the maximum number of prefixes. The maximum is 2.Giffie
D
0

If you want to insert data over a linked server the table needs to exist

--step 1 on TargetLinkedServerName: 

create table [TestDatabase].[dbo].[TargetTestTable]

--step 2 on SourceLinkedServerName:

INSERT INTO [TargetLinkedServerName].[TestDatabase].[dbo].[TargetTestTable] 
SELECT * From [SourceDatabase].[dbo].[SourceTestTable]
Discomfiture answered 10/11, 2022 at 16:27 Comment(0)
A
0

I've got the same issue recently. I found the solution by using below method, which is storing the searched result into temporary table, then only insert that temporary table into the linked server table.

SELECT @HelloWorld = Name 
INTO #TempDBA
From dbo.Names where Id = 1

INSERT INTO [TargetServer].[TestDatabase].dbo.TestTable (Name)
SELECT * FROM #TempDBA
Autoclave answered 29/11, 2023 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.