8

I have created a linkedserver as ravikiran-vm which is the virtual machine in my desktop.

Now I have a database called kiran which contains an employ table.
To retrieve employ data, I do the following:

select * from ravikiran-vm.kiran.employ

but it shows the error "Incorrect syntax near '-'."

Can anyone help me, please?

Thanks in advance.

Thanks guys with ur support it working fine... Now i hav to schedule this as a new job.when i execute it as normal it shows o/p. but when i cinfigure the same query as sqlserver agent job it gives error and query not executing...Plz help me in this regard

Thanks in advance

6 Answers 6

16

I think you should change the name of the linked server, as the - char is reserved in SQL.

You could try surrounding the name with brackets, but it becomes boring

Also, you should include the schema name in the query, or double point to use the default one:

so, you can try:

select * from [ravikiran-vm].kiran.dbo.employ
select * from [ravikiran-vm].kiran..employ

Or whatever your schema be.

0
5

You have to use OPENQUERY:

SELECT * FROM OPENQUERY([ravikiran-vm],'SELECT * FROM KIRAN..EMPLOY')
1
  • Yep. This was the only way I could do it. Thank you. Saved me hours!
    – Fandango68
    Commented Oct 15, 2014 at 1:49
2

to get data from linked server you use 4 part notation Server.Database.Schema.Table

since you have an invalid character in your name(-) you need to add brackets around the name

select * from [ravikiran-vm].kiran..employ

You probably also don't want all the data returned

0

Usually direct queries should not be used in case of linked server because it heavily use temp database of SQL server. At first step data is retrieved into temp DB then filtering occur. There are many threads about this. It is better to use open OPENQUERY because it passes SQL to the source linked server and then it return filtered results e.g.

SELECT * FROM OPENQUERY(Linked_Server_Name , 'select * from TableName where ID = 500')

0
1- Link the server

EXEC sp_addlinkedserver 'OracleSvr', 
'Oracle 7.3', 
'MSDAORA', 
'ORCLDB'


2-SELECT
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM albert.titles')

3-UPDATE 
UPDATE OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles WHERE id = 101') 

4-INSERT
INSERT OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles')
VALUES ('NewTitle');

5-DELETE
DELETE OPENQUERY (OracleSvr, 'SELECT name FROM albert.titles WHERE name = ''NewTitle''')

i just copied from here (http://www.sqlservercentral.com/Forums/Topic916320-392-1.aspx)

0
Select * from likedservername.databasename.dbo(schema).tablename

Ex1:

select * from [Bse].[Bse].[dbo].Binary

Else

Select * from openquery (linkedservername, 'select * from databasename.dbo(schema).tablename');

Ex2:

select * from openquery ([Bse], 'select * from [Bse].[dbo].Binary');
1
  • What is your question exactly?
    – Swifty
    Commented May 24, 2019 at 20:18

Your Answer

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