{"id":59,"date":"2013-07-02T14:25:00","date_gmt":"2013-07-02T19:25:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=59"},"modified":"2023-07-26T12:33:48","modified_gmt":"2023-07-26T17:33:48","slug":"sql-server-using-quotes-in-linked-server-queries","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=59","title":{"rendered":"SQL Server : Using Quotes In Linked Server Queries"},"content":{"rendered":"<div>\n<div>\n<div>\nDoing an OpenQuery across a linked server can get ugly very fast and it is often best to avoid them.\n<\/div>\n<div>\nThe basic syntax of an OpenQuery is :<\/div>\n<div>\n<\/div>\n<div>\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">SELECT<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\"> <span style=\"color: grey;\">*<\/span> <span style=\"color: blue;\">FROM<\/span> <span style=\"color: blue;\">OPENQUERY<\/span><span style=\"color: grey;\">(<\/span>YourLinkedServerName<span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8216;SELECT FIELD1, FIELD2 FROM YOURTABLE&#8217;<\/span><span style=\"color: grey;\">)<\/span><\/span><\/div>\n<div>\n<\/div>\n<div>\nFrom a coding perspective, this gets ugly if you have to incorporate string parameters; thus, utilizing many quotes.&nbsp; For Example:<\/div>\n<div>\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SELECT<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <span style=\"color: grey;\">*<\/span> <span style=\"color: blue;\">FROM<\/span> <span style=\"color: blue;\">OPENQUERY<\/span><span style=\"color: grey;\">(<\/span>YourLinkedServerName<span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8216;SELECT FIELD1, FIELD2 FROM YOURTABLE WHERE FIELD1 = &#8221;ID105&#8221; &#8216;<\/span><span style=\"color: grey;\">)<o:p><\/o:p><\/span><\/span><\/div>\n<div>\n<\/div>\n<div>\nMicrosoft made this easier with the built-in stored procedure that allows you to avoid multi-layered quotes:&nbsp; <span style=\"color: maroon; font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">sp_executesql<\/span><\/div>\n<div>\n<\/div>\n<div>\nThe above can become:<o:p><\/o:p><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">DECLARE<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @PARAM <span style=\"color: blue;\">char<\/span><span style=\"color: grey;\">(<\/span>5<span style=\"color: grey;\">)<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SELECT<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">&nbsp; @PARAM <span style=\"color: grey;\">=<\/span> <span style=\"color: red;\">&#8216;CA105&#8217;<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">EXEC<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> YourLinkedServer<span style=\"color: grey;\">.<\/span><span style=\"color: blue;\">master<\/span><span style=\"color: grey;\">.<\/span>dbo<span style=\"color: grey;\">.<\/span><span style=\"color: maroon;\">sp_executesql<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">N&#8217;SELECT FIELD1, FIELD2 FROM YOURTABLE WHERE FIELD1 = @p1&#8242;<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;; font-size: 10pt;\">,<o:p><\/o:p><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">N&#8217;@p1 char(5)&#8217;<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;; font-size: 10pt;\">,<o:p><\/o:p><\/span><\/div>\n<div>\n<span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">@PARAM<\/span><\/div>\n<div>\n<\/div>\n<div>\nBut, what if your WHERE clause becomes:<\/div>\n<div>\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">WHERE <\/span><span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">FIELD2 in (&#8221;ID105&#8221;,&#8221;ID107&#8221;,&#8221;ID109&#8221;,&#8221;ID112&#8221;,&#8221;ID116&#8221;,<o:p><\/o:p><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8221;ID119&#8221;,&#8221;ID400&#8221;,&#8221;ID404&#8221;,&#8221;ID101&#8221;,&#8221;ID102&#8221;,&#8221;ID115&#8221;,&#8221;ID106&#8221;,&#8221;ID111&#8221;,&#8221;ID117&#8221;,&#8221;ID412&#8221;,&#8221;654Z&#8221;,&#8221;ID103&#8221;,&#8221;ID121&#8221;,&#8221;ID401&#8221;,&#8221;ID403&#8221;,&#8221;ID407&#8221;,&#8221;ID108&#8221;,&#8221;ID113&#8221;,&#8221;ID114&#8221;,&#8221;ID120&#8221;,&#8221;ID123&#8221;,&#8221;ID405&#8221;,&#8221;ID409&#8221;,&#8221;ID100&#8221;,&#8221;ID110&#8221;,&#8221;ID124&#8221;,&#8221;ID408&#8221;,&#8221;ID104&#8221;,&#8221;ID118&#8221;,&#8221;ID406&#8221;,<o:p><\/o:p><\/span><\/div>\n<div>\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">&#8221;ID413&#8221;)<\/span><\/div>\n<div>\n<\/div>\n<div>\nThere are multiple ways to go about this but here is one cheap method for dealing with the quotes:<o:p><\/o:p><\/div>\n<div>\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">DECLARE <\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">@TSQL<span style=\"color: blue;\"> NVARCHAR(2000);<o:p><\/o:p><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">DECLARE <\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">@CODES<span style=\"color: blue;\"> NVARCHAR(500);<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">DECLARE<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @OPENQUERY <span style=\"color: blue;\">NVARCHAR<\/span><span style=\"color: grey;\">(<\/span>100<span style=\"color: grey;\">);<o:p><\/o:p><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">DECLARE<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @LINKED_SQL <span style=\"color: blue;\">NVARCHAR<\/span><span style=\"color: grey;\">(<\/span>2700<span style=\"color: grey;\">)<\/span><span style=\"color: blue;\"><o:p><\/o:p><\/span><\/span><\/div>\n<div>\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SET<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @CODES <span style=\"color: grey;\">= <span style=\"color: red;\">&#8221;&#8217;ID105&#8221;,&#8221;ID107&#8221;,&#8221;ID109&#8221;,&#8221;ID112&#8221;,&#8221;ID116&#8221;,<o:p><\/o:p><\/span><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8221;ID119&#8221;,&#8221;ID400&#8221;,&#8221;ID404&#8221;,&#8221;ID101&#8221;,&#8221;ID102&#8221;,&#8221;ID115&#8221;,&#8221;ID106&#8221;,&#8221;ID111&#8221;,&#8221;ID117&#8221;,&#8221;ID412&#8221;,&#8221;654Z &#8221;,&#8221;ID103&#8221;,&#8221;ID121&#8221;,&#8221;ID401&#8221;,&#8221;ID403&#8221;,<\/span><span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8221;ID407&#8221;,&#8221;ID108&#8221;,&#8221;ID113&#8221;,&#8221;ID114&#8221;,&#8221;ID120&#8221;,&#8221;ID123&#8221;,&#8221;ID405&#8221;,&#8221;ID409&#8221;,&#8221;ID100&#8221;,&#8221;ID110&#8221;,&#8221;ID124&#8221;,&#8221;ID408&#8221;,&#8221;ID104&#8221;,&#8221;ID118&#8221;,&#8221;ID406&#8221;,<o:p><\/o:p><\/span><\/div>\n<div>\n<span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">&#8221;ID413&#8221;&#8217;<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;; font-size: 10pt; line-height: 115%;\">;<\/span><\/div>\n<div>\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SELECT<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @TSQL <span style=\"color: grey;\">=<\/span> <span style=\"color: red;\">&#8216;SELECT \u2026 WHERE FIELD2 IN (\u2018<\/span><span style=\"color: grey;\">+<\/span> @CODES <span style=\"color: grey;\">+<\/span> <span style=\"color: red;\">&#8216;)&#8217;<\/span>&nbsp;&nbsp; <\/span>\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: green; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8212; CLEAN UP THE QUOTES FOR THE OPENQUERY<o:p><\/o:p><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: green; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p><\/o:p><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SET<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @TSQL <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">REPLACE<\/span><span style=\"color: grey;\">(<\/span>@TSQL<span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8221;&#8221;<\/span><span style=\"color: grey;\">,<\/span><span style=\"color: red;\">&#8221;&#8221;&#8221;<\/span><span style=\"color: grey;\">)<\/span>&nbsp; <span style=\"color: grey;\">;<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SET<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @TSQL <span style=\"color: grey;\">=<\/span> @TSQL <span style=\"color: grey;\">+<\/span> <span style=\"color: red;\">&#8221;&#8217;)&#8217;<\/span><span style=\"color: grey;\">;<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SET<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @OPENQUERY <span style=\"color: grey;\">=<\/span> <span style=\"color: red;\">&#8216;SELECT * FROM OPENQUERY(GHDB, &#8221;&#8217;<\/span><span style=\"color: grey;\">;<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SET<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> @LINKED_SQL <span style=\"color: grey;\">=<\/span><span style=\"color: blue;\"> <\/span><span style=\"color: grey;\">(<\/span>@OPENQUERY<span style=\"color: grey;\">+<\/span>@TSQL<span style=\"color: grey;\">)<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<\/div>\n<div style=\"line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">EXEC<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <span style=\"color: maroon;\">sp_executesql<\/span><span style=\"color: blue;\"> <\/span>@LINKED_SQL<\/span><o:p><\/o:p><\/div>\n<div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Doing an OpenQuery across a linked server can get ugly very fast and it is often best to avoid them. The basic syntax of an OpenQuery is : SELECT * FROM OPENQUERY(YourLinkedServerName, &#8216;SELECT FIELD1, FIELD2 FROM YOURTABLE&#8217;) From a coding perspective, this gets ugly if you have to incorporate string parameters; thus, utilizing many quotes.&nbsp; &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=59\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server : Using Quotes In Linked Server Queries&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}