CREATE PROCEDURE ypesearch
@typeid int,
@num int
 AS
set nocount on
begin 
select top @num  *  from news  where typeid=@typeid  and checkked=1
end
GO
===================提示我第七行'@num'附近有错误.如果这样写就没有错:CREATE PROCEDURE ypesearch
@typeid int,
@num int
 AS
set nocount on
begin 
select top 7 *  from news  where typeid=@typeid  and checkked=1
end
GO为什么?到第第一种写法哪里出错了

解决方案 »

  1.   

    CREATE PROCEDURE ypesearch
    @typeid int,
    @num int
     AS
    begin 
    set nocount on
    declare @s varchar(1000)
    set @s=''
    set @s=@s+'select top'+cast(@num as varchar)+' *  from news  where typeid=@typeid  and checkked=1'
    end
    GO
      

  2.   

    动态写,@num在select 中无法辨别
      

  3.   

    CREATE PROCEDURE ypesearch
    (
    @typeid int,
    @num int
    )
     AS
    set nocount on
    begin 
    exec('select top '+convert(varchar(20),@num)+'  *  from news  where typeid='+convert(varchar(20),@typeid)+'  and checkked=1'
    end
    GO
      

  4.   

    CREATE PROCEDURE ypesearch
    @typeid int,
    @num int
     AS
    begin 
    set nocount on
    declare @s varchar(1000)
    set @s=''
    set @s=@s+'select top'+cast(@num as varchar)+' *  from news  where typeid=@typeid  and checkked=1'
    exec(@s)
    end
    GO
    少了个exec(@s)
    不好意思
      

  5.   

    exec('select top '+cast(@num varchar)+'  *  from news  where typeid=@typeid  and checkked=1')
      

  6.   

    类型不对啊,得转成字符呀。cast(@num as varchar)