Monday, 30 September 2013

Building dynamic where condition in SQL statement

Building dynamic where condition in SQL statement

I want to build custom Where condition based on stored procedure inputs,
if not null then I will use them in the statement, else I will not use
them.
if @Vendor_Name is not null
begin
set @where += 'Upper(vendors.VENDOR_NAME) LIKE "%"+
UPPER(@Vendor_Name) +"%"'
end
else if @Entity is not null
begin
set @where += 'AND headers.ORG_ID = @Entity'
end
select * from table_name where @where
But I get this error
An expression of non-boolean type specified in a context where a condition
is expected, near 'set'.

No comments:

Post a Comment