Nogle platforme understøtter ikke dollarnotering. I dit specifikke eksempel skal du have et semikolon efter den sidste END
. Du skal muligvis tilføje en DECLARE
erklæring også.
DO
$$
DECLARE
BEGIN
IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'categories'))
THEN
CREATE TABLE IF NOT EXISTS categories
(
id SERIAL NOT NULL,
name character varying(40),
CONSTRAINT categories_pkey PRIMARY KEY (id)
);
INSERT INTO categories (name) VALUES ('Games');
INSERT INTO categories (name) VALUES ('Multimedia');
INSERT INTO categories (name) VALUES ('Productivity');
INSERT INTO categories (name) VALUES ('Tools');
INSERT INTO categories (name) VALUES ('Health');
INSERT INTO categories (name) VALUES ('Lifestyle');
INSERT INTO categories (name) VALUES ('Other');
END IF;
END;
$$ LANGUAGE PLPGSQL;
For platforme, der ikke genkender dollarnotering, kan du bruge '
i stedet. Du bliver nødt til at undslippe enhver '
dog i kroppen af den anonyme funktion.
Sådan:
DO
'
DECLARE
BEGIN
IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ''categories''))
THEN
CREATE TABLE IF NOT EXISTS categories
(
id SERIAL NOT NULL,
name character varying(40),
CONSTRAINT categories_pkey PRIMARY KEY (id)
);
INSERT INTO categories (name) VALUES (''Games'');
INSERT INTO categories (name) VALUES (''Multimedia'');
INSERT INTO categories (name) VALUES (''Productivity'');
INSERT INTO categories (name) VALUES (''Tools'');
INSERT INTO categories (name) VALUES (''Health'');
INSERT INTO categories (name) VALUES (''Lifestyle'');
INSERT INTO categories (name) VALUES (''Other'');
END IF;
END;
' LANGUAGE PLPGSQL;
DBFiddle for at se et fungerende eksempel.