日期:2014-05-18  浏览次数:20538 次

存储过程变量取值问题
我写了个存储过程

alter     PROCEDURE   P_GetDownloadAnalyse1
@pageIndex   int,
@pageSize   int,
@pageCount   int   output   --总记录数

  AS
declare   @Bid   int
declare   @sql   varchar(8000)
declare   @sql1   varchar(8000)
create   table   #tt(
bid   int
)

select   @sql1   =   'insert   into   #tt   select   min(bid)   from   (select   top   '+CAST(@pageSize*(@pageIndex-1)   AS   NVARCHAR(50))+ '   a.bid   from   t_behaviorinfo   a '  
select   @sql1   =   @sql1   +   '   left   join   t_datumtype   b   on   (a.linkurl   like   ' '% ' '+b.type)   where   b.type   <>   ' ' ' '   order   by   bid   desc)   w '
exec(@sql1)
print   (@sql1)
select   @Bid     =     bid   from   #tt
print(@Bid   )
if   @Bid   <>   ' '
begin
select   @sql   =   'select   top   '+CAST(@pageSize   AS   NVARCHAR(50))+ '   a.userid,a.linkname,a.linkurl,c.keyword,d.name   from   t_behaviorinfo   a   '
select   @sql   =   @sql   +   'inner   join   t_datumtype   b   on   (a.linkurl   like   ' '% ' '+b.type)   '
select   @sql   =   @sql   +   'inner   join   fsystem_table   e   on   (a.fromurl   like   ' '% ' '+e.sRegExp+ ' '% ' ')   '
select   @sql   =   @sql   +   'inner   join   t_PageKeyWord_FromCMS   c   on   (a.fromurl   like   ' '% ' '+c.url)   '
select   @sql   =   @sql   +   'left   join   t_subject   d   on   (a.fromurl   like   ' '% ' '+d.[key]+ ' '% ' '   and   e.sid   =   d.sid)   '
select   @sql   =   @sql   +   'where   b.type   <>   ' ' ' '     and   d.[key] <> ' ' ' '   and   c.url   <>   ' ' ' '   and   a.bid   <   '+@Bid+ '   order   by   bid   desc   '

从临时表中得到@Bid,然后用户查询语句,可是运行到a.bid   <   '+@Bid+ '时就执行不下去了,a.bid是int型的,我这点应该怎么写,谢谢!


------解决方案--------------------
alter PROCEDURE P_GetDownloadAnalyse1
@pageIndex int,
@pageSize int,
@pageCount int output --总记录数

AS
declare @Bid int
declare @sql varchar(8000)
declare @sql1 varchar(8000)
create table #tt(
bid int
)

select @sql1 = 'insert into #tt select min(bid) from (select top '+CAST(@pageSize*(@pageIndex-1) AS NVARCHAR(50))+ ' a.bid from t_behaviorinfo a '
select @sql1 = @sql1 + ' left join t_datumtype b on (a.linkurl like ' '% ' '+b.type) where b.type <> ' ' ' ' order by bid desc) w '
exec(@sql1)
print (@sql1)
select @Bid = bid from #tt
print(@Bid )
if isnull(@Bid,0) <> 0 --数值型的不要用 <> ' '
begin
select @sql = 'select top