zoukankan      html  css  js  c++  java
  • [原创]把SQL的动态查询改成SQL查询

    /*
    作者:阿牛(牛昆亮) QQ:273352165 MSN:niukl@hotmail.com

    声明:可以免费使用,请您保留此信息
        如果您有什么改过,记得告诉我!
    */
    在我们使用存储过程的时候,有时为了组合查询条件,不得不使用动态查询。比如下面的代码:

    create proc usp_search
    @city int
    as
    begin
        
    declare @sql varchar(8000)
        
    set @sql = N'select * from TestTable where 1=1 '
        
    if(@city <> -1)
            
    set @sql = @sql + ' and cityId = @city '
        
    exec sp_execute_sql @sql, N'@city int'@city
    end
    go

          如果我们不用动态SQL,则可以改成下面的存储过程:

    create proc usp_search
    @city int
    as
    begin
        select * from TestTable where 1=1 and (@city = -1 or cityId = @city)
    end
    go
         
    QQ:273352165 evlon#126.com 转载请注明出处。
  • 相关阅读:
    你好,明天
    一句话实现星级评价
    react路由
    react插件包
    react 组件列表
    css小demo
    react的项目坑
    配置react-sass
    node-sass下载失败 关于webpack
    react render
  • 原文地址:https://www.cnblogs.com/evlon/p/388875.html
Copyright © 2011-2022 走看看