Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am working with a postgreSQL database in VSCode via the SQL Tools extension. I have the following query, but it does not execute. I suspect the reason is because I am not "chaining" the queries properly.
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
)
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
I receive the following error: syntax error at or near "INSERT" at character 146. If I were to separate the commands and then run each separately, this error does not arise.
Ideally, I could execute the one query instead of having to first execute a CREATE TABLE and then executing an INSERT. Is there a proper terminology for doing this kind of chaining of queries?
missing semicolon";" after create statement :
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
); --<-- here
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
You just missed the semicolon(;) after create table statement.
-- #block create employee table
-- #conn myfirstconnection
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
Db-Fiddle:
CREATE TABLE test_table(
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
INSERT INTO test_table(
first_name,
last_name
)
VALUES ('Samantha', 'Gray');
Select query:
select * from test_table;
Output:
id
first_name
last_name
1
Samantha
Gray
db<fiddle here
You can run multiple queries at once but each query still need to be defined properly and especially ending with a ;.
How to run multiple queries at once depends a lot on the way you run them but from VSCode I suppose you have to select both queries and click on some button (as opposed to not selecting the query or only selecting one).
Related
I created a simple table with one PRIMARY Key column and two VARCHAR columns
CREATE TABLE student(
student_id INT PRIMARY KEY,
fName VARCHAR(20) NOT NULL,
lName VARCHAR(20)
);
INSERT INTO student VALUES(1, "Rahul", "Krub");
INSERT INTO VALUES(&student_id, &fName, &lName);
SELECT * FROM student;
I have no idea why the latter is not working while the former does? I want to dynamically enter data into the table (User Inputted). Please help me out I cannot find the answer for this anywhere as all of my searches keep leading me to dynamic SQL.
Evening, needing assistance regarding triggers.
Within my development environment I have two tables, one containing employee data (which contains various data errors that will be amended via ALTER TABLE) and the log table.
How do i go about designing a trigger that will update multiple rows contained within the log table such as 'issue_status','status_update_date' when ALTER TABLE sql is used to amend the data contained in the first data table?
-- employee table
CREATE TABLE emp(
emp_id INTEGER NOT NULL,
emp_name VARCHAR(30),
emp_postcode VARCHAR(20),
emp_registered DATE,
CONSTRAINT pk_emp PRIMARY KEY (emp_id));
-- SQL for the log table
CREATE TABLE data_log
(issue_id NUMBER(2) NOT NULL,
table_name VARCHAR2(20),
row_name VARCHAR2(20),
data_error_code NUMBER(2),
issue_desc VARCHAR2(50),
issue_date DATE,
issue_status VARCHAR2(20),
status_update_date DATE);
-- example log insert
INSERT INTO data_log( SELECT DI_SEQ.nextval, 'emp', emp.emp_id, '1', 'emp_name case size not UPPER', SYSDATE, 'un-fixed', '' FROM emp WHERE emp_name != UPPER(emp_name));
This is the example of the issue inserted into the log table. All i want to do is if I update the emp table to set 'emp_name' to Upper the trigger will register this update and change the rows 'issue_status' and 'status_update_date' to 'fixed' and the 'sysdate' of when the change was made
I've done some browsing however i'm still struggling to understand, any literature recommendations would be appreciated also.
Thanks in advance for the assistance.
Note that we don't use ALTER TABLE to update the rows of a table as you have mentioned.We use Update statement. Your trigger should be a BEFORE UPDATE TRIGGER like this.
CREATE OR REPLACE TRIGGER trig_emp_upd BEFORE
UPDATE ON emp
FOR EACH ROW
WHEN ( new.emp_name = upper(old.emp_name) )
BEGIN
UPDATE data_log
SET
issue_status = 'fixed',
status_update_date = SYSDATE
WHERE
row_name =:new.emp_id;
END;
/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am having some difficulties with Oracle and Inner Joins:
I am trying to do the following "List patientID and ages of patients who are allergic to tree nuts.
I have set up the following query:
SELECT patientID, age
2 FROM PATIENT
3 JOIN Allergy on patient.patientID=allergy.patientID
4 WHERE Allergy ='Tree Nuts';
JOIN Allergy on patient.patientID=allergy.patientID
*
ERROR at line 3:
ORA-00904: "ALLERGY"."PATIENTID": invalid identifier
THE DDL for patient and allergy are as follows:
CREATE TABLE Patient
(patientID NUMBER NOT NULL,
patientMRN VARCHAR2(30) NOT NULL,
lastName VARCHAR2(30),
firstName VARCHAR2(30),
age NUMBER,
gender VARCHAR2(10),
street VARCHAR2(60),
city VARCHAR2(60),
state VARCHAR2(30),
zip VARCHAR2(15),
CONSTRAINT patient_PK PRIMARY KEY (patientID));
CREATE TABLE Allergy
(allergyID NUMBER NOT NULL,
allergyName VARCHAR2(60) NOT NULL,
type VARCHAR2(30),
CONSTRAINT allergy_PK PRIMARY KEY (allergyID));
CREATE TABLE PatientAllergy
(
patientID NUMBER NOT NULL,
allergyID NUMBER NOT NULL,
notedDate DATE,
severity VARCHAR2(30),
CONSTRAINT patientallergy_PK PRIMARY KEY (patientID, allergyID),
CONSTRAINT patientallergy_FK1 FOREIGN KEY (patientID)
REFERENCES Patient(patientID) ON DELETE CASCADE,
CONSTRAINT patientallergy_FK2 FOREIGN KEY(allergyID)
REFERENCES Allergy(allergyID) ON DELETE CASCADE
)
I am not sure what I am doing to obtain this error.
I tried this query:
SELECT patientID, age
FROM Patient, PatientAllergy
WHERE patient.patientID=patientallergy.patientID AND Allergy = 'Tree Nuts';
I am still getting an error of: ERROR at line 4: ORA-00904: "ALLERGY": invalid identifier
You're missing a table in your query and not qualifying the column name in your WHERE clause correctly. Table aliases also make your query more readable. The below query should work for you:
SELECT p.patientID, p.age
FROM patient p
JOIN patientallergy pa ON p.patientid = pa.patientid
JOIN allergy a ON a.allergyid = pa.allergyid
WHERE a.allergyname = 'Tree Nuts'
The error is pretty clear, you're using a column [patientID] that doesn't exist in the table Allergy.
Your table Allergy only have the following columns (if I refer to your script):
allergyID
allergyName
type
Tell me if I'm wrong but it seems to be the reason of your problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm SORRY everyone, It wasn't allowing me to comment. you are right. It's the ALTER I'm having issues with. How do I add the Cumulative GPA column to the table (using the alter statement) where the gpa is displayed between 0.00 and 4.00
CREATE database "IS4440_DuBoseJasmine"
CREATE TABLE StudentInformation (
StudentID CHAR(7) not null,
StudentSSN CHAR(9) null,
StudentFirstName VARCHAR(50) null,
StudentLastName VARCHAR(50) null,
StudentMiddleName VARCHAR(50) null,
StudentHomeCountry CHAR(2) not null
)
/*2*/
ALTER TABLE StudentInformation ADD Cumulative GPA ;
/*3*/
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1352154', 'DuBose', 'Jasmine', 'Leigh', 'US')
INSERT INTO StudentInformation (StudentID, StudentLastName, StudentFirstName, StudentMiddleName, StudentHomeCountry)
VALUES ('1234565', 'Smith', 'Johnny', 'Apple', 'GB');
/*4*/
UPDATE StudentInformation SET StudentSSN = 123456789
WHERE StudentID = 1352154;
The update works fine (demonstrated on SQL Fiddle), it is the ALTER TABLE statement that fails. This line:
ALTER TABLE StudentInformation ADD Cumulative GPA;
should be:
ALTER TABLE StudentInformation ADD [Cumulative GPA] INT -- OR WHATEVER TYPE IT SHOULD BE;
ALTER TABLE Documentation
As an aside, although this may just be an example, if it isn't you will want to USE your database before you create the table:
CREATE database "IS4440_DuBoseJasmine";
GO
USE IS4440_DuBoseJasmine;
CREATE TABLE ...
Otherwise you will just be creating your table in whatever database you are connected to.
Your update query is fine, Your ALTER statement what fails, You are adding new column to your table without any datatype or constrains, try this to alter your table
You can use Numeric(3,2) or Decimal(3,2), that means total three digits two after decimal point so a max of 9.99 .
ALTER TABLE StudentInformation ADD CumulativeGPA Numeric(3,2) NULL
To alter your table to add new column make that column NULLABLE and while naming your column do not give spaces.
To make column type Not Nullable
ALTER TABLE StudentInformation ALTER COLUMN CumulativeGPA DECIMAL(3,2) NULL ;
i have a table donor_master:
create table donor_master
(
donor_id number(10) primary key not null,
dob date not null,
age number(3) not null,
gender char(1) not null,
blood_group char(3),
contact_no number(10),
address varchar(50) not null,
city varchar(10) not null,
pin number(10) not null,
state varchar(10) not null,
branch_registration_id number(5) references branch_master(branch_id)
);
when i try to insert into the table in a procedure insert_donor_master, i get "not enough values" error on compilation.
this is the procedure:
create or replace procedure insert_donor_master(
vdob donor_master.dob%type,
vage donor_master.age%type,
vgender donor_master.gender%type,
vblood_group donor_master.blood_group%type,
vcontact_no donor_master.contact_no%type,
vaddress donor_master.address%type,
vcity donor_master.city%type,
vpin donor_master.pin%type,
vstate donor_master.state%type,
vbranch_registration_id donor_master.branch_registration_id%type
)
is
begin
insert into donor_master values (sq_donor_master.nextval, vdob, vage, vgender, vblood_group, vcontact_no, vaddress, vcity, vpin, vstate, vbranch_registration_id);
commit;
end;
What is the problem?
Thanks.
Oracle hurls ORA-00947 when we specify an INSERT statement which doesn't have a value for every column in the table.
Now, the CREATE TABLE statement you posted shows a table with eleven columns. And the stored procedure code you posted shows an insert statement with eleven values in the VALUES (...) clause.
So, the explanations are:
you have a configuration management issue, and you're running the wrong version of the stored procedure or the wrong version of the table
you have a configuration management issue, and the actual structure of the table isn't what you think it is (doesn't match your CREATE TABLE script)
you aren't really getting an ORA-00947 error
Note that if you don't want to populate every row you can specify a projection of the relevant columns before the VALUES clause. For instance, if you just wanted to populate the mandatory columns you would code this:
insert into donor_master
(donor_id, dob, age, gender, address, city, pin, state )
values (sq_donor_master.nextval, vdob, vage, vgender, vaddress, vcity, vpin, vstate)
All that matters is that the number of values matches the number of columns.
The complete syntax for INSERT statements is in the documentation. enter link description hereFind out more.