Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 syntax error

Author  Topic 

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-06-24 : 06:41:36
Hi all

declare @server_name varchar(100)
set @server_name = 'GGNMACLYADAVA\MSSQL_2005'
select *
FROM OPENROWSET('SQLNCLI','server=' + @server_name +';trusted_connection=yes',
'select name from syslogins where sysadmin=1 ')




i am trying to execute this query but getting following error,not able
to ractify it...

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '+'.



and i getting proper result when i execute below one


select *
FROM OPENROWSET('SQLNCLI','server=GGNMACLYADAVA\MSSQL_2005;trusted_connection=yes',
'select name from syslogins where sysadmin=1 ')



Devart
Posting Yak Master

102 Posts

Posted - 2010-06-24 : 07:24:38
IMHO only string constants may be used in OPENROWSET function.

Devart,
Tools for SQL Server
http://www.devart.com/dbforge/sql
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 07:35:12
Yes. OPENROWSET isn't accepting that.
Instead you can use dynamic statement.
Try:
exec('select * FROM OPENROWSET(''SQLNCLI'',''server=''' + @server_name +''';trusted_connection=yes'',
''select name from syslogins where sysadmin=1 '') ')


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2010-06-24 : 07:38:22
No, it takes variable also ...i did some changes and my code is workig fine...



quote:
Originally posted by Devart

IMHO only string constants may be used in OPENROWSET function.

Devart,
Tools for SQL Server
http://www.devart.com/dbforge/sql

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-24 : 07:40:44
Maybe you can post your solution to help others which are having the same problem?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -