要注意,臨時表只能建在user tempory tables space 上,如果database只有system tempory table space是不能建臨時表的。 另外,DB2的臨時表和sybase及oracle的臨時表不太一樣,DB2的臨時表是在一個session內(nèi)有效的。所以,如果程序有多線程,最好不要用臨時表,很難控制。 建臨時表時最好加上 with replace選項,這樣就可以不顯示的drop 臨時表,建臨時表時如果不加該選項而該臨時表在該session內(nèi)已創(chuàng)建且沒有drop,這時會發(fā)生錯誤。 1.3 從數(shù)據(jù)表中取指定前幾條記錄 select * from tb_market_code fetch first 1 rows only
但下面這種方式不允許 select market_code into v_market_code from tb_market_code fetch first 1 rows only;
選第一條記錄的字段到一個變量以以下方式代替 declare v_market_code char(1); declare cursor1 cursor for select market_code from tb_market_code fetch first 1 rows only for update; open cursor1; fetch cursor1 into v_market_code; close cursor1;
1.4 游標的使用 注意commit和rollback 使用游標時要特別注意如果沒有加with hold 選項,在Commit和Rollback時,該游標將被關(guān)閉。Commit 和Rollback有很多東西要注意。特別小心
游標的兩種定義方式 一種為 declare continue handler for not found begin set v_notfound = 1; end;
declare cursor1 cursor with hold for select market_code from tb_market_code for update; open cursor1; set v_notfound=0; fetch cursor1 into v_market_code; while v_notfound=0 Do --work set v_notfound=0; fetch cursor1 into v_market_code; end while; close cursor1; 這種方式使用起來比較復(fù)雜,但也比較靈活。特別是可以使用with hold 選項。如果循環(huán)內(nèi)有commit或rollback 而要保持該cursor不被關(guān)閉,只能使用這種方式。 &n