In a MS-SQL 2019 Enviroment , i have a table with stored Policy Conditions that will continuously be updated by a user.
The user will change the operators of the conditions and the values per condition, insert new rows etc.
What i am struggling with , is to be able to have a query that will match the invoiced lines with the matched policy (if matched..). The need to to handle the more operator and in the future maybe like operator is what makes it difficult to me because it's not a simple concatenation join
I tried creating a Stored Procedure that returns an SQL statement with dynamic where clause as a string, but i am stuck because i cant use it dynamically in another Function using exec
Any help is appreciated ...
Consumer data is provided :
SET ANSI_NULLS, QUOTED_IDENTIFIER, ANSI_PADDING ON;
GO
DROP TABLE IF EXISTS #Conditions
CREATE TABLE #Conditions
(
Operator_ItemType Varchar(10) ,
ItemType Varchar(15) ,
Operator_Category Varchar(10) ,
Category Varchar(50) ,
Operator_Sex Varchar(10) ,
Sex Varchar(15) ,
Operator_Discount Varchar(10) ,
Discount int ,
PolicyCode varchar(10)
);
INSERT INTO #Conditions
VALUES ('<>', '01', '=', 'GUESS ACCESSORIES MEN' , '=' , 'MALE' , NULL , NULL , 'A00'),
('=', '01', '=', 'GUESS ACCESSORIES MEN' , '=' , 'MALE' , NULL , NULL, 'A01'),
('<>','01', '=', 'GUESS ACCESSORIES MEN' , '=' , 'MALE' , '>' , 15 , 'B00'),
('=', '01', '=', 'GUESS ACCESSORIES MEN' , '=' , 'MALE' , '>', 10 , 'C00')
Select * from #Conditions
DROP TABLE IF EXISTS #InvoiceLines
CREATE TABLE #InvoiceLines
(
ItemCode Varchar(20) ,
ItemType Varchar(15) ,
Category Varchar(50) ,
Sex Varchar(15) ,
Discount int
);
INSERT INTO #InvoiceLines VALUES ('100', '01', 'GUESS ACCESSORIES MEN' , 'MALE' , NULL ),
('101', '01', 'GUESS ACCESSORIES MEN' , 'MALE' , NULL ),
('102', '01', 'GUESS ACCESSORIES MEN' , 'MALE' , NULL ),
('103', '01', 'GUESS ACCESSORIES MEN' , 'MALE' , 18 )
Select * from #InvoiceLines