当前位置: 代码迷 >> Sql Server >> SQL多条件查询存储过程有关问题
  详细解决方案

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

热度:45   发布时间:2016-04-27 11:26:41.0
SQL多条件查询存储过程问题


CREATE PROCEDURE [sp_SelFindResources5]
[email protected] 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 @[email protected][email protected]

if(@WarehouseRN='')
set @[email protected]+'and WarehouseRN like [email protected]+'%'''
if(@WarehouseRN<>'')
set @[email protected]+'and WarehouseRN like [email protected]+'%'''

if(@Commodity='1')
set @[email protected]+'and Commodity like''%'''
if(@Commodity<>'1')
set @[email protected]+'and Commodity [email protected]

if(@Specifications='')
set @[email protected]+'and Specifications like [email protected]+'%'''
if(@Specifications<>'')
set @[email protected]+'and Specifications like [email protected]+'%'''

if(@Steelmill='')
set @[email protected]+'and Steelmill like [email protected]+'%'''
if(@Steelmill<>'')
set @[email protected]+'and Steelmill like [email protected]+'%'''

if(@Unitprice='0')
set @[email protected]+'and Unitprice like''%'''
if(@Unitprice<>'0')
set @[email protected]+'and Unitprice [email protected]+''

EXEC( 'select * from VIEW_sp_SelFindResources5 where [email protected])


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

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

前台选择或输入其他数据时,没有反应,还是显示全部

------解决方案--------------------
SQL code
set @[email protected]+'and Unitprice [email protected]+''--类似这种整型参数的都改过来set @[email protected]+'and Unitprice ='+convert(varchar(100),@Unitprice)+''
------解决方案--------------------
@Unitprice既然定义为int,为什么要把它当成varchar来用呢?

set @[email protected]+'and Unitprice [email protected][email protected]
convert(varchar(30),@Unitprice)
  相关解决方案