如何在SQL Server中的过程内运行游标

时间:2015-12-11 20:59:58

标签: oracle stored-procedures plsql

我创建了这两个表

CREATE TABLE STUD 
(
    ST_NO NUMBER(4), 
    NAME VARCHAR2(20), 
    CITY VARCHAR2(20), 
    TEL NUMBER(9), 
    SEX VARCHAR2(10)
); 

INSERT INTO STUD VALUES(1, 'hamda', 'alburimi', 98765, 'f');
INSERT INTO STUD VALUES(2, 'fooz', 'sohar', 98765, 'f');
INSERT INTO STUD VALUES(3, 'ali', 'alburimi', 98765, 'm');

CREATE TABLE soh_STUDENT AS
    (SELECT *
     FROM STUD
    );

然后我在一个程序中做了一个游标:

create or replace PROCEDURE SOH_MEMBER IS

CURSOR studC IS
SELECT* from STUD;
BEGIN
  for c1   IN  studC
LOOP
  IF C1.CITY = 'sohar'
  THEN INSERT INTO SOH_STUDENT (NAME, TEL, SEX)
  VALUES( C1.NAME,  C1.TEL, C1.SEX);
  end if;
  END LOOP;
  END SOH_MEMBER;

我想知道如何显示此程序的结果?

我的意思是我如何只在SOH_STUDENT表中为CITY ='sohar'显示NAME,TEL和SEX?

1 个答案:

答案 0 :(得分:0)

您可以像这样显示输出

create or replace PROCEDURE SOH_MEMBER IS

CURSOR studC IS
SELECT* from STUD;
BEGIN
  for c1   IN  studC
LOOP
  IF C1.CITY = 'sohar'
  THEN INSERT INTO SOH_STUDENT (NAME, TEL, SEX)
  VALUES( C1.NAME,  C1.TEL, C1.SEX);
  end if;
  END LOOP;
  END SOH_MEMBER;

/* show the output */
Select * From SOH_STUDENT

但你可以用这种方式更快地完成整个事情(OTTOMH):

create or replace PROCEDURE SOH_MEMBER IS

Insert Into SOH_STUDENT (NAME, TEL, SEX)
SELECT NAME, TEL, SEX
from STUD
Where CITY = 'sohar'

Select * From SOH_STUDENT