oracle使用遊標進行迴圈資料插入

來源:互聯網
上載者:User

建表語句:

begin
execute immediate ' drop table Customer';
exception when others then
null;
end;

 

begin
execute immediate ' drop table OrderHistory';
exception when others then
null;
end;  

  -- Create Customer Table
CREATE TABLE Customers
(   
    Customer_Id NUMBER(16) NOT NULL,
    Name VARCHAR(25),
    CONSTRAINT cus_id_pk PRIMARY KEY(Customer_Id )
)

-- Create Order Table
CREATE TABLE Orders
(   
    Order_Id  NUMBER(16) NOT NULL,
    Name VARCHAR(25),
    Customer_Id NUMBER(16) NOT NULL,
    CONSTRAINT ord_id_pk PRIMARY KEY(Order_Id ),
    CONSTRAINT cus_id_fk FOREIGN KEY (Customer_Id) REFERENCES Customers(Customer_Id)
)

 

 

-- Create Order History Table

begin
execute immediate ' drop table OrderHistory';
exception when others then
null;
end;

CREATE TABLE OrderHistory
(
    OrderHistory_Id NUMBER(16),
    CustomerName VARCHAR(25),
    OrderName VARCHAR(25),
    CONSTRAINT OrderHistory_Id_pk PRIMARY KEY(OrderHistory_Id )
)

插入資料的預存程序:

CREATE PROCEDURE spAddOrderHistory
(
    @CustomerName VARCHAR(25),
    @OrderName VARCHAR(25)
)
AS
BEGIN
    INSERT INTO OrderHistory(CustomerName,OrderName)
    VALUES(@CustomerName, @OrderName)
END

使用遊標進行資料插入:

-- use cursor to insert data into order history table
DECLARE @customer_name VARCHAR(25)
DECLARE @order_name VARCHAR(25)

DECLARE curOrder CURSOR READ_ONLY
FOR
SELECT c.Name as [Customer Name], o.Name as [Order Name]
FROM Customers c INNER JOIN Orders o
    ON c.CustomerId = o.Customer_Id
ORDER BY [Customer Name], [Order Name]

OPEN curOrder

FETCH NEXT FROM curOrder
INTO @customer_name, @order_name

WHILE @@FETCH_STATUS = 0    
BEGIN
    EXEC spAddOrderHistory @customer_name, @order_name 
           
    FETCH NEXT FROM curOrder INTO @customer_name, @order_name
END

CLOSE curOrder
DEALLOCATE curOrder

運行結果:

C2 O2 by C2
C1 O3 by C1
C2 O4 by C2
C1 O1 by C1
C1 O5 by C1
C2 O6 by C2

 

建表語句:

begin
execute immediate ' drop table Customer';
exception when others then
null;
end;

 

begin
execute immediate ' drop table OrderHistory';
exception when others then
null;
end;  

  -- Create Customer Table
CREATE TABLE Customers
(   
    Customer_Id NUMBER(16) NOT NULL,
    Name VARCHAR(25),
    CONSTRAINT cus_id_pk PRIMARY KEY(Customer_Id )
)

-- Create Order Table
CREATE TABLE Orders
(   
    Order_Id  NUMBER(16) NOT NULL,
    Name VARCHAR(25),
    Customer_Id NUMBER(16) NOT NULL,
    CONSTRAINT ord_id_pk PRIMARY KEY(Order_Id ),
    CONSTRAINT cus_id_fk FOREIGN KEY (Customer_Id) REFERENCES Customers(Customer_Id)
)

 

 

-- Create Order History Table

begin
execute immediate ' drop table OrderHistory';
exception when others then
null;
end;

CREATE TABLE OrderHistory
(
    OrderHistory_Id NUMBER(16),
    CustomerName VARCHAR(25),
    OrderName VARCHAR(25),
    CONSTRAINT OrderHistory_Id_pk PRIMARY KEY(OrderHistory_Id )
)

插入資料的預存程序:

CREATE PROCEDURE spAddOrderHistory
(
    @CustomerName VARCHAR(25),
    @OrderName VARCHAR(25)
)
AS
BEGIN
    INSERT INTO OrderHistory(CustomerName,OrderName)
    VALUES(@CustomerName, @OrderName)
END

使用遊標進行資料插入:

-- use cursor to insert data into order history table
DECLARE @customer_name VARCHAR(25)
DECLARE @order_name VARCHAR(25)

DECLARE curOrder CURSOR READ_ONLY
FOR
SELECT c.Name as [Customer Name], o.Name as [Order Name]
FROM Customers c INNER JOIN Orders o
    ON c.CustomerId = o.Customer_Id
ORDER BY [Customer Name], [Order Name]

OPEN curOrder

FETCH NEXT FROM curOrder
INTO @customer_name, @order_name

WHILE @@FETCH_STATUS = 0    
BEGIN
    EXEC spAddOrderHistory @customer_name, @order_name 
           
    FETCH NEXT FROM curOrder INTO @customer_name, @order_name
END

CLOSE curOrder
DEALLOCATE curOrder

運行結果:

C2 O2 by C2
C1 O3 by C1
C2 O4 by C2
C1 O1 by C1
C1 O5 by C1
C2 O6 by C2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.