Why I get error when call my stored procedure using sp_executesql?
Doesn't works.
exec sp_executesql N'sp_clnt_regional_experts_territories',
N'@action nvarchar(3), @regional_expert_id int,@region_id int,@territory_id int',
@action = N'SEL',
@regional_expert_id = 2,
@region_id = -1,
@territory_id = -1
Procedure or function 'sp_clnt_regional_experts_territories' expects parameter '@action', which was not supplied.
Works fine:
EXEC sp_clnt_regional_experts_territories
@action = N'SEL',
@regional_expert_id = 2,
@region_id = -1,
@territory_id = -1
Where the stored proc is:
ALTER PROCEDURE [dbo].[sp_clnt_regional_experts_territories]
@action NVARCHAR(3),
@regional_expert_id INT = -1,
@region_id INT = -1,
@territory_id INT = -1
Your sp_executesql is wrong .You need to format it like the one below
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
DECLARE @action nvarchar(3)
DECLARE @regional_expert_id int
DECLARE @region_id int
DECLARE @territory_id INT
Set @SQLString=N'EXEC sp_clnt_regional_experts_territories @action,
@regional_expert_id ,@region_id ,@territory_id'
Set @ParmDefinition='@action NVARCHAR(3),
@regional_expert_id INT ,
@region_id INT ,
@territory_id INT '
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@action=N'SEL',
@regional_expert_id=2,
@region_id=-1,
@territory_id=-1
Instead of directly entering everything in one statement ,dissecting your code into different parts helps you find your problems easily .
the error is because you didn't include the parameters right after your stored procedure name, which is required by sp_executesql
exec sp_executesql N'sp_clnt_regional_experts_territories @action,@regional_expert_id ,@region_id ,@territory_id',
N'@action nvarchar(3), @regional_expert_id int,@region_id int,@territory_id int',
@action = N'SEL',
@regional_expert_id = 2,
@region_id = -1,
@territory_id = -1
sp_executesql
says of @params
:
Is one string that contains the definitions of all parameters that have been embedded in @stmt .
(Emphasis added)
So, any parameters you identify there must also be mentioned in @stmt
, as the other answers have pointed out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.