declare @ID                          int
declare @field1                  float
declare @field2                  floatDECLARE sql_cursor CURSOR FOR       SELECT       ID
                   ,field1
                   ,field2
      FROM tablename
OPEN sql_cursor
FETCH NEXT FROM sql_cursor INTO @ID, @field1, @field2
WHILE (@@FETCH_STATUS <> -1) 
BEGIN -- do your actions here eg; 
-- UPDATE tablename SET field3 = @field1 * @field2 WHERE ID = @ID
FETCH NEXT FROM sql_cursor INTO @ID, @field1, @field2
END
DEALLOCATE sql_cursor-- return your modified records
SELECT * FROM tablename

解决方案 »

  1.   

    create proc p1
    @uid  int 
    ,@num1 int output 
    ,@num2 int output 
    ,@num3 int output 
    ,@num4 int output 
    ,@num5 int output 
    ,@num6 int output 
    as
    begin
    select @num1=count(*) from TaMessages where AcceptId=@i and State=0 and Type=0    --新短消息条数 
    select @num2=count(*) from TaSysMessages where AcceptId =@i and state = 0        --新系统消息条数  select @num3=count(*) from TaOnLines where UserId in (select FriendId from  TaFriendsList where MyId=@i)  --用户在线人数  select @num4=count(*) from TaOnLines --总的在线人数  select @num5=count(*) from TaMessages where AcceptId=@i and State=0 and type=1 --好友请求  select @num6=count(*) from TaSysMessages where state=0 and block='礼物' and acceptid=@i --最新收的新礼物  select * from TaUsers where [Id]=@uid --用户资料  select id,content,time from TaMessages where [Id]=@uid --消息资料 
    end
    GO
      

  2.   

    where AcceptId=@i 
    @i是哪儿来的,存储过程传入参数? 
      

  3.   

    我写一个页面,访问量非常大,由于很多原因,发现这个页面打开的速度特别慢而其他页面打开明显快多了.所以我想到优化这个页面的sql.
    然后在开发软件配置上再做一些优化之类的.所以才会有上面的问题.上面所有的查询就是我那个页面的所有查询语句,我想写在一个存储过程中然后调用它,这样会提交N多人访问的速度.
      

  4.   

    select @num1=count(*) from TaMessages where AcceptId=@uid and State=0 and Type=0    --新短消息条数 
    select @num2=count(*) from TaSysMessages where AcceptId =@uid and state = 0        --新系统消息条数 select @num3=count(*) from TaOnLines where UserId in (select FriendId from  TaFriendsList where MyId=@uid)  --用户在线人数 select @num4=count(*) from TaOnLines --总的在线人数 select @num5=count(*) from TaMessages where AcceptId=@uid and State=0 and type=1 --好友请求 select @num6=count(*) from TaSysMessages where state=0 and block='礼物' and acceptid=@uid --最新收的新礼物 select * from TaUsers where [Id]=@uid --用户资料 select id,content,time from TaMessages where [Id]=@uid --消息资料 就上面的select语句写一存储过程,不但返回@num1,@num2,@num3,@num4,@num5,@num6,还要返回用户资料和消息资料的结果集.存储过程传入的参数只有一个@uid. 
    返回结果集的存储过程我不会写,所以才来问问.谢谢大家帮忙,最好说的具体点.貌似4楼的答案.
      

  5.   


    1.   
      --把存储过程返回的记录集插入到表中.   
        
      insert   表   
      exec   存储过程   
        
      --对表进行处理   
        
      select   *   from   表   
      2.   
      --直接对存储过程返回的记录进行处理   
        
      select   a.*   
      from     openrowset('SQLOLEDB','服务器名称';'用户名';'密码',   
            'exec   存储过程名')   AS   a
    网上有这样解决问题的.就不知能不能返回两个结果集和6个int参数呢?希望高手帮忙解决,说得具体点.
      

  6.   

    获取存储过程的返回结果, 不一定要用T-SQL,可以在程序中完成,使用direction为output的参数可以接收返回的参数值,使用dataset可以接收存储过程的结果集,返回多个结果集的,dataset中会有多个table与结果集对应。
      

  7.   

    不是已经产生结果集了吗?在前台程序中调一下就出来了那几个燮量可以写作输出参数,也可以自己存为结果集中的虚列。比如
    select *,@num1 num1,@num2 num2,.... from ..不知道你想问什么/
      

  8.   


    你说的是.net的程序.呵呵,我是做java程序的.还没找到你说的用程序解决问题的方法.等待答案.
      

  9.   

    USE DBA
    IF EXISTS (SELECT name FROM sysobjects 
          WHERE name = 'pr_aa' AND type = 'P')
       DROP PROCEDURE pr_aa
    GO
    USE DBA
    GOcreate proc pr_aa
    @uid int
    as
    SET   XACT_ABORT   on
    BEGIN   TRANSACTION 
    begin
    select num1=(select count(*) from TaMessages where AcceptId=@uid and State=0 and Type=0),
    num2=(select count(*) from TaSysMessages where AcceptId =@uid and state = 0 ),
    num3=(select count(*) from TaOnLines where UserId in (select FriendId from  TaFriendsList where MyId=@uid)),
    num4=(select count(*) from TaOnLines),
    num5=(select count(*) from TaMessages where AcceptId=@uid and State=0 and type=1),
    num6=(select count(*) from TaSysMessages where state=0 and block='礼物' and acceptid=@uid),
    * from TaUsers where id=@uid
    end
    COMMIT   TRANSACTION
    SET   XACT_ABORT  off
    GOselect a.* from openrowset('SQLOLEDB','127.0.0.1';'sa';'','exec  DBA.dbo.pr_aa @uid=19') as a返回一条记录结果集的问题基本解决.
    如果能有方法解决返回多条记录的更好了...