SQL多条件查询存储过程问题

lyqcyfqttcyf 2012-08-13 04:10:42


CREATE PROCEDURE [sp_SelFindResources5]
--@Memberid int,--会员ID
@WarehouseRN varchar,--仓单号
@Commodity nvarchar, --品名
@Specifications varchar,--规格
@Steelmill nvarchar,--钢厂
@Unitprice int,--单价
@Pagecount int,--每页行数
@pagesize int --第几页
AS
set nocount on
declare @acount int,
@sqlwhere varchar(500)

set @acount=@Pagecount*@pagesize

if(@WarehouseRN='')
set @sqlwhere=@sqlwhere+'and WarehouseRN like ''%'+@WarehouseRN+'%'''
if(@WarehouseRN<>'')
set @sqlwhere=@sqlwhere+'and WarehouseRN like '+@WarehouseRN+'%'''

if(@Commodity='1')
set @sqlwhere=@sqlwhere+'and Commodity like''%'''
if(@Commodity<>'1')
set @sqlwhere=@sqlwhere+'and Commodity ='+@Commodity

if(@Specifications='')
set @sqlwhere=@sqlwhere+'and Specifications like ''%'+@Specifications+'%'''
if(@Specifications<>'')
set @sqlwhere=@sqlwhere+'and Specifications like '+@Specifications+'%'''

if(@Steelmill='')
set @sqlwhere=@sqlwhere+'and Steelmill like ''%'+@Steelmill+'%'''
if(@Steelmill<>'')
set @sqlwhere=@sqlwhere+'and Steelmill like '+@Steelmill+'%'''

if(@Unitprice='0')
set @sqlwhere=@sqlwhere+'and Unitprice like''%'''
if(@Unitprice<>'0')
set @sqlwhere=@sqlwhere+'and Unitprice ='+@Unitprice+''

EXEC( 'select * from VIEW_sp_SelFindResources5 where 1=1'+@sqlwhere)


前台代码都没错,问题出在存储过程中
装载的时候,显示的是全部数据,已经显示了

前台输入 单价:111
提示:[Exception: System.Data.SqlClient.SqlException: 将 varchar 值 ''' 转换为数据类型为 int 的列时发生语法错误。

前台选择或输入其他数据时,没有反应,还是显示全部
...全文
77 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyqcyfqttcyf 2012-08-14
  • 打赏
  • 举报
回复
我换了总方法,搞定了,还是谢谢1楼2楼2位的帮忙
lyqcyfqttcyf 2012-08-13
  • 打赏
  • 举报
回复
数据类型的转换好了,现在还有就是 赋一个条件的值,和赋多个条件的值,还是显示全部,并没有按条件搜索
lyqcyfqttcyf 2012-08-13
  • 打赏
  • 举报
回复
if(@Unitprice='0')
set @sqlwhere=@sqlwhere+'and Unitprice like''%'''
if(@Unitprice<>'0')
set @sqlwhere=@sqlwhere+'and Unitprice ='+convert(varchar(100),@Unitprice)+''

按照1楼和2楼的改了,还是提示同样的错误
将 varchar 值 ''' 转换为数据类型为 int 的列时发生语法错误。
  • 打赏
  • 举报
回复
@Unitprice既然定义为int,为什么要把它当成varchar来用呢?

set @sqlwhere=@sqlwhere+'and Unitprice ='+@Unitprice+''里的@Unitprice替换为
convert(varchar(30),@Unitprice)
人生无悔 2012-08-13
  • 打赏
  • 举报
回复

set @sqlwhere=@sqlwhere+'and Unitprice ='+@Unitprice+''
--类似这种整型参数的都改过来
set @sqlwhere=@sqlwhere+'and Unitprice ='+convert(varchar(100),@Unitprice)+''

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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