Postgres error updating column data - sql

Trying to run a update script on a table, but getting an error:
ERROR: column "ok" does not exist
LINE 2: SET first_name="ok", last_name="pk", email="ooo", phone="...
CREATE TABLE employee (
employee_id SERIAL PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
phone varchar(255)
);
INSERT INTO employee(
first_name, last_name, email, phone)
VALUES ('Kyle', 'Belanger', 'kbelanger#ok.com', '(240) 298-4664');
UPDATE "employee"
SET first_name="ok", last_name="pk", email="ooo", phone="000"
WHERE employee_id = 1;

There is no need to wrap table name in double quote "employee", and use single quotes for column values
UPDATE employee
SET first_name='ok', last_name='pk', email='ooo', phone='000'
WHERE employee_id = 1;
See Working Example

Try below sql:
UPDATE employee
SET first_name='ok', last_name='pk', email='ooo', phone='000'
WHERE employee_id = 1;
Table name was wrapped in double quotes which is not allowed.

Related

Error when trying to add multiple rows of data to table using SQL Query

I'm trying to insert multiple lines of data into a table that I created using SQL. Adding just one line into the table everything works fine. Whenever I put the comma after the first line in order to add more data that's when I get the error message "Incorrect syntax near ','." which is in reference to the first comma after the first line of data.
Copy of code:
CREATE TABLE Workers (
PersonID int NOT NULL IDENTITY PRIMARY KEY,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
Age int NOT NULL,
Specialty nvarchar(50)
)
INSERT INTO Workers (FirstName, LastName, Age, Specialty)
VALUES ('Marvin','Smith',30,'Bossman'),
('Jon','Reid',26,'Mentor'),
('Dexter','Akers',22,'New Guy'),
('John','Cena',26,'Hiding');

PostgreSQL: INSERT into and get the new ID for usage in LO-BASE

I'd like to add a line to a table:
CREATE TABLE actors (
id_act serial NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
CONSTRAINT actors_pkey PRIMARY KEY (id_act)
);
INSERT INTO actors (first_name, last_name) VALUES ('Tom', 'Hanks');
Using dBeaver, this statement provides the new ID:
select CurrVal(pg_get_serial_sequence('actors', 'id_act'));
With LibreOffice-BASE, I have to add the name of the scheme and this results in
ERROR: column "scheme_name.table_name" does not exist
I've got the same error using:
"scheme_name.table_name"
"scheme_name"."table_name"
"table_name"
How can I get the new ID for further usage (calculation, check, ...)? I don't mind to use CurrVal or RETURNING or something else. But I don't find the proper syntax.
Thank you!
The simplest option is to use the RETURNING clause in your INSERT query:
INSERT INTO actors (first_name, last_name) VALUES ('Tom', 'Hanks')
RETURNING id_act;
You can use an insert inside a CTE and then return the value:
WITH i AS (
INSERT INTO actors (first_name, last_name) VALUES ('Tom', 'Hanks')
RETURNING id_act
)
SELECT i.*
FROM i;
The outer query is a SELECT, so your UI should be comfortable with it returning a value.
You can even continue the processing in this statement -- by adding more CTEs for instance -- so you don't need to actually fetch the value.
There are three ways, which will provide you the new id's
Using SERIAL and PRIMARY KEY, postgres will automatically insert an unique value
CREATE TABLE actors (
id_act SERIAL PRIMARY KEY
first_name text NOT NULL,
last_name text NOT NULL
);
If you have sequence, you can use this sequence while creating DDL and every time data gets inserted,
new id will be generated
CREATE TABLE actors (
id_act integer NOT NULL DEFAULT nextval('sequence_name')
first_name text NOT NULL,
last_name text NOT NULL,
CONSTRAINT actors_pkey PRIMARY KEY (id_act)
);
If you have sequence, use the sequence in the insert query
CREATE TABLE actors (
id_act integer,
first_name text NOT NULL,
last_name text NOT NULL,
CONSTRAINT actors_pkey PRIMARY KEY (id_act)
);
INSERT INTO actors (id_act, first_name, last_name) VALUES (nextval('sequence_name'), 'Tom', 'Hanks');

SQL: Can't create function returning table. "Table is not valid at this position, expecting: bit, bool, boolean, ...."

I can't figure out what's wrong with this SQL function... in "returns table" it complains that "Table is not valid at this position, expecting: bit, bool, boolean, ....". Does anyone know?
create table department(
dept_name varchar(20),
n_prof int,
budget numeric(10,2),
primary key (dept_name)
);
create table instructor(
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department (dept_name)
);
create function instructors (dept_name char(20))
returns TABLE
as
return
select ID, name, dept_name, salary
from instructor
where instructor.dept_name=dept_name;
Based on error, I guess you are using MYSQL database.
As per documentation on user defined functions in MySQL
you can only return values of type {STRING|INTEGER|REAL|DECIMAL}
CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL}
SONAME shared_library_name
If you want to read a select resultset you have to define a procedure but not function.
DELIMITER //
DROP PROCEDURE IF EXISTS myProcedure //
CREATE PROCEDURE
myProcedure( dept_name char(20) )
BEGIN
select ID, name, dept_name, salary
from instructor
where instructor.dept_name=dept_name;
;
END
//
DELIMITER ;
And you can call procedure like
call myProcedure( 'CSE' )
That returns implicit objects based on the statements used in the procedure.
Please use below query.
USE Your_Database
create table dbo.department(
dept_name varchar(20),
n_prof int,
budget numeric(10,2),
primary key (dept_name)
);
GO
create table dbo.instructor(
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department (dept_name)
);
GO
create function dbo.instructors (#dept_name char(20))
returns TABLE
as
return
select ID, name, dept_name, salary
from dbo.instructor
where instructor.dept_name=#dept_name;
You have used dept_name which is already existing in your table column so use #dept_name instead of that.
Please use GO statement to separate the each transaction otherwise you will get error.
Msg 111, Level 15, State 1, Line 19 'CREATE FUNCTION' must be the
first statement in a query batch. Msg 137, Level 15, State 2, Line 25
Must declare the scalar variable "#dept_name".

PL SQL Parameter List Empty

If I have a PL SQL procedure like this:
Create Or Replace Procedure get_age (first_name varchar(40), last_name varchar(50))
Begin
Select age
From Person
Where first = first_name AND last = last_name;
End;
It is not guaranteed that the user will pass in a value for the first_name and last_name variable.
How do I account for this in the Procedure above since I do not want the first_name or last_name in the Where clause of my query if either one of those variables do not have a value.
Your query is good as it is now, you can accept null values in your WHERE clause:
Where (first = first_name OR first_name is NULL) AND (last = last_name OR last_name in NULL);
This way the user can enter first and last names, first or last names only or neither and results will be selected as expected.
Create Or Replace Procedure get_age (first_name varchar(40) default null, last_name varchar(50) default null)
Begin
Select age From Person Where first = nvl(first_name,first) AND last = nvl(last_name,last);
End;

oracle error: not enough values

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.