我是Oracle SQL Developer的新手。我有一个VFP背景,我很难尝试使用变量,因为我做了很多其他的应用程序。定义变量,确定变量是什么(以编程方式),然后使用变量。我对前两个好,但我无法弄清楚如何使用变量。创建一个过程,将select语句写入var字符串,输出到游标?我已经看过很多例子,但我无法使用,或者我只是不理解不同的概念。这是我开始使用的代码。
declare
v_mydate DATE;
begin
select max(date_added) into v_mydate from TEST_TB;
DBMS_OUTPUT.put_line(TO_CHAR(v_mydate)); -- checking to see if the variable has been assigned. It has
create table test_banks as
SELECT DISTINCT
FIELD1,
FIELD2,
, DATE_APPEND
FROM TEST_TB
WHERE DATE_ADDED=V_MYDATE
end;
Field1和Field2将有许多不同的组合。我希望他们都拥有max(date_added)值。
答案 0 :(得分:0)
此处不允许使用DDL。 DDL是数据定义语言。 CREATE TABLE是一个DDL语句。 尝试执行立即执行,这实在是效率低下。
declare
v_mydate DATE;
begin
select max(date_added) into v_mydate from TEST_TB;
DBMS_OUTPUT.put_line(TO_CHAR(v_mydate)); -- checking to see if the variable has been assigned. It has
EXECUTE IMMEDIATE
'create table test_banks as
SELECT DISTINCT
FIELD1,
FIELD2,
, DATE_APPEND
FROM TEST_TB
WHERE DATE_ADDED in (select max(date_added) from TEST_TB)';
end;
答案 1 :(得分:0)
您无需使用PLSQL执行此任务,您只需创建如下所示的视图
CREATE OR REPLACE VIEW VW_TEST_BANKS
AS SELECT DISTINCT field1, field2, date_append
FROM TEST_TB where date_added=(SELECT TRUNC(MAX(date_added)) FROM TEST_TB)
或者如果您想在WHERE子句中测试变量的使用,可以尝试以下
declare
v_mydate DATE;
begin
select max(date_added) into v_mydate from TEST_TB;
DBMS_OUTPUT.put_line(TO_CHAR(v_mydate)); -- checking to see if the variable has been assigned. It has
FOR REC in (SELECT DISTINCT FIELD1, FIELD2,, DATE_APPEND
FROM TEST_TB
WHERE DATE_ADDED = V_MYDATE) LOOP
DBMS_OUTPUT.put_line('field1 ' || rec.field1);
DBMS_OUTPUT.put_line('field2 ' || rec.field2);
DBMS_OUTPUT.put_line('date_append ' || rec.date_append);
END LOOP;
end;