CREATE procedure GetDetails
              (@pagesize int,
             @pageindex int,
             @docount bit,
                           @topicid int)
             as
             set nocount on
declare @Count int
declare @id datetime
             if(@docount=1)
             return (select count(repostid) from repost)+1
             else
             begin
if @pageindex>0
begin
 set @Count = @pagesize * (@pageindex -1) + 1
 set rowcount @Count
 select @id=posttime from where posttime in (select posttime from topic where topicid=@topicid union select posttime from repost where topicid=@topicid) order by posttime asc
endset rowcount @pagesize
select topicid,topiccontent,username,posttime from topic where topicid=@topicid union select topicid,repostcontent,username,posttime from repost where topicid=@topicid where posttime >=@id order by posttime ascset rowcount 0
end
set nocount off
GO说明下:我把topic(主题)和repost(回复)分开放两张表,显示主题的时候要合并起来,分页想用上面的存储过程,为什么出错?错误信息:在关键字where附近出错,在关键字where附近出错

解决方案 »

  1.   

    头一句就不对select @id=posttime from whereFROM后没有表名,当然会报错
      

  2.   

    select @id=posttime from where posttime in ……
                            ^^^^怎么from和where一起了?
      

  3.   

    那我要从两个表union以后生成的表中查询,怎么做呢?
      

  4.   

    视图好像不支持union的,期待高手
      

  5.   

    select @id=posttime from (select posttime from topic where topicid=@topicid union select posttime from repost where topicid=@topicid) order by posttime asc
    这样也不对
      

  6.   

    对变量赋值必须是单结果集,显然
    select posttime from topic where topicid=@topicid union select posttime from repost where topicid=@topicid不是返回单结果集,
    另外order by 前应加个别名 如...) as aaa order by aaa.posttime
      

  7.   

    但我是从union里面select的,不是相当于单结果集了么?