My stored procedure
Alter PROCEDURE [dbo].[SP_AffniGetMainReports]
@startdate datetime,
@enddate datetime,
@devices_count int,
@devices_id varchar(max)
AS
BEGIN
SET NOCOUNT ON;
declare @affni table (id int,datetime datetime,KWH_LM float,DEVICE_ID int)
while @startdate <=@enddate
begin
insert into @affni select top (@devices_count) id ,datetime ,KWH_LM ,DEVICE_ID
from affni_eng_tracking where
datetime between @startdate and @enddate and DEVICE_ID in (@devices_id )
end
select * from @affni
end
My inputs will be
EXEC @return_value = [dbo].[SP_AffniGetMainReports]
@startdate = N'2019-08-01 12:00',
@enddate = N'2019-08-06 12:59',
@devices_id = '101,102'
GO
It gives me an empty result. If I give input as @devices_id = 101 instead of @devices_id = '101,102' it gives the results under 101. I need to get combined result for two or more result ids. And if I manually set the stored procedures query to
insert into @affni select top (@devices_count) id ,datetime ,KWH_LM ,DEVICE_ID
from affni_eng_tracking where
datetime between @startdate and @enddate and DEVICE_ID in (101,102)
It gives me the result associated with 101 and 102.
I have tried dynamic querying but it takes about 40sec to execute the query. This above static query return data within 1sec.
Can anyone help me out to generate query to place the @devices_id as an Integer within the query