【问题标题】:Attribute names in table in the form varchar2表格中的属性名称 varchar2
【发布时间】:2017-02-03 07:24:56
【问题描述】:

嘿,我正在研究面向对象的数据库,我遇到了这个问题,'用户将输入属性名称 (X) 的名称和属性的值 (Y),您必须执行以下操作...select * from table where X = Y 如何做到这一点?

【问题讨论】:

  • 如果列名不固定且未知,您可能需要动态 SQL。
  • 嘿,谢谢,我用dynamic sql解决了这个问题
  • 请注意,您可以将Y 作为绑定变量传递。
  • 绑定变量是什么意思?

标签: select plsql attributes oracle-sqldeveloper


【解决方案1】:

从 sqlplus 你可以做一个简单的&

SQL> select * from table where x='&y' ;

Enter value for y: 12
old   1: select * from table where x='&y'
old   1: select * from table where x='12'
....

如果你想要它在 plsql 中,那么这里是如何bind variables

-- declare variable
VARIABLE y varchar2(10);

-- assign its value
begin
   :y := 'Y';
end;
/

-- use it in your procedure
declare
  v_r number :=0;
begin
  select count(1) from "table" where x like ('%'||:y||'%');
  dbms_output.put_line('found:'||v_r);

  -- you can also change bind variable value
  :y := :y||':'||v_r;
end;
/

-- print value
print y

从 sqlplus 中的脚本文件启动(带@):

PL/SQL procedure successfully completed.

found:1

PL/SQL procedure successfully completed.

Q:1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多