I have a table with different columns. Each column contains a filter.
Course | Filter1 | Filter2 |
---|---|---|
First | and startdate > '20200101' | and active = 0 |
Second | and startdate > '20210101' | and active = -1 |
I want to dynamically create a SQL with those filter
DECLARE @filter1 as varchar(1000)
DECLARE @filter2 as varchar(1000)
DECLARE File_Cursor CURSOR FOR
SELECT filter1, filter2 FROM _table_filters
OPEN File_Cursor;
FETCH NEXT FROM File_Cursor INTO @filter1, @filter2;
WHILE @@FETCH_STATUS = 0
BEGIN
declare @sql varchar(8000) = '
select statusdate
from t_table
where 1=1
'''+@filter1+'''
'''+@filter2+'''
'
exec(@sql)
FETCH NEXT FROM File_Cursor INTO @filter1, @filter2;
END;
CLOSE File_Cursor;
DEALLOCATE File_Cursor;
However the query has filter with single quotes and I can't get it right.
select statusdate
from t_table
where 1=1
'and startdate > '20200101''
'and active=0'