日期:2014-05-17  浏览次数:20479 次

帮忙看个存储过程
if exists(select *from sysobjects where name='getcompanymap')
drop proc getcompanymap
go
create proc getcompanymap
@areaid int,
@CustTradePtr int,
@CustTrade1 int,
@CustTrade2 int
as
declare @sql varchar(2000)
set @sql='select custname,url,didu,mobile,addr from company where 1=1'
if @CustTradePtr<>0
set @sql=@sql+' and CustTradePtr='+@CustTradePtr
if @CustTrade1<>0
set @sql=@sql+' and CustTrade1='+@CustTrade1
if @CustTrade2<>0
set @sql=@sql+' and CustTrade2='+@CustTrade2
set @sql=@sql+' and areaid='+@areaid
print(@sql)

exec getcompanymap 2342,15,0,0

在将 varchar 值 'select custname,url,didu,mobile,addr from company where 1=1 and CustTradePtr=' 转换成数据类型 int 时失败。


------解决方案--------------------
SQL code

if exists(select *from sysobjects where name='getcompanymap')
drop proc getcompanymap
go
create proc getcompanymap
@areaid int,
@CustTradePtr int,
@CustTrade1 int,
@CustTrade2 int
as
declare @sql varchar(2000)
set @sql='select custname,url,didu,mobile,addr from company where 1=1'
if @CustTradePtr<>0
set @sql=@sql+' and CustTradePtr='+convert(varchar(20),@CustTradePtr)
if @CustTrade1<>0
set @sql=@sql+' and CustTrade1='+convert(varchar(20),@CustTrade1)
if @CustTrade2<>0
set @sql=@sql+' and CustTrade2='+convert(varchar(20),@CustTrade2)
set @sql=@sql+' and areaid='+convert(varchar(20),@areaid)
print @sql

------解决方案--------------------
exec (@sql),加括号

------解决方案--------------------
你的存储过程中,exec @sql改写成execute (@sql),加括号
探讨
我是这样写的

SQL code


if exists(select *from sysobjects where name='getcompanymap')
drop proc getcompanymap
go
create proc getcompanymap
@areaid int,
@CustTradePtr int,
@CustTrade1 int,
@Cus……

------解决方案--------------------
declare @sql varchar(2000) 
@sql是字符串,参数是int,不能直接 “+”哈,用convert转换一下就好了