RSS订阅 | 本站微信 【慢半拍的佬唐】 - 应用为王,深度分享
你的位置:首页 » 野生橙须猿 » 正文

存储过程动态查询参数

2020-7-30 21:53:25 | 作者:mLaoTang | 1个评论 | 人浏览

在存储过程参数中,传入查询参数,返回结果,非execsql拼接SQL语句,用CodeSmith生成代码可获得列名。

初始来源:“系统的存储过程sp_tables”


CREATE procedure usp_selectDemo
    @DemoId varchar(50) = null,
    @DemoName varchar(50) = null,
    @ListPrice decimal = null,
    @Quantity int = null,
    @LastUpdatedDate datetime = null,
    @LastUpdatedDateBegin datetime = null,
    @LastUpdatedDateEnd datetime = null
as

/* Powered by 江千帆(cnblogs.com) */

SELECT DemoId, DemoName, ListPrice, Quantity, LastUpdatedDate
FROM Demo
where 1=1
and (@DemoId is null or DemoId = @DemoId)
and (@DemoName is null or DemoName = @DemoName)
and (@ListPrice is null or ListPrice = @ListPrice)
and (@Quantity is null or Quantity = @Quantity)
and (@LastUpdatedDate is null or LastUpdatedDate = @LastUpdatedDate)
and (@LastUpdatedDateBegin is null or LastUpdatedDate >= @LastUpdatedDateBegin)
and (@LastUpdatedDateEnd is null or LastUpdatedDate < @LastUpdatedDateEnd)


GO







参考:
https://www.cnblogs.com/ClarkChan/archive/2006/10/18/532565.html
https://blog.csdn.net/mxly520/article/details/1925430

  • 来自: 【慢半拍的佬唐】,欢迎评论
  • 标签:SQL  存储过程  参数  
  • 已有1位网友发表了一针见血的评论,你还等什么?

    必填

    选填

    选填

    记住我,下次回复时不用重新输入个人信息

    必填

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。