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