create proc query_records
  @dowhat tinyint=3,
  @id  int
as
if @dowhat=1
begin
   select n_title,n_note,uptime from tablename where n_id=@id
   return
end
if @dowhat=2
begin
   select n_title,uptime from tablename where n_class=150
   return
end
if @dowhat=3
begin
   select top 6 * from tablename where n_class=150
   union all
   select * from tablename where n_class=151
   return
end
print '@dowhat is not in (1,2,3)'
go

解决方案 »

  1.   

    create procedure proc
    @dowhat int,
    @id int =null
    as
    if @dowhat=1
    select n_title,n_note,uptime from t where n_id=@id
    if @dowhat =2
    select n_title,uptime from t where n_class=150
    if @dowhat=3
    select top 6 * from t where n_class=150
    union
    select top 6 * from t where n_class=151go
      

  2.   

    create procedure proc
    @dowhat int,
    @id int =null
    as
    if @dowhat=1
    select n_title,n_note,uptime from t where n_id=@id
    if @dowhat =2
    select n_title,uptime from t where n_class=150
    if @dowhat=3
    select top 6 * from t where n_class=150
    union
    select top 6 * from t where n_class=151go
      

  3.   

    to CSDNM(CSDN经理(信就不假) :你给的存储过程我试过,是正确的,但是我在ASP中调用的时候只能显示第一条数据,用rs.movenext的时候就报错。请问该怎么调用呢?to newly_ignorant(不学无术) :你给的我试了,报语法出错,说'@dowhat'未定义,是不是一定要beginend才行?
      

  4.   

    newly_ignorant(不学无术)的语句中输入参数要用括号括起来。
      

  5.   

    用rs.movenext的时候就报错和你的记录集打开方式有关,最好用只读的,或者你把斑竹的代码as后面加句
    set nocount on再试下!!
      

  6.   

    同意杨兄的说法:
    你只是查询数据,可以这样:rs.open strsql,conn,1,1
    涉及到多个记录集的时,用set nocount on(结束处写回-set nocount off),或者用set rs=rs.movenextrecordset()
      

  7.   

    我还有一个问题,就是当@dowhat=2时所列出的数据不能够排序,我需要的是找到n_id最大的6条数据。但是如果这样写的话就会报错:
    select top 6 * from t where n_class=150 order by n_id desc
    union
    select top 6 * from t where n_class=151 order by n_id desc有没有别的什么好办法?
      

  8.   

    select top 6 * from t where n_class=150 or n_class=151 order by n_id desc
      

  9.   

    不好意思,我没说清楚,
    我需要的是n_class=150和n_class=151各6条数据,并且是按照n_id的大小排序的
      

  10.   

    select identity(int,1,1) x, * into #tmp1 from t where n_class=150 order by n_id desc 
    select identity(int,1,1) x, * into #tmp2 from t where n_class=150 order by n_id descselect * from #tmp1 where x<=6 union select * from #tmp2 where x<=6
      

  11.   

    to sky_blue(老衲):我按你的代码试了,但是提示出错:
    服务器: 消息 8108,级别 16,状态 1,行 1
    无法使用 SELECT INTO 语句向表 '#tmp1' 中添加标识列,该表中已有继承了标识属性的列 'n_ID'。
    我将代码改为:
    select top 6 * into #tmp1 from t where n_class=150 order by n_id desc 
    select top 6 * into #tmp2 from t where n_class=151 order by n_id descselect * from #tmp1
    union all
    select * from #tmp2
    第一次执行正确,再次执行的时候提示出错:
    数据库中已存在名为 '#tmp1' 的对象。
      

  12.   

    add a jufge statement in first line:
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablename]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[tablename]
    GO
      

  13.   

    to leimin(黄山光明顶):
      很不好意思,我看不太明白你给的代码,能具体解释一下这段代码吗?
      

  14.   

    现在有一个很头疼的问题出现了,我用这段代码:
    select top 6 * into #tmp1 from t where n_class=150 order by n_id desc 
    select top 6 * into #tmp2 from t where n_class=151 order by n_id descselect * from #tmp1
    union all
    select * from #tmp2returndrop table #tmp1
    drop table #tmp2能够在查询分析器里正确执行,但是我在asp里调用的时候好象不能返回值,一用到
    rs.movenext的时候就提示出错信息:ADODB.Recordset  (0x800A0CC1)
      

  15.   

    做成一个存储过程吧
    create proc proc_test
    as
    begin
    set nocount on
    select top 6 * into #tmp1 from t where n_class=150 order by n_id desc 
    select top 6 * into #tmp2 from t where n_class=151 order by n_id descselect * from #tmp1
    union all
    select * from #tmp2set nocount off
    end