Why do I get the error “Xml data type is not supported in distributed queries” when querying a linked...












49















I have two SQL Servers (running SQL Server 2008) named DATA01 and DATA02. DATA02 has a linked server definition LINK, that points at DATA01, with suitable user mapping set up. On DATA01 there is a database MyDatabase containing these two tables:



CREATE TABLE T_A (
Id int
)

CREATE TABLE T_B (
Id int,
Stuff xml
)


When I run this command from DATA02, I get data returned as expected:



SELECT Id FROM LINK.MyDatabase.dbo.T_A;


However, when I run this command from DATA02, I get an error:



SELECT Id, Stuff FROM LINK.MyDatabase.dbo.T_B;


The error is




Xml data type is not supported in distributed queries. Remote object 'DATA02.MyDatabase.dbo.T_B' has xml column(s).




And strangely, this command:



SELECT Id FROM LINK.MyDatabase.dbo.T_B;


also gives the same error, even though I'm not SELECTing the xml column! What's going on?










share|improve this question



























    49















    I have two SQL Servers (running SQL Server 2008) named DATA01 and DATA02. DATA02 has a linked server definition LINK, that points at DATA01, with suitable user mapping set up. On DATA01 there is a database MyDatabase containing these two tables:



    CREATE TABLE T_A (
    Id int
    )

    CREATE TABLE T_B (
    Id int,
    Stuff xml
    )


    When I run this command from DATA02, I get data returned as expected:



    SELECT Id FROM LINK.MyDatabase.dbo.T_A;


    However, when I run this command from DATA02, I get an error:



    SELECT Id, Stuff FROM LINK.MyDatabase.dbo.T_B;


    The error is




    Xml data type is not supported in distributed queries. Remote object 'DATA02.MyDatabase.dbo.T_B' has xml column(s).




    And strangely, this command:



    SELECT Id FROM LINK.MyDatabase.dbo.T_B;


    also gives the same error, even though I'm not SELECTing the xml column! What's going on?










    share|improve this question

























      49












      49








      49


      10






      I have two SQL Servers (running SQL Server 2008) named DATA01 and DATA02. DATA02 has a linked server definition LINK, that points at DATA01, with suitable user mapping set up. On DATA01 there is a database MyDatabase containing these two tables:



      CREATE TABLE T_A (
      Id int
      )

      CREATE TABLE T_B (
      Id int,
      Stuff xml
      )


      When I run this command from DATA02, I get data returned as expected:



      SELECT Id FROM LINK.MyDatabase.dbo.T_A;


      However, when I run this command from DATA02, I get an error:



      SELECT Id, Stuff FROM LINK.MyDatabase.dbo.T_B;


      The error is




      Xml data type is not supported in distributed queries. Remote object 'DATA02.MyDatabase.dbo.T_B' has xml column(s).




      And strangely, this command:



      SELECT Id FROM LINK.MyDatabase.dbo.T_B;


      also gives the same error, even though I'm not SELECTing the xml column! What's going on?










      share|improve this question














      I have two SQL Servers (running SQL Server 2008) named DATA01 and DATA02. DATA02 has a linked server definition LINK, that points at DATA01, with suitable user mapping set up. On DATA01 there is a database MyDatabase containing these two tables:



      CREATE TABLE T_A (
      Id int
      )

      CREATE TABLE T_B (
      Id int,
      Stuff xml
      )


      When I run this command from DATA02, I get data returned as expected:



      SELECT Id FROM LINK.MyDatabase.dbo.T_A;


      However, when I run this command from DATA02, I get an error:



      SELECT Id, Stuff FROM LINK.MyDatabase.dbo.T_B;


      The error is




      Xml data type is not supported in distributed queries. Remote object 'DATA02.MyDatabase.dbo.T_B' has xml column(s).




      And strangely, this command:



      SELECT Id FROM LINK.MyDatabase.dbo.T_B;


      also gives the same error, even though I'm not SELECTing the xml column! What's going on?







      sql-server xml linked-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 21 '13 at 15:36









      AakashMAakashM

      52.9k13127172




      52.9k13127172
























          3 Answers
          3






          active

          oldest

          votes


















          86














          This is a deficiency within SQL Server. The mere existence of an xml column on the table prevents it from participating in distributed queries (eg being queried through a linked server connection). This is mentioned in the documentation, though not particularly prominently. You can see the main Connect bug report here, and a similar report here. The latter gives two workarounds:






          1. Create [a] view without the XML column(s) on remote server and query that.




            In your example, this would involve adding a view to MyDatabase
            that looks like this:



            CREATE VIEW V_T_B AS SELECT Id FROM T_B;


            You could then query this view through the link to get the Id
            data. Note that something like



            SELECT Id FROM ( SELECT Id FROM T_B ) T_B;


            doesn't work.





          2. Use a pass-through query in the form



            SELECT * from OPENQUERY (... )



            This method has the advantage of not requiring any change to the
            source database; the downside is that it is no longer possible to
            use standard four-part naming for both local and linked data. The
            query would look like



            SELECT Id FROM OPENQUERY(DATA02, 'SELECT Id FROM T_B') T_B;


            Note that if you actually do want the xml data, this method (along
            with casting to and from a non-xml datatype) will be required :



            SELECT Id, CAST(Stuff AS XML) Stuff 
            FROM OPENQUERY(DATA02, 'SELECT Id, CAST(Stuff AS nvarchar(max)) Stuff
            FROM T_B') T_B;



          Note that the bug was first reported in SQL Server 2005, and remains unfixed in SQL Server 2014.






          share|improve this answer





















          • 4





            You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

            – Aaron Bertrand
            Jan 21 '13 at 15:37











          • when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

            – RMiranda
            Jun 20 '13 at 0:21






          • 1





            @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

            – AakashM
            Jul 3 '13 at 16:46











          • such a simple solution! Thanks

            – melodiouscode
            Oct 26 '16 at 7:42



















          9














          Try this:




          • Create a view on the source side with xml cast to nvarchar(max):


          CREATE VIEW vXMLTest
          AS
          SELECT cast(Stuff as nvarchar(max)) as STUFF FROM T_B




          • You can select it on the destination side with cast to xml


          SELECT Cast(Stuff as XML) as Stuff
          FROM OPENQUERY(DATA02, 'SELECT Stuff FROM vXMLTest')



          This solution works for me in 2008R2.






          share|improve this answer





















          • 1





            Excellent idea! Works great.

            – NealWalters
            Mar 20 '14 at 16:43



















          1














          I found another way of doing this:




          1. Create a Linked Server on DATA01, DATA02 or even some third server (could be local).

          2. Execute your query using EXEC [linked_server].[sp_sqlexecute].


          Why I prefer this method over creating Views or using OPENQUERY?




          1. This method does not require to have any permissions on the Linked Server (you don't need to create Views).

          2. You could pass a parameters to your query using the sp_sqlexecute syntax (find more information about sp_sqlexecute here). For OPENQUERY you must pass a STRING or TEXT_LEX, witch means that all the values must be typed right into this function.


          Here is the example:



          DECLARE @UserID UNIQUEIDENTIFIER = ''

          DECLARE @SearchForUserParams NVARCHAR(MAX) = N'@UserID UNIQUEIDENTIFIER';
          DECLARE @SearchForUserQuery NVARCHAR(MAX) =
          N'SELECT
          UserID,
          Username,
          Email,
          CONCART(NVARCHAR(MAX), UserDataXml) AS UserDataXml
          FROM User
          WHERE UserID = @UserID
          '

          EXEC [linked_server].[dbo].[sp_executesql]
          @SearchForUserQuery,
          @SearchForUserParams,
          @UserID = @UserID





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14442039%2fwhy-do-i-get-the-error-xml-data-type-is-not-supported-in-distributed-queries-w%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            86














            This is a deficiency within SQL Server. The mere existence of an xml column on the table prevents it from participating in distributed queries (eg being queried through a linked server connection). This is mentioned in the documentation, though not particularly prominently. You can see the main Connect bug report here, and a similar report here. The latter gives two workarounds:






            1. Create [a] view without the XML column(s) on remote server and query that.




              In your example, this would involve adding a view to MyDatabase
              that looks like this:



              CREATE VIEW V_T_B AS SELECT Id FROM T_B;


              You could then query this view through the link to get the Id
              data. Note that something like



              SELECT Id FROM ( SELECT Id FROM T_B ) T_B;


              doesn't work.





            2. Use a pass-through query in the form



              SELECT * from OPENQUERY (... )



              This method has the advantage of not requiring any change to the
              source database; the downside is that it is no longer possible to
              use standard four-part naming for both local and linked data. The
              query would look like



              SELECT Id FROM OPENQUERY(DATA02, 'SELECT Id FROM T_B') T_B;


              Note that if you actually do want the xml data, this method (along
              with casting to and from a non-xml datatype) will be required :



              SELECT Id, CAST(Stuff AS XML) Stuff 
              FROM OPENQUERY(DATA02, 'SELECT Id, CAST(Stuff AS nvarchar(max)) Stuff
              FROM T_B') T_B;



            Note that the bug was first reported in SQL Server 2005, and remains unfixed in SQL Server 2014.






            share|improve this answer





















            • 4





              You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

              – Aaron Bertrand
              Jan 21 '13 at 15:37











            • when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

              – RMiranda
              Jun 20 '13 at 0:21






            • 1





              @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

              – AakashM
              Jul 3 '13 at 16:46











            • such a simple solution! Thanks

              – melodiouscode
              Oct 26 '16 at 7:42
















            86














            This is a deficiency within SQL Server. The mere existence of an xml column on the table prevents it from participating in distributed queries (eg being queried through a linked server connection). This is mentioned in the documentation, though not particularly prominently. You can see the main Connect bug report here, and a similar report here. The latter gives two workarounds:






            1. Create [a] view without the XML column(s) on remote server and query that.




              In your example, this would involve adding a view to MyDatabase
              that looks like this:



              CREATE VIEW V_T_B AS SELECT Id FROM T_B;


              You could then query this view through the link to get the Id
              data. Note that something like



              SELECT Id FROM ( SELECT Id FROM T_B ) T_B;


              doesn't work.





            2. Use a pass-through query in the form



              SELECT * from OPENQUERY (... )



              This method has the advantage of not requiring any change to the
              source database; the downside is that it is no longer possible to
              use standard four-part naming for both local and linked data. The
              query would look like



              SELECT Id FROM OPENQUERY(DATA02, 'SELECT Id FROM T_B') T_B;


              Note that if you actually do want the xml data, this method (along
              with casting to and from a non-xml datatype) will be required :



              SELECT Id, CAST(Stuff AS XML) Stuff 
              FROM OPENQUERY(DATA02, 'SELECT Id, CAST(Stuff AS nvarchar(max)) Stuff
              FROM T_B') T_B;



            Note that the bug was first reported in SQL Server 2005, and remains unfixed in SQL Server 2014.






            share|improve this answer





















            • 4





              You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

              – Aaron Bertrand
              Jan 21 '13 at 15:37











            • when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

              – RMiranda
              Jun 20 '13 at 0:21






            • 1





              @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

              – AakashM
              Jul 3 '13 at 16:46











            • such a simple solution! Thanks

              – melodiouscode
              Oct 26 '16 at 7:42














            86












            86








            86







            This is a deficiency within SQL Server. The mere existence of an xml column on the table prevents it from participating in distributed queries (eg being queried through a linked server connection). This is mentioned in the documentation, though not particularly prominently. You can see the main Connect bug report here, and a similar report here. The latter gives two workarounds:






            1. Create [a] view without the XML column(s) on remote server and query that.




              In your example, this would involve adding a view to MyDatabase
              that looks like this:



              CREATE VIEW V_T_B AS SELECT Id FROM T_B;


              You could then query this view through the link to get the Id
              data. Note that something like



              SELECT Id FROM ( SELECT Id FROM T_B ) T_B;


              doesn't work.





            2. Use a pass-through query in the form



              SELECT * from OPENQUERY (... )



              This method has the advantage of not requiring any change to the
              source database; the downside is that it is no longer possible to
              use standard four-part naming for both local and linked data. The
              query would look like



              SELECT Id FROM OPENQUERY(DATA02, 'SELECT Id FROM T_B') T_B;


              Note that if you actually do want the xml data, this method (along
              with casting to and from a non-xml datatype) will be required :



              SELECT Id, CAST(Stuff AS XML) Stuff 
              FROM OPENQUERY(DATA02, 'SELECT Id, CAST(Stuff AS nvarchar(max)) Stuff
              FROM T_B') T_B;



            Note that the bug was first reported in SQL Server 2005, and remains unfixed in SQL Server 2014.






            share|improve this answer















            This is a deficiency within SQL Server. The mere existence of an xml column on the table prevents it from participating in distributed queries (eg being queried through a linked server connection). This is mentioned in the documentation, though not particularly prominently. You can see the main Connect bug report here, and a similar report here. The latter gives two workarounds:






            1. Create [a] view without the XML column(s) on remote server and query that.




              In your example, this would involve adding a view to MyDatabase
              that looks like this:



              CREATE VIEW V_T_B AS SELECT Id FROM T_B;


              You could then query this view through the link to get the Id
              data. Note that something like



              SELECT Id FROM ( SELECT Id FROM T_B ) T_B;


              doesn't work.





            2. Use a pass-through query in the form



              SELECT * from OPENQUERY (... )



              This method has the advantage of not requiring any change to the
              source database; the downside is that it is no longer possible to
              use standard four-part naming for both local and linked data. The
              query would look like



              SELECT Id FROM OPENQUERY(DATA02, 'SELECT Id FROM T_B') T_B;


              Note that if you actually do want the xml data, this method (along
              with casting to and from a non-xml datatype) will be required :



              SELECT Id, CAST(Stuff AS XML) Stuff 
              FROM OPENQUERY(DATA02, 'SELECT Id, CAST(Stuff AS nvarchar(max)) Stuff
              FROM T_B') T_B;



            Note that the bug was first reported in SQL Server 2005, and remains unfixed in SQL Server 2014.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 24 '17 at 14:40

























            answered Jan 21 '13 at 15:36









            AakashMAakashM

            52.9k13127172




            52.9k13127172








            • 4





              You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

              – Aaron Bertrand
              Jan 21 '13 at 15:37











            • when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

              – RMiranda
              Jun 20 '13 at 0:21






            • 1





              @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

              – AakashM
              Jul 3 '13 at 16:46











            • such a simple solution! Thanks

              – melodiouscode
              Oct 26 '16 at 7:42














            • 4





              You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

              – Aaron Bertrand
              Jan 21 '13 at 15:37











            • when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

              – RMiranda
              Jun 20 '13 at 0:21






            • 1





              @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

              – AakashM
              Jul 3 '13 at 16:46











            • such a simple solution! Thanks

              – melodiouscode
              Oct 26 '16 at 7:42








            4




            4





            You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

            – Aaron Bertrand
            Jan 21 '13 at 15:37





            You could also create a stored procedure on DATA02 and call the stored procedure remotely instead of trying to run the query remotely.

            – Aaron Bertrand
            Jan 21 '13 at 15:37













            when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

            – RMiranda
            Jun 20 '13 at 0:21





            when i create the view on the local DB, from the select id of the remote DB table, i get the same error...SQL SERVER 2008 R2 here

            – RMiranda
            Jun 20 '13 at 0:21




            1




            1





            @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

            – AakashM
            Jul 3 '13 at 16:46





            @RMiranda the view has to be created on the remote DB, then from the local DB you should be able to query that view through the link.

            – AakashM
            Jul 3 '13 at 16:46













            such a simple solution! Thanks

            – melodiouscode
            Oct 26 '16 at 7:42





            such a simple solution! Thanks

            – melodiouscode
            Oct 26 '16 at 7:42













            9














            Try this:




            • Create a view on the source side with xml cast to nvarchar(max):


            CREATE VIEW vXMLTest
            AS
            SELECT cast(Stuff as nvarchar(max)) as STUFF FROM T_B




            • You can select it on the destination side with cast to xml


            SELECT Cast(Stuff as XML) as Stuff
            FROM OPENQUERY(DATA02, 'SELECT Stuff FROM vXMLTest')



            This solution works for me in 2008R2.






            share|improve this answer





















            • 1





              Excellent idea! Works great.

              – NealWalters
              Mar 20 '14 at 16:43
















            9














            Try this:




            • Create a view on the source side with xml cast to nvarchar(max):


            CREATE VIEW vXMLTest
            AS
            SELECT cast(Stuff as nvarchar(max)) as STUFF FROM T_B




            • You can select it on the destination side with cast to xml


            SELECT Cast(Stuff as XML) as Stuff
            FROM OPENQUERY(DATA02, 'SELECT Stuff FROM vXMLTest')



            This solution works for me in 2008R2.






            share|improve this answer





















            • 1





              Excellent idea! Works great.

              – NealWalters
              Mar 20 '14 at 16:43














            9












            9








            9







            Try this:




            • Create a view on the source side with xml cast to nvarchar(max):


            CREATE VIEW vXMLTest
            AS
            SELECT cast(Stuff as nvarchar(max)) as STUFF FROM T_B




            • You can select it on the destination side with cast to xml


            SELECT Cast(Stuff as XML) as Stuff
            FROM OPENQUERY(DATA02, 'SELECT Stuff FROM vXMLTest')



            This solution works for me in 2008R2.






            share|improve this answer















            Try this:




            • Create a view on the source side with xml cast to nvarchar(max):


            CREATE VIEW vXMLTest
            AS
            SELECT cast(Stuff as nvarchar(max)) as STUFF FROM T_B




            • You can select it on the destination side with cast to xml


            SELECT Cast(Stuff as XML) as Stuff
            FROM OPENQUERY(DATA02, 'SELECT Stuff FROM vXMLTest')



            This solution works for me in 2008R2.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 25 '13 at 7:45

























            answered Jul 25 '13 at 7:40









            Csaba MolnárCsaba Molnár

            9112




            9112








            • 1





              Excellent idea! Works great.

              – NealWalters
              Mar 20 '14 at 16:43














            • 1





              Excellent idea! Works great.

              – NealWalters
              Mar 20 '14 at 16:43








            1




            1





            Excellent idea! Works great.

            – NealWalters
            Mar 20 '14 at 16:43





            Excellent idea! Works great.

            – NealWalters
            Mar 20 '14 at 16:43











            1














            I found another way of doing this:




            1. Create a Linked Server on DATA01, DATA02 or even some third server (could be local).

            2. Execute your query using EXEC [linked_server].[sp_sqlexecute].


            Why I prefer this method over creating Views or using OPENQUERY?




            1. This method does not require to have any permissions on the Linked Server (you don't need to create Views).

            2. You could pass a parameters to your query using the sp_sqlexecute syntax (find more information about sp_sqlexecute here). For OPENQUERY you must pass a STRING or TEXT_LEX, witch means that all the values must be typed right into this function.


            Here is the example:



            DECLARE @UserID UNIQUEIDENTIFIER = ''

            DECLARE @SearchForUserParams NVARCHAR(MAX) = N'@UserID UNIQUEIDENTIFIER';
            DECLARE @SearchForUserQuery NVARCHAR(MAX) =
            N'SELECT
            UserID,
            Username,
            Email,
            CONCART(NVARCHAR(MAX), UserDataXml) AS UserDataXml
            FROM User
            WHERE UserID = @UserID
            '

            EXEC [linked_server].[dbo].[sp_executesql]
            @SearchForUserQuery,
            @SearchForUserParams,
            @UserID = @UserID





            share|improve this answer






























              1














              I found another way of doing this:




              1. Create a Linked Server on DATA01, DATA02 or even some third server (could be local).

              2. Execute your query using EXEC [linked_server].[sp_sqlexecute].


              Why I prefer this method over creating Views or using OPENQUERY?




              1. This method does not require to have any permissions on the Linked Server (you don't need to create Views).

              2. You could pass a parameters to your query using the sp_sqlexecute syntax (find more information about sp_sqlexecute here). For OPENQUERY you must pass a STRING or TEXT_LEX, witch means that all the values must be typed right into this function.


              Here is the example:



              DECLARE @UserID UNIQUEIDENTIFIER = ''

              DECLARE @SearchForUserParams NVARCHAR(MAX) = N'@UserID UNIQUEIDENTIFIER';
              DECLARE @SearchForUserQuery NVARCHAR(MAX) =
              N'SELECT
              UserID,
              Username,
              Email,
              CONCART(NVARCHAR(MAX), UserDataXml) AS UserDataXml
              FROM User
              WHERE UserID = @UserID
              '

              EXEC [linked_server].[dbo].[sp_executesql]
              @SearchForUserQuery,
              @SearchForUserParams,
              @UserID = @UserID





              share|improve this answer




























                1












                1








                1







                I found another way of doing this:




                1. Create a Linked Server on DATA01, DATA02 or even some third server (could be local).

                2. Execute your query using EXEC [linked_server].[sp_sqlexecute].


                Why I prefer this method over creating Views or using OPENQUERY?




                1. This method does not require to have any permissions on the Linked Server (you don't need to create Views).

                2. You could pass a parameters to your query using the sp_sqlexecute syntax (find more information about sp_sqlexecute here). For OPENQUERY you must pass a STRING or TEXT_LEX, witch means that all the values must be typed right into this function.


                Here is the example:



                DECLARE @UserID UNIQUEIDENTIFIER = ''

                DECLARE @SearchForUserParams NVARCHAR(MAX) = N'@UserID UNIQUEIDENTIFIER';
                DECLARE @SearchForUserQuery NVARCHAR(MAX) =
                N'SELECT
                UserID,
                Username,
                Email,
                CONCART(NVARCHAR(MAX), UserDataXml) AS UserDataXml
                FROM User
                WHERE UserID = @UserID
                '

                EXEC [linked_server].[dbo].[sp_executesql]
                @SearchForUserQuery,
                @SearchForUserParams,
                @UserID = @UserID





                share|improve this answer















                I found another way of doing this:




                1. Create a Linked Server on DATA01, DATA02 or even some third server (could be local).

                2. Execute your query using EXEC [linked_server].[sp_sqlexecute].


                Why I prefer this method over creating Views or using OPENQUERY?




                1. This method does not require to have any permissions on the Linked Server (you don't need to create Views).

                2. You could pass a parameters to your query using the sp_sqlexecute syntax (find more information about sp_sqlexecute here). For OPENQUERY you must pass a STRING or TEXT_LEX, witch means that all the values must be typed right into this function.


                Here is the example:



                DECLARE @UserID UNIQUEIDENTIFIER = ''

                DECLARE @SearchForUserParams NVARCHAR(MAX) = N'@UserID UNIQUEIDENTIFIER';
                DECLARE @SearchForUserQuery NVARCHAR(MAX) =
                N'SELECT
                UserID,
                Username,
                Email,
                CONCART(NVARCHAR(MAX), UserDataXml) AS UserDataXml
                FROM User
                WHERE UserID = @UserID
                '

                EXEC [linked_server].[dbo].[sp_executesql]
                @SearchForUserQuery,
                @SearchForUserParams,
                @UserID = @UserID






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 21:04

























                answered Nov 21 '18 at 20:57









                semptrasemptra

                113




                113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14442039%2fwhy-do-i-get-the-error-xml-data-type-is-not-supported-in-distributed-queries-w%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?