Пример ввода (Насколько мы понимаем, мы предполагаем ваши данные):
select
* into ##demilit
from (
values
(1 ,'Ger','Ind', 100 )
,(2 ,'Ind',Null, 10 )
,(3 ,'Ger',Null, 24 )
,(4 ,'Ind','Ger', 54 )
,(5 ,'USA','Ind', 56 )
,(6 ,Null,'USA', 75 )-- NULL. But USA is three time came.
,(7 ,'USA','USA', 60 )-- same country with diff val.
,(8 ,'USA','USA', 80 )-- same country with diff val.
) demilit(Id,FromPr,ToPr,Val)
select * from ##demilit
ПРОЦЕДУРА (Вы просто используете это вместо ПРОЦЕДУРЫ):
create PROCEDURE [dbo].[get_data_Dyna]
(
@param1 varchar(max) = null,
@param2 varchar(max) = null,
@start varchar(max) = null,
@end varchar(max) = null
)
AS
begin
select * from ##demilit d
join ( --| Here We check the val btw @start and @end
select distinct s.FinalColumn StartVal --|
, e.FinalColumn EndVal --|
from dbo.WithoutDelimit (@start,',') s --| S means 'Start'
join ( --|
select * from dbo.WithoutDelimit (@end,',') --|
) e on s.id = e.id --| E means 'End'
) se --| se mean StartEnd
on d.val between se.StartVal and se.EndVal --| Here YOUR CONDITION is accomplished
where ( -- | checks whether
frompr in ( -- | column1 in @param1 or not
select FinalColumn from dbo.WithoutDelimit (@param1,',') -- | frompr means, 'column1'
) or @param1 is null -- |
)
and ( -- | checks whether
ToPr in ( -- | column2 in @param2 or not
select FinalColumn from dbo.WithoutDelimit (@param2,',') -- | frompr means, 'column2'
) or @param2 is null -- |
)
end
Звоните SP
[get_data_Dyna] null,'usa','75','100,' -- 6 row
[get_data_Dyna] 'Ind,Ger',null,'1,15','20,30' --2 and 3 rows are selected.
[get_data_Dyna] 'usa','usa','50,60','55,79'
-- 7 and 8 has same country. But due to Val, 8 has been rejected.
[get_data_Dyna] NULL,'usa','70,60','80,79'
-- 6 and 7 and 8 has been selected. Due to val condition.
Функция (Вызывается из SP):
alter function WithoutDelimit ( -- We use one Function for all conditions.
@Parameter varchar (max)
, @demilit varchar (1)
)
returns @FinalTable table (
Id int identity (1,1) -- Auto increament
, FinalColumn varchar (max) -- It returns the values as a column.
) as
begin
;with cte as -- recurrsive cte.
(
select convert (varchar (255), @Parameter + @demilit) con
, convert (varchar (255), @Parameter + @demilit) want
union all
select convert (varchar (255), stuff (con, 1, CHARINDEX (@demilit,con),'') )
, substring (con, 1, CHARINDEX (@demilit,con)-1)
from cte
where con <> ''
) insert into @FinalTable (FinalColumn)
select want from cte
where con <> want
return
end
Обратитесь к нам, если запрос требует обновления.