各位大侠帮帮忙,怎样把这段存储过程转换为SQL语句,在线等,先谢谢了!
createprocSelectProduct@ProdIDvarchar(10),@ProdNamenvarchar(30),@CategoryIDvarchar(5)...
create proc SelectProduct
@ProdID varchar(10),
@ProdName nvarchar(30),
@CategoryID varchar(5),
@MinPrice decimal(10,2),
@MaxPrice decimal(10,2)
as
declare @sql varchar(2000)
set @sql='select * from Product where 1=1'
if @ProdID<>''
set @sql=@sql+' and ProdID like ''%'+@ProdID+'%'''
if @ProdName<>''
set @sql=@sql+' and ProdName like ''%'+@ProdName+'%'''
if @CategoryID<>'ALL'
set @sql=@sql+' and CategoryID='''+@CategoryID+''''
if @MinPrice<>-1
set @sql=@sql+' and Price>='+cast(@MinPrice as varchar)
if @MaxPrice<>-1
set @sql=@sql+' and Price<='+cast(@MaxPrice as varchar)
set @sql=@sql+' order by AddTime desc'
--print @sql
exec(@sql) 展开
@ProdID varchar(10),
@ProdName nvarchar(30),
@CategoryID varchar(5),
@MinPrice decimal(10,2),
@MaxPrice decimal(10,2)
as
declare @sql varchar(2000)
set @sql='select * from Product where 1=1'
if @ProdID<>''
set @sql=@sql+' and ProdID like ''%'+@ProdID+'%'''
if @ProdName<>''
set @sql=@sql+' and ProdName like ''%'+@ProdName+'%'''
if @CategoryID<>'ALL'
set @sql=@sql+' and CategoryID='''+@CategoryID+''''
if @MinPrice<>-1
set @sql=@sql+' and Price>='+cast(@MinPrice as varchar)
if @MaxPrice<>-1
set @sql=@sql+' and Price<='+cast(@MaxPrice as varchar)
set @sql=@sql+' order by AddTime desc'
--print @sql
exec(@sql) 展开
3个回答
展开全部
整个过程相当于执行如下语句:
select * from Product
如果过程没写错,应是如下意思吧:
select * from Product
where (@ProdID = '' or ProdID like '%'+@ProdID+'%')
and (@ProdName = '' or ProdName like '%'+@ProdName+'%')
and (@CategoryID = 'ALL' or CategoryID = @CategoryID)
and (@MinPrice = -1 or Price >= @MinPrice)
and (@MaxPrice = -1 or Price <= @MaxPrice)
order by AddTime desc
select * from Product
如果过程没写错,应是如下意思吧:
select * from Product
where (@ProdID = '' or ProdID like '%'+@ProdID+'%')
and (@ProdName = '' or ProdName like '%'+@ProdName+'%')
and (@CategoryID = 'ALL' or CategoryID = @CategoryID)
and (@MinPrice = -1 or Price >= @MinPrice)
and (@MaxPrice = -1 or Price <= @MaxPrice)
order by AddTime desc
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询