SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


OPENQUERY SYNTAX IS INSANE


OPENQUERY SYNTAX IS INSANE

Author
Message
applebyte
applebyte
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 152
Hello everyone.
I've been trying to execute an openquery statement where a table is loaded using data from a linked server. The query is successful if the WHERE clause is not used. However, I want to limit the data retrieved via the WHERE clause. After many attempts working with the syntax I have not been successful in achieving a successful execution. The correct usage of quotes (double quotes?) is extremely frustrating. Would anyone care to take a shot at this and maybe provide a few tips. Thanks much in advance.
John

** the code below is using all single quotes.
----------------------------------------------------------------------------
OPEN HOST_Cursor
FETCH NEXT FROM HOST_Cursor INTO @LinkedServer,@port_nbr

WHILE @@FETCH_STATUS = 0

BEGIN

SET @SQL = N'INSERT dbo.SERVER_LEVEL_Access_cmpr

SELECT *
From OPENQUERY ([' + @linkedServer + ',' + @port_nbr + '],
''SELECT
@@servername
,name
,type_desc
,convert(varchar(10),create_date,121)
,convert(varchar(10),getdate(),121)
,null
,null
FROM master.sys.server_principals
WHERE type_desc not in ('SERVER_ROLE' , 'CERTIFICATE_MAPPED_LOGIN') ')

EXEC (@SQL)

FETCH NEXT FROM HOST_Cursor INTO @LinkedServer,@port_nbr

END

CLOSE HOST_Cursor

John Michael Robertson
John Michael Robertson
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 357
it appears this is a case of needing to "embed" the single quotes within a quoted string. Anytime a sql statement is embedded in a "string" all items normally single-quoted require an extra one.
here is an example using two single quotes together based on your query: WHERE type_desc not in (''SERVER_ROLE'' , ''CERTIFICATE_MAPPED_LOGIN'')

try adding the extra single quote to the items in the where clause that are already single-quoted.
does this make sense? I hope I did not make it more confusing.
applebyte
applebyte
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 152
thanks for the reply. Adding the single quotes allowed the 'parse' check to be successful but when the code was executed, a syntax error occured.

--
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near 'SERVER_ROLE'.
Msg 105, Level 15, State 1, Line 14
Unclosed quotation mark after the character string ')'.

Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66309 Visits: 9671
You need quadruple quotes when you're in double dynamic sql.
John Michael Robertson
John Michael Robertson
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 357
applebyte (11/9/2011)
thanks for the reply. Adding the single quotes allowed the 'parse' check to be successful but when the code was executed, a syntax error occured.

--
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near 'SERVER_ROLE'.
Msg 105, Level 15, State 1, Line 14
Unclosed quotation mark after the character string ')'.


Yes, i was wondering about that too. You have the select statement embedded too, but do not close the select statement either. Since the SELECT statement is embedded in a string, the single-quoted items in the where have to "double-quoted" twice.

Here is the final version of your SQL. Note the 4 single-quotes on the where clause items.

SET @SQL = N'INSERT dbo.SERVER_LEVEL_Access_cmpr

SELECT *
From OPENQUERY ([' + @linkedServer + ',' + @port_nbr + '],
''SELECT
@@servername
,name
,type_desc
,convert(varchar(10),create_date,121)
,convert(varchar(10),getdate(),121)
,null
,null
FROM master.sys.server_principals
WHERE type_desc not in (''''SERVER_ROLE'''' , ''''CERTIFICATE_MAPPED_LOGIN'''') '') '




Another helpful trick I usually do in these cases is actually PRINT the variable during tests without performing the "EXEC(@SQL)" command. This way you can copy the output and try it to see if all the single-quoted items are terminated correctly with the correct number of single-quotes. It can be very tricky to get them right sometimes.
I hope this helps.


EDIT: Also notice the terminated "SELECT" clause with the additional quotes prior to the end parantheses { '') ' }
applebyte
applebyte
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 152
Success! Gentlemen, thanks so much for your assitance. Your responses have been extremely helpful and insightful.

John
Rich Mechaber
Rich Mechaber
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2707 Visits: 3671
I'm a little late to this question, but here's my chicken-#$%^ way I deal with things like this when the OPENROWSET target is small: I read everything in to a temp table (no WHERE clause), then run my real query w/ the WHERE clause against the temp table. For 2,000 row text files, it works just fine. Obviously if your WHERE was necessary to reduce 15,000,000 rows to 17, this wouldn't be so good, but then you probably would be reaching for SSIS or BULK COPY anyway.

Rich
applebyte
applebyte
SSC Veteran
SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)SSC Veteran (254 reputation)

Group: General Forum Members
Points: 254 Visits: 152
Good tip. thanks rich.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search