I am building a SQL query in Java and ending up with
INSERT INTO authorizations (id, typeid, s)
VALUES (HEXTORAW('1a225e19-8893-4068-958c-3d9914a34ca5'),
HEXTORAW('00000000-0000-0000-0000-000000000000'),
'1');
I am getting a ORA-01465 error. What is my dumb mistake I am doing?
Remove the hyphens...
INSERT INTO authorizations (user_id, source_id, access_level) VALUES (HEXTORAW('1a225e1988934068958c3d9914a34ca5'), HEXTORAW('00000000000000000000000000000000'), '1');
Related
I have two tables, cinema and theater.
Table Cinema
Columns:
id, name, is_active
Table Theater
Columns:
id, cinema_id
I'm doing insertion into the DB, in sequence. First, I'll insert into cinema and then into theater. The cinema_id.theater is a foreign key that reference cinema.id. After the insertion into cinema, I'll insert data into the theater, but I need the value from cinema's id before insert the data in cinema_id.
I was thinking about RETURNING id INTO cinema_id and, then, save into theater. But I really don't know how I can possibly do something like this.
Any thoughts? Is there any better way to do something like this?
You have two options.
The first one is using the lastval() function which returns the value of the last generated sequence value:
insert into cinema(name, is_active) values ('Cinema One', true);
insert into theater(cinema_id) values (lastval());
Alternatively you can pass the sequence name to the currval() function:
insert into theater(cinema_id)
values (currval(pg_get_serial_sequence('cinema', 'id')));
Alternatively you can chain the two statements using a CTE and the returning clause:
with new_cinema as (
insert into cinema (name, is_active)
values ('Cinema One', true)
returning id
)
insert into theater (cinema_id)
select id
from new_cinema;
In both statements I assume theater.id is also a generated value.
this way works.
with new_cinema as (
insert into cinema (name, is_active)
values ('Cinema One', true)
returning id
)
insert into theater (cinema_id)
select id
from new_cinema;
INSERT INTO tableB
(
columnA
)
SELECT
columnA
FROM
tableA
ORDER BY columnA desc
LIMIT 1
Working with a nodejs server, and I thought instead of having to do 2 method calls, with each separate function, I would call one query to add the 2 to the database. I've searched around abit on stack overflow for some questions regarding this, however they seem to not work.
My example would be that at the register phase, the user is inserted into the database, and then a sub user table called user_settings is added with a relational key to the user id of the created user.
One example that I tried but didn't work is this:
WITH ins1 AS (
INSERT INTO users(firstname, lastname)
VALUES ('foo', 'bar')
RETURNING id AS user_id
)
, ins2 AS (
INSERT INTO user_settings(user_id, adddetails)
SELECT user_id, 'foobar' FROM ins1
)
It doesn't seem to know the returning user_id when I try and put it in as a query.
Your CTE is missing a final statement. The second insert should be "outside" a CTE definition:
WITH ins1 AS (
INSERT INTO users(firstname, lastname)
VALUES ('foo', 'bar')
RETURNING id AS user_id
)
INSERT INTO user_settings(user_id, adddetails, "timestamp")
SELECT user_id, 'foobar', current_timetamp
FROM ins1;
I have these three tables for services, executed_services and a rating table, which rates the service. My rating table has a foreign key to executed_services, and so, I wanted to "copy" the service to be rated to the executed_services relation. I tried using the following procedure, but this select won't work, as it has to return only one result.
BEGIN
INSERT INTO servico_executado (id, data_abertura, id_solicitacao, id_profissional)
VALUES (SELECT id, data_abertura, id_solicitacao, id_profissional FROM servico WHERE id = NEW.id_servico);
DELETE FROM servico WHERE id = NEW.id_servico;
RETURN NEW;
END;
So, what should I do to get all the values from one table and insert on another? Or is there other way to do that?
You can do it very easily in one step with a CTE with the following simple query:
WITH deleted AS (DELETE FROM servico WHERE id = $1 RETURNING *)
INSERT INTO servico_executado (id, data_abertura, id_solicitacao, id_profissional)
SELECT id, data_abertura, id_solicitacao, id_profissional FROM deleted
RETURNING *;
There is no need to use PL/pgSQL. The id of the service moved is denoted by the placeholder $1 in the above query.
INSERT INTO servico_executado
SELECT id, data_abertura, id_solicitacao, id_profissional
FROM servico WHERE id = NEW.id_servico;
Do not use 'VALUES'. If you have more columns in the table, you can use null in the select statement for those missing columns.
SELECT
Q."COLUMN1"
FROM
(SELECT
"COLUMN1",
CAST ((SELECT CAST (RTRIM (PARAM) AS VARCHAR(50)) FROM TABLE_VIEW WHERE PARAM_ID = :ID) AS VARCHAR(50)) AS "COLUMN2"
FROM ("TABLE1")Q
WHERE
RTRIM(CAST("COLUMN1" AS CHAR(10))) IN (SELECT VALUE_1 FROM TABLE (SPLIT_PARAMS(CAST(Q."COLUMN2" AS VARCHAR(50)),',',5)))
COLUMN2 gets its value from a separate table based on the input provided at run time.
The filter used in the query consists of a used defined table valued function that is used to split the comma separated valued to individual values.
The query throws the error message as:
"FUNCTION NOT SUPPORTED. SQLCODE=-270, SQLSTATE=42997"
Can anyone help me find the cause of the issue.
I am trying to Insert few lines into my db using this ex.
INSERT INTO product_line
(product_line_ID, m_product_id,
process_type, process_time)
VALUES
(get_uuid(),(SELECT M_PRODUCT_ID FROM M_PRODUCT WHERE product_name='DKP'), //this will return multiple results
'L', now());
I know that I will get Multiple Results from select query Error, But I want it to insert same for all results got from select query.
How do i do this?
Insert same statement for all Products came in select loop?
Do you mean something like:
INSERT INTO ...
SELECT get_uuid(), m_product_id, 'L', now()
FROM m_product
WHERE ...
This will insert one row for each you select from m_product.