我有这段代码:
declare
instr varchar2(20);
num_no number;
begin
select AR_ID, ID
into instr, num_no
from quelle_match
where ID = 1;
execute immediate 'CREATE or replace VIEW interm2 AS
SELECT AR_ID, kurs,
((TO_CHAR(to_date(di.kursdatum,''dd.mm.yyyy''),''YYYYMMDD''))+ (TO_CHAR(to_date(di.verfall,''dd.mm.yyyy''),''YYYYMMDD''))) AS dumdate
FROM daten_import di
where di.AR_ID = '||instr||'';
-- where di.AR_ID = ''GXIndex''';
end ;
我收到错误b / c instr无效。当我用实际字符串GXIndex替换变量时,代码可以工作。它也可以使用数字作为变量,但不能使用字符串。这是为什么?
答案 0 :(得分:1)
这样:
execute immediate 'CREATE or replace VIEW interm2 AS
SELECT AR_ID, kurs,
((TO_CHAR(to_date(di.kursdatum,''dd.mm.yyyy''),''YYYYMMDD''))+ (TO_CHAR(to_date(di.verfall,''dd.mm.yyyy''),''YYYYMMDD''))) AS dumdate
FROM daten_import di
where di.AR_ID = '||instr||'';
应该是:
execute immediate 'CREATE or replace VIEW interm2 AS
SELECT AR_ID, kurs,
((TO_CHAR(to_date(di.kursdatum,''dd.mm.yyyy''),''YYYYMMDD''))+ (TO_CHAR(to_date(di.verfall,''dd.mm.yyyy''),''YYYYMMDD''))) AS dumdate
FROM daten_import di
where di.AR_ID = '||instr;
如果instr
是一个整数,我认为它是。
答案 1 :(得分:0)
您在连接instr
的方式上遇到了问题。为清楚起见,让我简化您的问题:
EXECUTE IMMEDIATE
'CREATE OR REPLACE VIEW interm2 AS
SELECT ar_id FROM daten_import di
WHERE di.ar_id = ' || instr;
instr
是一个字符串。你需要在它周围加上引号。
EXECUTE IMMEDIATE
'CREATE OR REPLACE VIEW interm2 AS
SELECT ar_id FROM daten_import di
WHERE di.ar_id = ''' || instr || '''';
-- ^^ ^^
-- || ||
-- -- escaped quotes--