SqlDataAdapter.FillSchema not getting DataTable on SQL 2017
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
add a comment |
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
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
add a comment |
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
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
.net sql-server tsql sql-server-2017
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 4 '18 at 13:09
Jeroen MostertJeroen Mostert
18.1k2455
18.1k2455
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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