Nous essayons de construire un script qui vérifie automatiquement si une table est déjà créée et si c'est le cas, le script doit se terminer. Si la table n'est pas créée, le script doit la créer.
Error starting at line : 3 in command -
create table FEZ.GPS_TRACKER_DATA as select * from KAKATEL.GPS_POSITION
else END
Error report -
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
c'est l'erreur que nous obtenons
case
if select count(*) from ALL_TAB_COLS where table_name = 'GPS_TRACKER_DATA' = 0 then
create table AZUBI.GPS_TRACKER_DATA as select * from KAKATEL.GPS_POSITION
else end
3 Réponses :
Cela doit être fait dans le bloc PL / SQL:
DECLARE
V_CNT NUMBER := 0;
BEGIN
SELECT
COUNT(*)
INTO V_CNT
FROM
ALL_TABLES -- USE ALL_TABLES
WHERE
TABLE_NAME = 'GPS_TRACKER_DATA'
AND OWNER = 'AZUBI'; -- ADDED OWNER CONDITION
IF V_CNT = 0 THEN
EXECUTE IMMEDIATE 'create table AZUBI.GPS_TRACKER_DATA as
select * from KAKATEL.GPS_POSITION';
END IF;
END;
/
Cheers !!
Hé, si je voulais ajouter un PK pour une colonne, comment ferais-je cela?
Une option serait:
declare
v_cnt pls_integer;
begin
select count(*)
into v_cnt
from ALL_TAB_COLS
where table_name = 'GPS_TRACKER_DATA';
case
when v_cnt = 0 then
execute immediate 'create table AZUBI.GPS_TRACKER_DATA as
select * from KAKATEL.GPS_POSITION';
end case;
end;
/
exécution immédiate est nécessaire pour une instruction DDL dans un PL / SQL
bloquer. Vous pouvez remplacer if..then..end if par case..when then..end case , également:
XXX
Vous n'avez pas besoin d'interroger le dictionnaire de données pour voir si la table existe; essayez simplement de créer la table en utilisant EXECUTE IMMEDIATE et attrapez l'exception si elle existe déjà.
DECLARE
ALREADY_EXISTS EXCEPTION;
PRAGMA EXCEPTION_INIT( ALREADY_EXISTS, -955 );
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE new_table AS SELECT * FROM existing_table';
EXCEPTION
WHEN ALREADY_EXISTS THEN
NULL;
END;
/
db fiddle ici