Dynamic creative where the condition in the SQL statement

存储架构 2017-11-15

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

    set @where += 'Upper(vendors.VENDOR_NAME) LIKE "%"+ UPPER(@Vendor_Name) +"%"'

    else if @Entity is not null
    set @where += 'AND headers.ORG_ID = @Entity'
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'.

Use this :

Declare @Where NVARCHAR(MAX) 

...... Create your Where

Set @Command = 'Select * From SEM.tblMeasureCatalog AS MC ' ;

If( @Where  '' )
   Set @Comand = @Command + ' Where ' + @Where

Execute SP_ExecuteSQL  @Command

I test this and it Worked

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!


PhalApi (π框架) – v1.4.0 发布 在五四青年节之际,PhalApi迎来了1.4.0版本更新,此次更新围绕着快速开发为开发者提供更方便的工具功能考虑,实现了更友好的调试方式,更多的工具函数...
A litle Big Moose forward It carry on testing developing day here in the Moose-Pen. I left offyester...
Hibernate to get a list of primitive integers for ... How do I get a list of downloaded videos for a certain channel? ...
译 | 如何更优雅的写出你的SQL语句 毫无疑问,编写代码是一门艺术而非科学,没有程序员可以编写出既可读又可维护的漂亮代码,即使有经验也是如此。 一般来说,当...
Download SQL patch Info Any DBA worth his salt checks for available SQL patches on a regular basis and t...