2009-09-30 65 views
0

我嘗試使用下面的代碼塊PL SQL和動態SQL

firstSqlStatement := true; 
    updateText := 'UPDATE T_EMPLOYEES SET '; 
    if FIRSTNAME IS NOT NULL and FIRSTNAME > 0 THEN 
     updateText:=updateText || ' firstName=' || FIRSTNAME || ' '; 
     firstSqlStatement := false; 
    end if; 

    if MIDDLENAME IS NOT NULL and length(MIDDLENAME) > 0 THEN 
     if firstSqlStatement = false THEN 
      updateText:=updateText || ','; 
     end if; 

     updateText:=updateText || ' middleName=' || MIDDLENAME || ' '; 
     firstSqlStatement := false; 
    end if; 

    updateText:=updateText 
      || ' where upper(id)=upper(' || ID ||');';      
    DBMS_OUTPUT.put_line(updateText); 
    EXECUTE IMMEDIATE updateText; 

的語句根本不會執行正確,因爲有缺失值左右單引號來創建一些動態SQL。

任何想法我能做些什麼來使這個小例子工作或有沒有更好的方式做到這一點?

回答

1
firstSqlStatement := true; 
updateText := 'UPDATE T_EMPLOYEES SET '; 

if FIRSTNAME IS NOT NULL and FIRSTNAME > 0 THEN 
    updateText:=updateText || ' firstName=''' || FIRSTNAME || ''' '; 
    firstSqlStatement := false; 
end if; 

if MIDDLENAME IS NOT NULL and length(MIDDLENAME) > 0 THEN 
    if firstSqlStatement = false THEN 
     updateText:=updateText || ','; 
    end if; 

    updateText:=updateText || ' middleName=''' || MIDDLENAME || ''' '; 
    firstSqlStatement := false; 
end if; 
updateText:=updateText || ' where upper(id)=upper(' || ID || ');'; 
DBMS_OUTPUT.put_line(updateText); 
EXECUTE IMMEDIATE updateText; 

使用 ''」

1

也許你可以這樣來做。

declare 
    ll_employee_id number := 10; 
    lv_firstname varchar2(30) := 'Thomas'; 
    lv_middlename varchar2(30) := null; 
begin 
    update t_employees 
    set firstname = decode(lv_firstname, null, firstname, lv_firstname), 
     middlename = decode(lv_middlename, null, middlename, lv_middlename) 
    where employee_id = ll_employee_id; 
end; 
0
DECLARE 
    my_error exception; 
    sql_stmt   VARCHAR2 (500); 
    v_char_field  VARCHAR2 (500); 
    v_number_field NUMBER; 
    v_stmt_number NUMBER; 
BEGIN 
    sql_stmt := 'UPDATE TABLE'; 

    IF 1 = 1 -- REPLACE WITH CONDITION FOR SELECTION FIELD 
    THEN 
     sql_stmt := sql_stmt || 'field_1 = :1'; 
     v_stmt_number := 1; 
    ELSIF 1 = 1 -- REPLACE WITH CONDITION FOR SELECTION FIELD 
    THEN 
     sql_stmt := sql_stmt || 'field_2 = :1'; 
     v_stmt_number := 2; 
    ELSE 
     DBMS_OUTPUT.put_line ('Field unmanaged'); 
     RAISE my_error; 
    END IF; 

    IF 1 = 1 -- REPLACE WITH CONDITION FOR SELECTION TYPE FIELD 
    THEN 
     EXECUTE IMMEDIATE sql_stmt USING v_char_field; 
    ELSIF 1 = 1 -- REPLACE WITH CONDITION FOR SELECTION TYPE FIELD 
    THEN 
     EXECUTE IMMEDIATE sql_stmt USING v_number_field; 
    ELSE 
     DBMS_OUTPUT.put_line ('Type Field unmanaged'); 
     RAISE my_error; 
    END IF; 

    DBMS_OUTPUT.PUT_LINE ('STATEMENT NUMBER : ' || v_stmt_number); 

    DBMS_OUTPUT.PUT_LINE ('TOTAL RECORD UPDATE : ' || SQL%ROWCOUNT); 
EXCEPTION 
    WHEN my_error 
    THEN 
     NULL; 
    WHEN OTHERS 
    THEN 
     DBMS_OUTPUT.PUT_LINE ('ERROR :' || SQLERRM); 
END; 

您可以使用多個選項來撰寫你的語句的選擇字段進行更新和類型。