February 28, 2010 at 3:38 am
I have a stored procedure which expects a few parameters, all have a default value. The procedure returns a table, that is it ends with SELECT ... FROM. I call this procedure from C# with a DbCommand of type set to stored procedure. The profiler show the following command is passed:
exec sp_executesql N'[Analysis].[pInterfaceStatistic]',N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)',@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
The output is an empty table.
When I run the following command from within ssms
exec [Analysis].[pInterfaceStatistic] @PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
the output is a table with a few rows, as expected.
I added a few print statements to the procedure and it turns out, that the values are not passed to the procedure; it is run with all parameters set to their default values.
I also tried to run sp_executesql as administrator but to no avail, the parameters are not passed.
What is wrong here?
February 28, 2010 at 4:13 am
ma-516002 (2/28/2010)
I have a stored procedure which expects a few parameters, all have a default value. The procedure returns a table, that is it ends with SELECT ... FROM. I call this procedure from C# with a DbCommand of type set to stored procedure. The profiler show the following command is passed:exec sp_executesql N'[Analysis].[pInterfaceStatistic]',N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)',@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
The output is an empty table.
When I run the following command from within ssms
exec [Analysis].[pInterfaceStatistic] @PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
the output is a table with a few rows, as expected.
I added a few print statements to the procedure and it turns out, that the values are not passed to the procedure; it is run with all parameters set to their default values.
I also tried to run sp_executesql as administrator but to no avail, the parameters are not passed.
What is wrong here?
You're passing this command:
exec sp_executesql N'[Analysis].[pInterfaceStatistic]',N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)',@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
Why don't you pass this command?
exec [Analysis].[pInterfaceStatistic] @PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
Or better still:
[exec Analysis.pInterfaceStatistic @PeriodStartName=N'Y2010', @PeriodEndName=N'Y2010', @PeriodType=N'Y']
- less chance of a clash between string delimiters and object identifiers.
In any case, I can't see why you need sp_executesql here.
Cheers
ChrisM
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 28, 2010 at 4:20 am
That statement is generated by System.Data.Common.DbCommand, you cannot modify it.
February 28, 2010 at 5:11 am
ma-516002 (2/28/2010)
That statement is generated by System.Data.Common.DbCommand, you cannot modify it.
Thanks for the extra info.
I think the problem is with setting up the command string in your interface code. SQL Server hasn't been told where to apply the declared parameters. It has them but doesn't know what to do with them.
This works:
execute sp_executesql N'Analysis.pInterfaceStatistic @PeriodStartName, @PeriodEndName, @PeriodType',N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)',
@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
because the parameter usage is defined - in the stmt argument of sp_executesql, right where they're expected to be.
Cheers
ChrisM
Edit: Changed schema back to OP's from test environment
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 28, 2010 at 6:16 am
No, your suggested command has the same result as the auto generated one, an empty table.
This looks like there is something missing to sp_executesql or the target procedure in terms of like permissions or something.
February 28, 2010 at 7:03 am
ma-516002 (2/28/2010)
No, your suggested command has the same result as the auto generated one, an empty table.This looks like there is something missing to sp_executesql or the target procedure in terms of like permissions or something.
It works in my little test here. The original syntax did not, for the reason already explained. If it were permissions, you'd get an error, not an empty table.
Can you post the command (the one I suggested) that you see in profiler, please?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 28, 2010 at 7:23 am
ma-516002 (2/28/2010)
No, your suggested command has the same result as the auto generated one, an empty table.This looks like there is something missing to sp_executesql or the target procedure in terms of like permissions or something.
Also, have you tried running the modified command in SSMS?
exec sp_executesql N'[Analysis].[pInterfaceStatistic] @PeriodStartName, @PeriodEndName, @PeriodType',
N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)',
@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 28, 2010 at 7:24 am
I'm not sure what you mean. The command I got from the profiler is the first one in my first post. As I said, this command is generated by the .Net runtime and this is for good reason not modifyable. I used the profiler in the first place to check wether a command is sent to the database at all. I don't care about the command itself, because I use the offical MS api to access a database object so I must not know about how the runtime retrieves the data I ask for, I must rely on the runtime to do it. At least, that is the idea behind an api, isn't it. I'll dig deeper into this, see what I can find.
February 28, 2010 at 7:56 am
Don't know what happened there. I recreated the database and everything works now 🙂 (Gremlins?)
February 28, 2010 at 8:17 am
{Edit} Sorry... posted an incorrect observation.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 28, 2010 at 8:53 am
From your original post:
The command I got from the profiler is the first one in my first post. As I said, this command is generated by the .Net runtime and this is for good reason not modifyable.
Based on the below simplified test cases, the command being sent to SQL Server is incorrect as the @Stmt parameter does not include the parameters of the inner stored procedure.
Original statement:
exec sp_executesql N'[Analysis].[pInterfaceStatistic]'
, N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)'
,@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
Required statement:
exec sp_executesql N'[Analysis].[pInterfaceStatistic] @PeriodStartName,@PeriodEndName,@PeriodType'
, N'@PeriodStartName nvarchar(5),@PeriodEndName nvarchar(5),@PeriodType nvarchar(1)'
,@PeriodStartName=N'Y2010',@PeriodEndName=N'Y2010',@PeriodType=N'Y'
See the below code for a reproducible test harness, which shows that:
When @stmt is "EXECUTE dbo.test_executesql @MyParm = @MyParm", the parameter is available to the sp.
When @stmt is "EXECUTE dbo.test_executesql", the parameter is NOT available to the sp.
Outputs of the test cases are:
with paramater EXECUTE dbo.test_executesql @MyParm = @MyParm
MyParmValue
--------------------------
@MyParm value is hello
without paramater EXECUTE dbo.test_executesql
MyParmValue
------------------------
@MyParm is NULL
-- Set "results to text" - hit CTRL-T
USE tempdb
GO
IFOBJECT_ID('dbo.test_executesql') IS NOT NULL
DROP PROCEDURE dbo.test_executesql
GO
CREATE PROCEDURE dbo.test_executesql
(@MyParmVARCHAR(255)= NULL
)
AS
SELECTCASE WHEN @MyParm IS NULL THEN '@MyParm is NULL'
ELSE '@MyParm value is ' + @MyParm
END AS MyParmValue
GO
SETNOCOUNT ON;
DECLARE @rcINT
,@MyParmVARCHAR(255)
,@StmtNVARCHAR(MAX)
,@paramsNVARCHAR(MAX)
SET@MyParm=' hello '
SET@params=N'@MyParmvarchar(255)'
SET@Stmt= N'EXECUTE dbo.test_executesql @MyParm = @MyParm';
PRINT'with paramater ' + @Stmt
EXEC@rc = sp_executesql @stmt = @Stmt , @params = @params , @MyParm = @MyParm
SET@Stmt= N'EXECUTE dbo.test_executesql';
PRINT'without paramater ' + @Stmt
EXEC@rc = sp_executesql @stmt = @Stmt , @params = @params , @MyParm = @MyParm
GO
SQL = Scarcely Qualifies as a Language
February 28, 2010 at 10:28 am
ma-516002 (2/28/2010)
Don't know what happened there. I recreated the database and everything works now 🙂 (Gremlins?)
Well done - but your observation that rebuilding the db solved the problem is almost certainly coincidental.
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
February 28, 2010 at 9:36 pm
Probably just a PEBKAC or ID-10T error with your instance of SQL Server.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply