zoukankan      html  css  js  c++  java
  • 游标调用函数 老猫

    测试环境 sqlserver 2005

    工作需要,回家再转成oracle的

    --游标调用函数

    --1、建立基表
    create table planwork
    (
      planid int,
      empid int
    )

    insert into planwork values (1,100)
    insert into planwork values (2,200)
    insert into planwork values (3,300)
    insert into planwork values (4,400)
    insert into planwork values (5,500)
    insert into planwork values (6,600)
    insert into planwork values (7,700)
    insert into planwork values (8,800)

    select * fom planwork

    --2、建立函数
    drop function findworkplan

    create  function findworkplan(@num int)
    returns int
    as
    begin
      declare @eid int
      set @eid=(select empid from planwork where planid=@num)
      return @eid;
    end;

    --3、测试函数
    select dbo.findworkplan(3)

    --4、利用游标调用函数
    --4.1、创建一个表,利用这个表里面的数值得到workplan表里面对应的empno
    create table xb_test1
    (
      xid int
    )

    insert into xb_test1 values (1)
    insert into xb_test1 values (2)
    insert into xb_test1 values (3)
    insert into xb_test1 values (4)
    insert into xb_test1 values (5)
    insert into xb_test1 values (6)
    insert into xb_test1 values (7)
    insert into xb_test1 values (8)

    select * from xb_test1

    --4.2、只能用循环遍历xb_test1表 分别找出对应表workplan的empno
    --考虑到需遍历整个xb_test1表, 所以决定用游标
    --不知道用oracle的with函数怎么样?

    --该 WHILE 结构测试用于游标的函数 @@FETCH_STATUS 的返回值。
    --因为 @@FETCH_STATUS 可能返回 –2、-1 或 0,所以,
    --所有的情况都应进行测试。如果某一行在开始执行此存储过程
    --以后从游标结果中删除,将跳过该行。
    --成功提取 (0) 后将执行 BEGIN...END 循环内部的 SELECT 语句。

    declare empno_cursor cursor
    for
      select xid from xb_test1
    open empno_cursor
    declare 
      @a int,
      @result int
      fetch next from empno_cursor into @a
      while (@@fetch_status <> -1)
      begin
         if (@@fetch_status <> -2)
         begin   
           --print @a
           set @result=(select dbo.findworkplan(@a))
           print @result
         end
         fetch next from empno_cursor into @a
      end
    close empno_cursor
    deallocate empno_cursor

  • 相关阅读:
    TCP发送窗口更新tcp_ack_update_window
    关于nginx
    通过导出表找导出函数
    导出表
    静态链接库、动态链接库
    数据目录
    扩大节、合并节
    新增一个节
    用程序在代码节空白处加代码
    节空白处添加代码
  • 原文地址:https://www.cnblogs.com/oldcat/p/2146949.html
Copyright © 2011-2022 走看看