急!!!关于查询条件的一个问题!!!在线等希望大虾们帮我看一下哦!!!

foollt 2006-04-10 03:39:50
我要写一个搜索的存储过程,里面包括了个个不同的搜索条件,代码如下:
use ekoshop
go
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

alter Procedure spGetSearchItems
@i_EndDate int,
--@i_Bit int,
@i_title varchar(50)=null,
@i_Zip varchar(50)=null,
@i_UserName varchar(50)=null,
@i_ItemCode char(10)=null,
@i_page int,
@i_pageSize int,
@i_kind bit,
@i_MaxPrice money,
@i_MinPrice money,
@i_Categories int=null,
@o_row_total int=null output,
@o_pageCount int=null output,
@o_RecordCount int=null output
AS
begin
declare @l_Sql varchar(1000)
declare @l_temp varchar(1000)
declare @l_CountBank int
DECLARE @l_Start int --start record
DECLARE @l_End int --end record
DECLARE @l_Page int
DECLARE @l_returncount int
declare @l_CateCode int
declare @l_Dendtime datetime
set nocount on
select --查出第3第4级分类
@l_CateCode=CategoryCode
from
viewCategoryName
where
Sid like '%'+cast(@i_Categories as varchar(50))+'%'
--set @l_temp='select * from TbonlineItem,tbuser where 1=1'
set @l_temp='1=1'
/*if(@i_title is not null)
begin
set @l_temp=@l_temp+ 'and TbonlineItem.title like'+'%'+@i_title+'%'
end*/
if(@i_Zip is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.Zip='+@i_Zip
end
if(@i_username is not null)
begin
set @l_temp=@l_temp+'and Tbuser.username='+@i_Username
end
if(@i_ItemCode is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.ItemCode='+cast(@i_ItemCode as varchar(50))
end
if(@i_MaxPrice is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.price<='+cast(@i_MaxPrice as varchar(50))
end
if(@i_MinPrice is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.price>='+cast(@i_MinPrice as varchar(50))
end
if(@i_Enddate is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.EndTime<dateadd(day,'+cast(@i_EndDate as varchar(50))+',getdate())'
end
set @l_temp=@l_temp+'and TbonlineItem.kind='+cast(@i_kind as varchar(50))+'and TbonlineItem.CategoryCode in'+'('+cast(@l_CateCode as varchar(50))+')'

create table #t --临时表
(ID int IDENTITY,--自增字段
ItemCode char(10),
username varchar(50)
)
set @l_Sql='insert into #t
SELECT
TbonlineItem.ItemCode,
Tbuser.username
from TbonlineItem
left join tbuser
on TbonlineItem.userId=Tbuser.id
where '
set @l_Sql=@l_Sql+@l_temp

exec (@l_Sql)
--确定总页数
if(@o_RecordCount%@i_pageSize = 0)
set @o_pageCount = CEILING(@o_RecordCount/@i_pageSize)
else
set @o_pageCount = CEILING(@o_RecordCount/@i_pageSize) + 1

--若请求的页号大于总页数,则显示最后一页
if(@i_page > @o_pageCount)
set @l_page = @o_pageCount
else
set @l_page = @i_page

--确定当前页的始末记录
set @l_Start = (@l_page - 1) * @i_pageSize
set @l_End = @l_Start + @i_pageSize + 1
select
a.Title,
a.[Description],
b.UserName,
c.ItemImage1,
a.Price,
a.EndTime,
a.kind,
a.categorycode,
a.ItemCode
from TbOnlineItem a
left join TbUser b
On a.UserId=b.Id
left join TbItemImages c
on a.ItemCode=c.ItemCode and a.UserId=c.SellerId
where a.ItemCode in(select ItemCode from #t where ID > @l_Start and ID < @l_End ) order by endTime desc
--确定当前记录数
set @o_RecordCount = @@rowcount
set @o_pageCount = 1
set @l_returncount = @@rowcount
return @o_RecordCount

TRUNCATE TABLE #t
Drop Table #t

end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

有点长,请大虾们帮我看一下,我测试怎么都没有数据显示出来啊!我知道是@l_temp这个出了问题,但我看不出有什么问题!!!请大虾们帮我看一下吧!!!
...全文
169 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbsql 2006-04-11
  • 打赏
  • 举报
回复
没仔细看完,不过这个:
if(@i_Zip is not null)
begin
set @l_temp=@l_temp+'and TbonlineItem.Zip='+@i_Zip
end
if(@i_username is not null)
begin
set @l_temp=@l_temp+'and Tbuser.username='+@i_Username
end
至少应该改成这样吧:
if(@i_Zip is not null)
begin
set @l_temp=@l_temp+' and TbonlineItem.Zip='''+@i_Zip+''''
end
if(@i_username is not null)
begin
set @l_temp=@l_temp+' and Tbuser.username='''+@i_Username+''''
end
注意and前后应有空格,字符串的连接应有引号

把exec (@l_Sql)改为:
print @l_Sql
然后执行看结果就知道你构造的SQL语句是否正确了
bugchen888 2006-04-11
  • 打赏
  • 举报
回复
把exec (@l_Sql)改为:
print @l_Sql
然后执行看结果就知道你构造的SQL语句是否正确了

-------------------------------------------------

完全赞同!!!
sxdoujg 2006-04-11
  • 打赏
  • 举报
回复
up
OracleRoob 2006-04-10
  • 打赏
  • 举报
回复
用查询分析器调试一下
本文涵盖了使用Visual Basic (VB) 进行编程时遇到的各种问题及解决方案,包括但不限于安装程序本地化、OLE DB错误处理、MDI ActiveX封装、多进程结束、文件名显示、系统信息提取、SQL查询、字符串连接、EEPROM操作、二进制文件读取、数据库连接、数据导入导出、权限提升、串口通信、多媒体处理、网络编程、浏览器控件使用、数据库操作、Excel导出、图片显示、数据库中文支持、颜色区域显示、远程用户识别、网页自动提交、数据网格批量录入、网页同步、函数调用、硬件信息读取、编译自动化、软件许可验证、批量操作、数据采集、编程书籍获取、网络版系统使用、打印问题、按钮交互、属性修改、实时曲线绘制、控件数据绑定、对话框保存、前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具、嵌入式硬件、嵌入式电路知识、嵌入式开发环境、音视频基础、音视频直播流媒体、图像处理AR特效、AI音视频处理、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算、文档协作与知识管理、版本控制、项目管理与协作工具、有监督学习、无监督学习、半监督学习、强化学习、数据安全、数据挖掘、数据结构、算法、非IT技术、自动推理、人工神经网络与计算、自动驾驶、数据分析、数据工程、数据挖掘、程序设计方法、数据库理论、代码管理工具

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧