Saturday, September 28, 2013

query with where condition

Hi,


Is this what you are looking for? Getting the where clause from tblExpressions and executing a dynamic sql string.



declare @tblExpressions table (id int primary key, predicate varchar(100))
declare @expression nvarchar(300) = ''

insert into @tblExpressions (id, predicate)
values (1, 'a = 1'), (2, 'b = 2'), (3, 'c = 3')

set @expression = 'select a, b, c from tblX where '

select
@expression = @expression + te.predicate + ' and '
from @tblExpressions te

select @expression = left (@expression, len(@expression) - 4)

select @expression

exec sp_executesql @expression



HTH




Regards, Dean Savović


No comments:

Post a Comment