Ask Your Question
0

SQL Command help [closed]

asked 2017-09-11 02:00:02 +0200

Joe Castor gravatar image

updated 2017-09-11 02:19:11 +0200

I am trying to use SQL command to create a new table that is like an existing table except to add a new key, and to include data for records meeting a certain criteria (NotActive =TRUE). I try to execute the command first without an AS and I receive an error saying an AS is required. I add the AS and the error says "unexpected token AS." Am I misunderstanding how this works?

CREATE TABLE "RetiredMembers" (ID  INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1)  PRIMARY KEY, LIKE "MemberMaster" INCLUDING DEFAULTS EXCLUDING IDENTITY)  AS  (SELECT * FROM "MemberMaster" WHERE "NotActive"  = TRUE)  WITH DATA
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Joe Castor
close date 2017-09-12 01:10:57.688332

1 Answer

Sort by » oldest newest most voted
0

answered 2017-09-11 06:39:21 +0200

Ratslinger gravatar image

updated 2017-09-11 08:15:36 +0200

Having tried variations of you statement and comparing to manual, don't see a way for that combination to work. I did have success with the following:

DROP TABLE "RetiredMembers";

CREATE TABLE "RetiredMembers" 
AS
(SELECT * FROM "MemberMaster" WHERE "NotActive" = TRUE)
WITH DATA;

ALTER TABLE "RetiredMembers" ADD ID   INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1);

ALTER TABLE "RetiredMembers" ADD PRIMARY KEY (ID);

The first statement drops the existing "RetiredMembers' table (needed many times in tests). The second statement (as in the manual) creates the new table with the specific data wanted. The third table adds the new field (at end of fields) and inserts the auto-increment number in each record. The final statement make the new field the primary key.

Edit:

Here is another method - create DB then insert wanted records. This will put ID field as first field:

DROP TABLE "RetiredMembers";

CREATE TABLE "RetiredMembers" 
(
  ID   INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1) PRIMARY KEY,
  LIKE "MemberMaster" INCLUDING DEFAULTS EXCLUDING IDENTITY
);

INSERT INTO "RetiredMembers" ( "MemberID", "FirstName", "AlternateFirstName","LastName",
"Address", "City","StateOrProvince","PostalCode", "SecondaryAddress","SecondaryCity",
"SecondaryStateProvince", "SecondaryPostalCode", "MemberType", "MemberThruDate", 
"ALLeagueMember","DateJoined", "LastRenewalDate", "NwsltrByMail", "PrimaryID",
 "DateLeft", "ReasonLeft", "NotActive", "Family", "CommentsLeft", "Reflect", "Address2",
 "AltAddress2", "OfficerTitle", "EmailAddress")
 SELECT  * FROM "MemberMaster" WHERE "NotActive"  = TRUE;

The only way (at least at this point) for the insert to work was with individual fields. Tried with DEFAULT for the auto-increment ID field but kept getting errors. I believe this is also part of the problem with your original statement.

edit flag offensive delete link more

Comments

BTW - Where a field is located in a table makes no difference. Many people complain and I believe with some obscure process you can re-position the field.

Ratslinger gravatar imageRatslinger ( 2017-09-11 06:41:47 +0200 )edit

Thank you very much. I like your first solution. Now I can just rename a few of the queries and reports for MemberMaster and only change the table name inside. I ran it and it failed with an error "table RetiredMembers doesn't exist," so I created a dummy table, reran the code and it Worked great. Now to finish this retired member job; flag PhoneNumbers & PaymentHistory tables from/if MemberMaster is inactive and create tables using above. Don't understand how to do it yet. Again, thank you

Joe Castor gravatar imageJoe Castor ( 2017-09-11 15:09:18 +0200 )edit

When trying to run your second option, I get the following error:1: user lacks privilege or object not found: MemberID

Joe Castor gravatar imageJoe Castor ( 2017-09-11 15:36:01 +0200 )edit

The first solution has four statements in it. The first being to drop (delete) the existing table. If starting out without a table this first statement is not needed. That is why you got the 'doesn't exist' error. There was no need to create a dummy table, just eliminate the Drop statement.

In the second error, that is usually caused because of a mismatch in field names. I can duplicate the error by renaming 'MemberID' to MembrID' for example.

Ratslinger gravatar imageRatslinger ( 2017-09-11 16:34:20 +0200 )edit

FYI - If running the first set (of four) statements in SQL Workbench/J, a message would have appeared stating the table didn't exist and asking to ignore the error (thus continuing the process). An answer of 'Ignore this error' completes the remaining statements again eliminating the need to create the dummy table.

Ratslinger gravatar imageRatslinger ( 2017-09-11 16:40:45 +0200 )edit

Don't know if you are still having a problem with deletes. I have no problem using this statement:

DELETE FROM "RetiredMembers" WHERE "MemberID" = 0
Ratslinger gravatar imageRatslinger ( 2017-09-11 17:11:59 +0200 )edit

Using the TOOLS > SQL from the Base main page (still not up to speed on Workbench/J yet), When running the first set without the Drop, I get the error 1: user lacks privilege or object not found: PUBLIC.RetiredMembers That's why I had to create the initial dummy table. I recopied an reran the second set and all ran successfully. Question? Where or how can these be stored so they can be run in the future? Are the created as a fake query?

Joe Castor gravatar imageJoe Castor ( 2017-09-11 19:45:28 +0200 )edit

When running from Tools->SQL you must run each statement separately or you will get that error. Just ran test & individual statements ran without problem.

As far as storage of these statements, there is none in Base. You either save them in a text file, create a DB to save them in or, as I prefer, save them in Workbench/J (can create different work spaces for various reasons).

Ratslinger gravatar imageRatslinger ( 2017-09-11 21:57:24 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-11 02:00:02 +0200

Seen: 191 times

Last updated: Sep 11 '17