SqlDataAdapter.FillSchema not getting DataTable on SQL 2017












1















I don't know why, but when I'm executing that code on MSSQL 2012 and 2015 everything works fine (I'm receving the DataTable's schema into the declared DataSet), while when I'm doing it on MSSQL 2017 - FillSchema puts NOTHING to DataSet.



DataSet.Tables.Count is equal 0.



Here is a sample code below:



var da = new SqlDataAdapter();
var cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
try
{
cn.Open();
var sql = "SELECT * FROM MyTable T" +
"WHERE T.MyTableId=123 AND " +
"EXISTS" +
"(" +
" SELECT * FROM dbo.MyFunction() CTX WHERE ISNULL(T.MyOtherId, -1) = CTX.MyOtherId" +
")";
da.SelectCommand = new SqlCommand(sql, cn);

var ds = new DataSet();
da.FillSchema(ds, SchemaType.Source);
//There no table in the DataSet!
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
finally
{
cn.Close();
}


Something has changed in MS SQL 2017? Or maybe I'm doing something wrong?



EDIT 1: Problem disappears when I remove the EXISTS condition from the query. I think calling the function might be the reason.



EDIT 2: When I simplify the query to:



var sql = "SELECT * FROM dbo.MyFunction()";


Still not working... You may wondering what MyFunction in SQL do. It's a Multi-Statement Table function:



CREATE FUNCTION dbo.MyFunction ()
RETURNS @RESULT TABLE
(
ID BIGINT NOT NULL,
NAME VARCHAR(50),
CODE VARCHAR(50)
)
AS
BEGIN
INSERT INTO @RESULT VALUES(1, 'Name', 'NAME')
RETURN
END
GO


When I rewrite Multi-Statement Table Function to Inline-Statement it works.
I know it's not the best way in .NET to receive schema... but any ideas why the multi-statement functions not working?










share|improve this question

























  • Does the user mentioned in the connection string have rights to execute MyFunction?

    – Razvan Socol
    Nov 21 '18 at 5:53











  • Razvan, yes the user in connection string has rights to execute function :)

    – Nexor
    Nov 21 '18 at 6:49
















1















I don't know why, but when I'm executing that code on MSSQL 2012 and 2015 everything works fine (I'm receving the DataTable's schema into the declared DataSet), while when I'm doing it on MSSQL 2017 - FillSchema puts NOTHING to DataSet.



DataSet.Tables.Count is equal 0.



Here is a sample code below:



var da = new SqlDataAdapter();
var cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
try
{
cn.Open();
var sql = "SELECT * FROM MyTable T" +
"WHERE T.MyTableId=123 AND " +
"EXISTS" +
"(" +
" SELECT * FROM dbo.MyFunction() CTX WHERE ISNULL(T.MyOtherId, -1) = CTX.MyOtherId" +
")";
da.SelectCommand = new SqlCommand(sql, cn);

var ds = new DataSet();
da.FillSchema(ds, SchemaType.Source);
//There no table in the DataSet!
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
finally
{
cn.Close();
}


Something has changed in MS SQL 2017? Or maybe I'm doing something wrong?



EDIT 1: Problem disappears when I remove the EXISTS condition from the query. I think calling the function might be the reason.



EDIT 2: When I simplify the query to:



var sql = "SELECT * FROM dbo.MyFunction()";


Still not working... You may wondering what MyFunction in SQL do. It's a Multi-Statement Table function:



CREATE FUNCTION dbo.MyFunction ()
RETURNS @RESULT TABLE
(
ID BIGINT NOT NULL,
NAME VARCHAR(50),
CODE VARCHAR(50)
)
AS
BEGIN
INSERT INTO @RESULT VALUES(1, 'Name', 'NAME')
RETURN
END
GO


When I rewrite Multi-Statement Table Function to Inline-Statement it works.
I know it's not the best way in .NET to receive schema... but any ideas why the multi-statement functions not working?










share|improve this question

























  • Does the user mentioned in the connection string have rights to execute MyFunction?

    – Razvan Socol
    Nov 21 '18 at 5:53











  • Razvan, yes the user in connection string has rights to execute function :)

    – Nexor
    Nov 21 '18 at 6:49














1












1








1








I don't know why, but when I'm executing that code on MSSQL 2012 and 2015 everything works fine (I'm receving the DataTable's schema into the declared DataSet), while when I'm doing it on MSSQL 2017 - FillSchema puts NOTHING to DataSet.



DataSet.Tables.Count is equal 0.



Here is a sample code below:



var da = new SqlDataAdapter();
var cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
try
{
cn.Open();
var sql = "SELECT * FROM MyTable T" +
"WHERE T.MyTableId=123 AND " +
"EXISTS" +
"(" +
" SELECT * FROM dbo.MyFunction() CTX WHERE ISNULL(T.MyOtherId, -1) = CTX.MyOtherId" +
")";
da.SelectCommand = new SqlCommand(sql, cn);

var ds = new DataSet();
da.FillSchema(ds, SchemaType.Source);
//There no table in the DataSet!
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
finally
{
cn.Close();
}


Something has changed in MS SQL 2017? Or maybe I'm doing something wrong?



EDIT 1: Problem disappears when I remove the EXISTS condition from the query. I think calling the function might be the reason.



EDIT 2: When I simplify the query to:



var sql = "SELECT * FROM dbo.MyFunction()";


Still not working... You may wondering what MyFunction in SQL do. It's a Multi-Statement Table function:



CREATE FUNCTION dbo.MyFunction ()
RETURNS @RESULT TABLE
(
ID BIGINT NOT NULL,
NAME VARCHAR(50),
CODE VARCHAR(50)
)
AS
BEGIN
INSERT INTO @RESULT VALUES(1, 'Name', 'NAME')
RETURN
END
GO


When I rewrite Multi-Statement Table Function to Inline-Statement it works.
I know it's not the best way in .NET to receive schema... but any ideas why the multi-statement functions not working?










share|improve this question
















I don't know why, but when I'm executing that code on MSSQL 2012 and 2015 everything works fine (I'm receving the DataTable's schema into the declared DataSet), while when I'm doing it on MSSQL 2017 - FillSchema puts NOTHING to DataSet.



DataSet.Tables.Count is equal 0.



Here is a sample code below:



var da = new SqlDataAdapter();
var cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString);
try
{
cn.Open();
var sql = "SELECT * FROM MyTable T" +
"WHERE T.MyTableId=123 AND " +
"EXISTS" +
"(" +
" SELECT * FROM dbo.MyFunction() CTX WHERE ISNULL(T.MyOtherId, -1) = CTX.MyOtherId" +
")";
da.SelectCommand = new SqlCommand(sql, cn);

var ds = new DataSet();
da.FillSchema(ds, SchemaType.Source);
//There no table in the DataSet!
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
finally
{
cn.Close();
}


Something has changed in MS SQL 2017? Or maybe I'm doing something wrong?



EDIT 1: Problem disappears when I remove the EXISTS condition from the query. I think calling the function might be the reason.



EDIT 2: When I simplify the query to:



var sql = "SELECT * FROM dbo.MyFunction()";


Still not working... You may wondering what MyFunction in SQL do. It's a Multi-Statement Table function:



CREATE FUNCTION dbo.MyFunction ()
RETURNS @RESULT TABLE
(
ID BIGINT NOT NULL,
NAME VARCHAR(50),
CODE VARCHAR(50)
)
AS
BEGIN
INSERT INTO @RESULT VALUES(1, 'Name', 'NAME')
RETURN
END
GO


When I rewrite Multi-Statement Table Function to Inline-Statement it works.
I know it's not the best way in .NET to receive schema... but any ideas why the multi-statement functions not working?







.net sql-server tsql sql-server-2017






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 11:56







Nexor

















asked Nov 20 '18 at 14:10









NexorNexor

485




485













  • Does the user mentioned in the connection string have rights to execute MyFunction?

    – Razvan Socol
    Nov 21 '18 at 5:53











  • Razvan, yes the user in connection string has rights to execute function :)

    – Nexor
    Nov 21 '18 at 6:49



















  • Does the user mentioned in the connection string have rights to execute MyFunction?

    – Razvan Socol
    Nov 21 '18 at 5:53











  • Razvan, yes the user in connection string has rights to execute function :)

    – Nexor
    Nov 21 '18 at 6:49

















Does the user mentioned in the connection string have rights to execute MyFunction?

– Razvan Socol
Nov 21 '18 at 5:53





Does the user mentioned in the connection string have rights to execute MyFunction?

– Razvan Socol
Nov 21 '18 at 5:53













Razvan, yes the user in connection string has rights to execute function :)

– Nexor
Nov 21 '18 at 6:49





Razvan, yes the user in connection string has rights to execute function :)

– Nexor
Nov 21 '18 at 6:49












1 Answer
1






active

oldest

votes


















1














This happens because SQL Server 2017 does not return metadata when SELECT * FROM dbo.MyFunction() is executed under SET FMTONLY ON, which is still the method used by ADO.NET to retrieve metadata. It's been deprecated, and sp_describe_first_result_set does work correctly for this case, but that's still no reason for SQL Server 2017 to fail. This has been reported as a bug.



Speculation: this bug may have been introduced as a side effect of the new interleaved execution feature, which changes the way these functions are executed. This is reinforced by the observation that it disappears if the database compatibility level is dropped down to 130, which disables this (among other things). I'm not aware of a trace flag that disables only interleaved execution, which could be used to test this.



There is a workaround, of sorts: set fmtonly on; exec ('select * from dbo.MyFunction()'); set fmtonly off will return metadata even under compat level 140, so fetching metadata of a query using EXEC will still work. In the above,



var sql = "EXEC ('SELECT * FROM dbo.MyFunction()')";


should return results.






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%2f53394884%2fsqldataadapter-fillschema-not-getting-datatable-on-sql-2017%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This happens because SQL Server 2017 does not return metadata when SELECT * FROM dbo.MyFunction() is executed under SET FMTONLY ON, which is still the method used by ADO.NET to retrieve metadata. It's been deprecated, and sp_describe_first_result_set does work correctly for this case, but that's still no reason for SQL Server 2017 to fail. This has been reported as a bug.



    Speculation: this bug may have been introduced as a side effect of the new interleaved execution feature, which changes the way these functions are executed. This is reinforced by the observation that it disappears if the database compatibility level is dropped down to 130, which disables this (among other things). I'm not aware of a trace flag that disables only interleaved execution, which could be used to test this.



    There is a workaround, of sorts: set fmtonly on; exec ('select * from dbo.MyFunction()'); set fmtonly off will return metadata even under compat level 140, so fetching metadata of a query using EXEC will still work. In the above,



    var sql = "EXEC ('SELECT * FROM dbo.MyFunction()')";


    should return results.






    share|improve this answer




























      1














      This happens because SQL Server 2017 does not return metadata when SELECT * FROM dbo.MyFunction() is executed under SET FMTONLY ON, which is still the method used by ADO.NET to retrieve metadata. It's been deprecated, and sp_describe_first_result_set does work correctly for this case, but that's still no reason for SQL Server 2017 to fail. This has been reported as a bug.



      Speculation: this bug may have been introduced as a side effect of the new interleaved execution feature, which changes the way these functions are executed. This is reinforced by the observation that it disappears if the database compatibility level is dropped down to 130, which disables this (among other things). I'm not aware of a trace flag that disables only interleaved execution, which could be used to test this.



      There is a workaround, of sorts: set fmtonly on; exec ('select * from dbo.MyFunction()'); set fmtonly off will return metadata even under compat level 140, so fetching metadata of a query using EXEC will still work. In the above,



      var sql = "EXEC ('SELECT * FROM dbo.MyFunction()')";


      should return results.






      share|improve this answer


























        1












        1








        1







        This happens because SQL Server 2017 does not return metadata when SELECT * FROM dbo.MyFunction() is executed under SET FMTONLY ON, which is still the method used by ADO.NET to retrieve metadata. It's been deprecated, and sp_describe_first_result_set does work correctly for this case, but that's still no reason for SQL Server 2017 to fail. This has been reported as a bug.



        Speculation: this bug may have been introduced as a side effect of the new interleaved execution feature, which changes the way these functions are executed. This is reinforced by the observation that it disappears if the database compatibility level is dropped down to 130, which disables this (among other things). I'm not aware of a trace flag that disables only interleaved execution, which could be used to test this.



        There is a workaround, of sorts: set fmtonly on; exec ('select * from dbo.MyFunction()'); set fmtonly off will return metadata even under compat level 140, so fetching metadata of a query using EXEC will still work. In the above,



        var sql = "EXEC ('SELECT * FROM dbo.MyFunction()')";


        should return results.






        share|improve this answer













        This happens because SQL Server 2017 does not return metadata when SELECT * FROM dbo.MyFunction() is executed under SET FMTONLY ON, which is still the method used by ADO.NET to retrieve metadata. It's been deprecated, and sp_describe_first_result_set does work correctly for this case, but that's still no reason for SQL Server 2017 to fail. This has been reported as a bug.



        Speculation: this bug may have been introduced as a side effect of the new interleaved execution feature, which changes the way these functions are executed. This is reinforced by the observation that it disappears if the database compatibility level is dropped down to 130, which disables this (among other things). I'm not aware of a trace flag that disables only interleaved execution, which could be used to test this.



        There is a workaround, of sorts: set fmtonly on; exec ('select * from dbo.MyFunction()'); set fmtonly off will return metadata even under compat level 140, so fetching metadata of a query using EXEC will still work. In the above,



        var sql = "EXEC ('SELECT * FROM dbo.MyFunction()')";


        should return results.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 '18 at 13:09









        Jeroen MostertJeroen Mostert

        18.1k2455




        18.1k2455
































            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%2f53394884%2fsqldataadapter-fillschema-not-getting-datatable-on-sql-2017%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)