pl sql 和动态sql

发布于 2024-08-05 18:31:35 字数 836 浏览 6 评论 0原文

我正在尝试使用以下代码块创建一些动态 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;

该语句永远不会正确执行,因为值周围缺少单引号。

我可以做些什么来使这个小例子发挥作用,或者有什么更好的方法吗?

I am trying to create some dynamic sql using the following code block

    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;

The statement never executes properly as there are missing single quotes around values.

Any ideas what i can do to make this small example work or is there any better way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍暮颜 2024-08-12 18:31:35
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;

使用 '''

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;

use '''

淡墨 2024-08-12 18:31:35

也许你可以这样做。

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;

Maybe you can do it this way.

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;
荒芜了季节 2024-08-12 18:31:35
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;

您可以使用多个选择来编写要更新的选择字段和类型的语句。

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;

You can use multiple selections to compose your statement for selection fields to be updated and for the type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文