如何在 Insert 语句中使用 PL SQL 常量

发布于 2024-10-30 15:32:07 字数 559 浏览 0 评论 0原文

我是 PL SQL 代码新手,需要帮助编写插入查询语句。 例如:我想在员工表中插入员工,其中只有员工姓名发生变化,但城市不变。

DECLARE
  emp_var_prefix varchar2(12) := 'QATEST';
  emp_var_temp varchar2(12) := '';
  city constant varchar(30) := 'dallas'

begin
  DBMS_OUTPUT.ENABLE;
  for i in 1..2 loop
    emp_var_temp  := emp_var_prefix;
    emp_var_temp := emp_var_prefix ||i;

    INSERT INTO EMPLOYEE TABLE ('EMPLOYEE_NAME', 'CITY') values ('emp_var_temp', '<what should I put here for constant dallas for city name>');

    DBMS_OUTPUT.PUT_LINE(emp_var_temp);
  end loop;

END;

I am new to PL SQL Code and need help writing Insert query statement .
eg: I want to insert Employee in Employees table where only employee name changes but the city is constant.

DECLARE
  emp_var_prefix varchar2(12) := 'QATEST';
  emp_var_temp varchar2(12) := '';
  city constant varchar(30) := 'dallas'

begin
  DBMS_OUTPUT.ENABLE;
  for i in 1..2 loop
    emp_var_temp  := emp_var_prefix;
    emp_var_temp := emp_var_prefix ||i;

    INSERT INTO EMPLOYEE TABLE ('EMPLOYEE_NAME', 'CITY') values ('emp_var_temp', '<what should I put here for constant dallas for city name>');

    DBMS_OUTPUT.PUT_LINE(emp_var_temp);
  end loop;

END;

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

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

发布评论

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

评论(1

带上头具痛哭 2024-11-06 15:32:07

看起来你会想要类似的东西,

DECLARE
  l_emp_var_prefix varchar2(12) := 'QATEST';
  l_emp_var_temp varchar2(12) := '';
  l_city constant varchar(30) := 'dallas'
begin
  DBMS_OUTPUT.ENABLE;
  for i in 1..2 loop
    l_emp_var_temp := l_emp_var_prefix ||i;

    INSERT INTO EMPLOYEE(employee_name, city) 
      values (l_emp_var_temp, l_city );

    DBMS_OUTPUT.PUT_LINE(l_emp_var_temp);
  end loop;

END;

一般来说,使用同时也是列名的变量名是一个坏主意——这使得你很可能会遇到范围错误,当你打算引用一个变量和编译器无意中假定您要引用该列。这些调试起来相当麻烦。采用一些约定会更容易,例如在局部变量前添加 l_ 前缀,以将它们与可以使用 p_ 前缀的列名或参数区分开来。

It appears that you would want something like

DECLARE
  l_emp_var_prefix varchar2(12) := 'QATEST';
  l_emp_var_temp varchar2(12) := '';
  l_city constant varchar(30) := 'dallas'
begin
  DBMS_OUTPUT.ENABLE;
  for i in 1..2 loop
    l_emp_var_temp := l_emp_var_prefix ||i;

    INSERT INTO EMPLOYEE(employee_name, city) 
      values (l_emp_var_temp, l_city );

    DBMS_OUTPUT.PUT_LINE(l_emp_var_temp);
  end loop;

END;

It is a bad idea in general to use variable names that are also column names-- that makes it exceedingly likely that you're going to encounter scope bugs where you intend to refer to a variable and the compiler inadvertently assumes that you meant to refer to the column. Those are rather nasty to debug. It's easier to adopt some convention like prefixing local variables with l_ to differentiate them from column names or parameters that can be prefixed with p_.

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