Incorrect Syntax Near '0' - when using sp_executeSQL to execute stored procedure with output parameter

entity-framework-6 sp-executesql sql-server

Question

Actual Stored Procedure is

ALTER Procedure [dbo].[ApplyList] 
    @oldlist int, @username varchar(50),
    @newlist int, @errormessage varchar(2000) output
AS
BEGIN
    SET NOCOUNT ON;
    SET @errormessage = '';
END

Here is the SQL code generated by Entity Framework 6.1 and using Database First approach.

declare @p6 varchar(2000);
set @p6=NULL;
exec sp_executesql N'EXEC [dbo].[ApplyList] {0}, {1}, {2}, {3}',N'@oldlist int,@username nvarchar(50), @newlist int,@errormessage varchar(2000) output',@oldlist='20',@username=N'mk',@newlist='18',@errormessage=@p6 output
select @p6

Why I'm getting the message "Incorrect syntax near '0'." while I'm trying to execute the above query in Management Studio.

This is working if I call in the below format from Management Studio.

DECLARE @P6 nvarchar(2000)
set @P6 = null
exec ApplyList '20', 'mk', '18', @p6 output
select @p6
go
1
2
9/29/2015 4:34:02 PM

Accepted Answer

In SSMS (T-SQL) you cannot use parameter place-holders, you need to state them explicitly:

exec sp_executesql 
    N'EXEC [dbo].[ApplyList] @oldlist, @username, @newlist, @errormessage output',
    N'@oldlist int,@username nvarchar(50), @newlist int,@errormessage varchar(2000) output',
    @oldlist='20',@username=N'mk',@newlist='18',@errormessage=@p6 output
select @p6
1
9/29/2015 4:33:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow