在 PL/SQL 中创建表? [英] Create Table As within PL/SQL?
问题描述
我正在尝试在 PL/SQL 中创建一个表
I'm trying to create a table within PL/SQL
我如何才能做到这一点?
how I can achieve that?
继续获取
错误报告:
ORA-00933:SQL 命令未正确结束"
这是我有错误的代码
DECLARE
station_id_ms1 NUMBER :=10347;
realtime_start DATE :=to_date('2012-01-01 00:00:00','YYYY-DD-MM HH24:MI:SS');
realtime_end DATE :=to_date('2012-07-01 00:00:00','YYYY-DD-MM HH24:MI:SS');
BEGIN
EXECUTE IMMEDIATE ('
CREATE TABLE new_table_name
AS
SELECT
((realtime - to_date(''01-JAN-1970'',''DD-MON-YYYY'')) * (86400)) AS realtime_ms1,
magnetic_ms_id,
ADC_value_pp_2_mgntc_fld_amp(ch2_value,ch2_gain_value,magnetic_ms_id,2) AS B_x_ms1,
ADC_value_pp_2_mgntc_fld_amp(ch1_value,ch1_gain_value,magnetic_ms_id,1) AS B_y_ms1,
real_nanosecs2*4/3*360/20e6 AS phase_x_ms1,
real_nanosecs1*4/3*360/20e6 AS phase_y_ms1
FROM
raw_mag
WHERE
magnetic_ms_id = '||station_id_ms1||'
AND realtime > '||realtime_start||'
AND realtime < '||realtime_end||'
AND ch1_tune_value = 0
AND realtime < pkg_timezone.change_timezone(gettime,''CET'',''UTC'')
');
END;
推荐答案
你应该在你立即执行的 plsql-string 内 做 char-to-date 转换.
You should do the char-to-date conversion within the plsql-string that you excecute immediate.
您声明的日期将在串联中反向转换"为 varchar2 并再次重新转换"为日期以执行 create table 语句.并且在这两个转换中可能会发生各种各样的事情",因此您要确保在转换为日期时控制字符串的解释方式.
The date you declared will be "back-cast" to a varchar2 in the concatenation and "re-cast" into a date again for the execution of the create table statement. And "all sorts of things" can happen in these two casts, so you want to make sure you're in control how the character-string is interpreted when cast to a date.
DECLARE
station_id_ms1 NUMBER :=10347;
realtime_start VARCHAR2(100) :='2012-01-01 00:00:00';
realtime_end VARCHAR2(100) :='2012-07-01 00:00:00';
BEGIN
EXECUTE IMMEDIATE ('
CREATE TABLE new_table_name
AS
SELECT
((realtime - to_date(''01-JAN-1970'',''DD-MON-YYYY'')) * (86400)) AS realtime_ms1,
magnetic_ms_id,
ADC_value_pp_2_mgntc_fld_amp(ch2_value,ch2_gain_value,magnetic_ms_id,2) AS B_x_ms1,
ADC_value_pp_2_mgntc_fld_amp(ch1_value,ch1_gain_value,magnetic_ms_id,1) AS B_y_ms1,
real_nanosecs2*4/3*360/20e6 AS phase_x_ms1,
real_nanosecs1*4/3*360/20e6 AS phase_y_ms1
FROM
raw_mag
WHERE
magnetic_ms_id = '||station_id_ms1||'
AND realtime > to_date(''' || realtime_start || ''', ''YYYY-DD-MM HH24:MI:SS'')
AND realtime < to_date(''' || realtime_end || ''', ''YYYY-DD-MM HH24:MI:SS'')
AND ch1_tune_value = 0
AND realtime < pkg_timezone.change_timezone(gettime,''CET'',''UTC'')
');
END;
这篇关于在 PL/SQL 中创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!