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

存储过程动态查询参数

存储过程动态查询参数

在存储过程参数中,传入查询参数,返回结果,非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




发布时间:2020-07-30 | 评论:1 | 浏览: | 标签:SQL  存储过程  参数  

«1»