1. 程式人生 > >Oracle(遊標 table type )Mysql(遊標)Sqlserver(遊標 迴圈)

Oracle(遊標 table type )Mysql(遊標)Sqlserver(遊標 迴圈)

############################################################################
Oracle:

CREATE OR REPLACE PROCEDURE SP_STR(V_OUT OUT SYS_REFCURSOR)
 AS
    CURSOR C1 IS SELECT * FROM DM_CUST_ACCOUNT_INFO_TEST;
    
    TYPE C3 IS TABLE OF DM_CUST_ACCOUNT_INFO_TEST%ROWTYPE;
    C4 C3;
					V_START NUMBER :=1;
					V_LOCATE NUMBER;
					V_STR_BEFORE VARCHAR2(4000);
					V_STR VARCHAR2(100);
BEGIN
  OPEN C1;
    LOOP
       EXIT WHEN C1%NOTFOUND;
          FETCH C1 BULK COLLECT INTO C4;
            FOR I IN 1..C4.COUNT LOOP
                V_STR:=C4(I).PROPERTY_NAME;
                WHILE INSTR(V_STR,'+')>0 
                LOOP     
					      V_LOCATE :=INSTR(V_STR,'+');     
					      V_STR_BEFORE :=SUBSTR(V_STR,V_START,V_LOCATE-V_START);     
					      INSERT INTO DM_CUST_ACCOUNT_INFO_TEST_TMP
					                                             (DATA_DT,
					                                             INVESTOR_ID,
					                                             PROPERTY_NAME) 
					                                    VALUES(C4(I).DATA_DT,
					                                           C4(I).INVESTOR_ID,
					                                           V_STR_BEFORE);     
					      V_STR :=SUBSTR(V_STR,V_LOCATE+1);     
					      END LOOP;     
					      INSERT INTO DM_CUST_ACCOUNT_INFO_TEST_TMP(DATA_DT,
					                                                INVESTOR_ID,PROPERTY_NAME) 
					                                        VALUES(C4(I).DATA_DT,
					                                               C4(I).
					                                               INVESTOR_ID,
					                                               V_STR);  
					      commit;           
               END LOOP;
               COMMIT;
    END LOOP;
    CLOSE C1;
  OPEN V_OUT FOR SELECT DATA_DT,INVESTOR_ID,PROPERTY_NAME FROM DM_CUST_ACCOUNT_INFO_TEST_TMP;
 END;
 /


#############################################################################################
#############################################################################################
#############################################################################################
MYSQL:
Mysql 中游標的用法
CREATE TABLE EMP
   (EMPNO DECIMAL(4,0),
    ENAME VARCHAR(10),
    JOB   VARCHAR(9),
    MGR   DECIMAL(4,0),
    HIREDATE DATE,
    SAL    DECIMAL(7,2),
    COMM   DECIMAL(7,2),
    DEPTNO DECIMAL(2,0),
    CONSTRAINT PK_EMP PRIMARY KEY (EMPNO));
    


insert into emp values(7788,'xugq','eng',5599,str_to_date('2018-11-07',"%Y-%m-%d"),5000,11,10);

insert into emp values(7888,'lfn','org',699,str_to_date('2018-11-07',"%Y-%m-%d"),50000,11,10);

insert into emp values(7856,'xuguoqi','orgname',699,str_to_date('2018-12-07',"%Y-%m-%d"),580,11,10);


mysql> select * from emp;
+-------+---------+---------+------+------------+-------+------+--------+
| EMPNO | ENAME   | JOB     | MGR  | HIREDATE   | SAL   | COMM | DEPTNO |
+-------+---------+---------+------+------------+-------+------+--------+
| 7788  | xugq    | eng     | 5599 | 2018-11-07 | 5000  | 11   | 10     |
| 7856  | xuguoqi | orgname | 699  | 2018-12-07 | 580   | 11   | 10     |
| 7888  | lfn     | org     | 699  | 2018-11-07 | 50000 | 11   | 10     |
+-------+---------+---------+------+------------+-------+------+--------+


CREATE PROCEDURE SP_STR
AS

BEGIN
     DECLARE ERR INT DEFAULT 0;
     DECLARE V_EMPNO DECIMAL;
     DECLARE V_ENAME VARCHAR(255);
     DECLARE V_JOB   VARCHAR(255);
			 DECLARE C1 CURSOR FOR SELECT EMPNO,ENAME,JOB FROM EMP;
			 DECLARE CONTINUE HANDLER FOR NOT FOUND SET ERR=1;
			 OPEN C1;
			   REPEAT
					  FETCH C1 INTO V_EMPNO,V_ENAME,V_JOB;
            IF ! ERR THEN
					  INSERT INTO XUGQ VALUES(V_ENAME);
            END IF;
				  UNTIL ERR=1
			   END REPEAT; 
	  SELECT * FROM XUGQ;
    DELETE FROM XUGQ;
    CLOSE C1;   
END;

執行過程:

xugq
xuguoqi
lfn


#############################################################################################
#############################################################################################
#############################################################################################
sqlserver
#if 後面是沒有then 的, while後面也不需要關鍵字.以end結尾.
#WHILE 迴圈
CREATE  PROCEDURE FEP_GET_ACTION_NAME_BAK_1(@V_ACTION_RULE varchar(1000))
AS
BEGIN
DECLARE @V_START  INTEGER;
DECLARE @V_LOCATE INTEGER;
SET @V_START=1;
  BEGIN
     DECLARE  @V_DISPLAY_NAEM VARCHAR(255);  
     DECLARE  @V_STR_BEFORE VARCHAR(4000);

     SET @V_ACTION_RULE = CONCAT(@V_ACTION_RULE,',');
     WHILE CHARINDEX(@V_ACTION_RULE,',')>0
     BEGIN
			     SET @V_LOCATE =CHARINDEX(@V_ACTION_RULE,',');
			     SET @V_STR_BEFORE=SUBSTRING(@V_ACTION_RULE,@V_START,@
[email protected]
_START); SELECT @V_DISPLAY_NAEM=ACTION_LEVEL_DISPLAY_NAME FROM action_level WHERE [email protected]_ACTION_RULE AND STATUS!=0; if @V_DISPLAY_NAEM is NOT NULL BEGIN INSERT INTO temp_action_name VALUES(@V_STR_BEFORE,@V_DISPLAY_NAEM); END; commit; SET @V_ACTION_RULE =SUBSTRING(@V_ACTION_RULE,1,@V_LOCATE+1); END; SELECT ACTION_LEVEL_ID ,ACTION_LEVEL_DISPLAY_NAME from temp_action_name; TRUNCATE TABLE temp_action_name; END; END; #CURSOR: ALTER PROCEDURE SQLSERVER_CUSSOR(@master_id_1 varchar(255) output) AS BEGIN DECLARE @V_MASTER_ID VARCHAR(255); DECLARE @V_DD_DATE VARCHAR(128); DECLARE C1 CURSOR FOR select sname from users where lid in (38,137,264); OPEN C1; FETCH NEXT FROM C1 INTO @V_MASTER_ID; WHILE @@FETCH_STATUS=0 BEGIN insert into ttt(id,al) values(1,@V_MASTER_ID);--動作最終結果 ttt表將會有三條記錄 fetch next from c1 into @V_MASTER_ID; END; CLOSE C1; DEALLOCATE C1; select @master_id_1=count(*) from ttt; delete from ttt; END; 執行sp: DECLARE @IDTest varchar(255); EXEC dbo.SQLSERVER_CUSSOR     @master_id_1 = @IDTest OUTPUT;
[email protected]
_id_1是sp中定義的輸出引數 SELECT  @IDTest; 執行結果為:3