2012-04-26 85 views
1

我回到了甲骨文的過於複雜的世界裏,我想做到以下幾點:選擇最近插入的記錄然後回滾

  • 插入一條記錄到數據庫中
  • 再次選擇記錄
  • 輥一切恢復

我已經創建了下面的代碼失敗

BEGIN 

insert into sometable values (1, 1, 'test', 'test', 1, 'a', 1, 1, 1, 'test', 'test', 'test', 'test', 1); 

select * 
from sometable 
where id = 1; 

ROLLBACK; 

END; 

錯誤消息:

ORA-06550: line 5, column 1: 
PLS-00428: an INTO clause is expected in this SELECT statement 

我相信這個問題是顯而易見的,但我已經檢查了文檔並不能從中獲得任何智慧。任何指針將不勝感激。

+0

聲明類型'sometable%ROWTYPE'的變量,然後選擇* INTO變量。 – Ollie 2012-04-26 13:37:08

+2

這是一種讓人感到有必要問「爲什麼」的好奇情境。 – 2012-04-26 18:33:24

+0

@DavidAldridge - 哈哈我可以理解你爲什麼問。只是這樣我才能做出改變,檢查我對它感到滿意,然後像在SQL Server中那樣刪除'ROLLBACK'。 – 2012-04-26 22:03:54

回答

3

從SQL * Plus而不只是BEGIN..END執行代碼;

insert into sometable values (1, 1, 'test', 'test', 1, 'a', 1, 1, 1, 'test', 'test', 'test', 'test', 1); 

select * 
from sometable 
where id = 1; 

ROLLBACK; 
3

你必須把選擇的值的變量 像

DECLARE 
    V_COUNT NUMBER; 
BEGIN 

insert into sometable values (1, 1, 'test', 'test', 1, 'a', 1, 1, 1, 'test', 'test', 'test', 'test', 1); 

select COUNT(1) INTO V_COUNT 
from sometable 
where id = 1; 

ROLLBACK; 

END; 
+0

這已停止了這些錯誤,但是如何獲取V_COUNT的值以顯示? – 2012-04-26 13:44:07

+0

SET SERVEROUTPUT ON在您的IDE中,然後在您的代碼塊中選擇後添加'dbms_output.put_line(v_count);' – Ollie 2012-04-26 13:48:58