EXEC sp_executesql con múltiples parámetros
Cómo pasar los parámetros a laEXEC sp_executesql
declaración correctamente?
Esto es lo que tengo ahora, pero recibo errores:
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 es una lista de enteros delimitada por comas, como esta: "1,2,3,4,5"
aquí está mi intento # 2, también sin éxito
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 es incompatible con int