Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXEC sp_executesql with multiple parameters

How to pass the parameters to the EXEC sp_executesql statement correctly?

This is what I have now, but i'm getting errors:

alter PROCEDURE [dbo].[usp_getReceivedCases]     -- Add the parameters for the stored procedure here     @LabID int,     @RequestTypeID varchar(max),     @BeginDate date,     @EndDate date AS BEGIN     -- SET NOCOUNT ON added to prevent extra result sets from     -- interfering with SELECT statements.     SET NOCOUNT ON;   declare @statement nvarchar(4000)  set @statement = N'select   SentToLab, FROM     dbo.vEmailSent WHERE     SentToLab_ID=@LabID and convert(date,DateSent) >= @BeginDate  and CONVERT(date, datesent) <= @EndDate and RequestType_ID in ( @RequestTypeID )  EXEC sp_executesql  @statement,N'@LabID int',  @LabID, N'@BeginDate date', @BeginDate,N'@EndDate date', @EndDate, @RequestTypeID=@RequestTypeID  END 

RequestTypeID is a comma delimited list of integers, like so: "1,2,3,4,5"

here is my try #2, also unsuccessful

declare @statement nvarchar(4000)  SET @statement =' select    SentToLab_ID  FROM     dbo.vEmailSent WHERE      SentToLab_ID='+@LabID+' and convert(date,DateSent) >= '+@BeginDate +' and CONVERT(date, datesent) <= '+@EndDate+' and RequestType_ID in ('+ @RequestTypeID+' ) group by FileStream_ID, SentToLab_ID'   EXEC(@statement) 

Operand type clash: date is incompatible with int

like image 713
Madam Zu Zu Avatar asked Feb 12 '15 15:02

Madam Zu Zu


People also ask

What is the difference between EXEC vs Sp_executesql?

EXEC : EXEC/Execute is used to execute any stored procedure or character string. Mostly it is used to execute the stored procedure. 2. SP_ExecuteSQL: SP_ExecuteSQL is used to execute ad-hoc SQL statements so that they can be executed as parameterized statements.

What is EXEC Sp_executesql in SQL?

January 9, 2020 by Esat Erkec. The sp_executesql is a built-in stored procedure in SQL Server that enables to execute of the dynamically constructed SQL statements or batches. Executing the dynamically constructed SQL batches is a technique used to overcome different issues in SQL programming sometimes.

How do I execute a SQL stored procedure with parameters?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

How do you pass dynamic parameters in SQL query?

Executing dynamic SQL queries Dynamic SQL queries are those built at runtime based on one or more variable values. To execute those queries, we must concatenate them into one SQL statement and pass them as a parameter to the sp_executesql stored procedure.


1 Answers

Here is a simple example:

EXEC sp_executesql @sql, N'@p1 INT, @p2 INT, @p3 INT', @p1, @p2, @p3; 

Your call will be something like this

EXEC sp_executesql @statement, N'@LabID int, @BeginDate date, @EndDate date, @RequestTypeID varchar', @LabID, @BeginDate, @EndDate, @RequestTypeID 
like image 52
SAS Avatar answered Sep 25 '22 22:09

SAS