multiple select inserts - sql

I need to be able to have multiple select statements or values for my insert. The following works if I remove the second column name (notication_timestamp).
insert into notification (msg,notification_timestamp) select msg from data;
I want to be able to say:
insert into notification (msg,notification_timestamp) values (select msg from data,now());
But that doesn't work. Any ideas?

Remove the values statement:
Insert into notification(msg, notification_timestamp)
Select msg, now()
from data

you didn't put notification_timestamp on select query
insert into notification (msg,notification_timestamp) select msg,'value for notification_timestamp' from data;
So it may
insert into notification (msg,notification_timestamp) select msg,now() from data;

Related

I am getting Syntax error in sql (insert)

So, I am trying to add a data if a value in field does not exist. I am keep getting syntax error and not sure where I am getting it wrong.
INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
VALUES ('test','010-4843-0000','www.company.com')
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');
This is my code.
I am using H2 database
You're trying to combine a values table constructor with syntax of a select query
You can insert into a table using select:
INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
SELECT 'test','010-4843-0000','www.company.com'
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');

How to insert values into a postgres table using for loop?

I have a script like this in postgres
begin;
INSERT INTO "schema"."table"(price, different_table_foreign_key)
VALUES
(1, 1)
end;
for testing purposes I want to fill table 100 times with the same values as seen above.
how can I do this using a for loop?
No need for a loop, you can use generate_series() for that:
INSERT INTO "schema"."table"(price, different_table_foreign_key)
select 1,1
from generate_series(1,100);
If you want a different value for each row, just use the one returned by `generate_series()
INSERT INTO "schema"."table"(price, different_table_foreign_key)
select 1, g.value
from generate_series(1,100) as g(value)

Looking for help solving an INSERT INTO error

I have a small sample query that creates a temp table with a WITH command, and then runs a SELECT.
I want to INSERT INTO another table the result of my SELECT statement, but I am getting an error
WITH testingINSERT AS
(
SELECT *
FROM Dashboard.test1
)
INSERT INTO Dashboard.test2 (number)
SELECT *
FROM Dashboard.test1
The WITH statement in this case isn't really doing anything. However, I am trying to solve for the issue.
If I remove the INSERT line, the query runs fine
ERROR: Syntax error: Expected "(" or keyword SELECT but got keyword INSERT at [6:1]
Tried to be more explicit as well, and see the same error.
WITH testingINSERT AS
(
SELECT *
FROM Dashboard.test1
)
INSERT INTO Dashboard.test2 (number)
SELECT number
FROM Dashboard.test1
Tried this as well:
WITH testingINSERT AS
(
SELECT number
FROM Dashboard.test1
)
INSERT INTO Dashboard.test2 (number)
SELECT number
FROM testingINSERT
If I removve that line INSERT INTO, everything works fine, however, I am trying to put the returned values into another table.
INSERT INTO Dashboard.test2 (number)
WITH testingINSERT AS
(
SELECT number
FROM Dashboard.test1
)
SELECT number
FROM testingINSERT

Error while insert into hive table using rand() function

I tried below snippet to insert random values in fileid column.
I got error like
//cannot recognize input near 'AS' 'floor' '(' in selection target)//
Can anyone help me out .
Select floor(RAND()*(99999-10000)+10000); //works fine though.
I only got issue at insert time.
INSERT INTO table test.a1
SELECT
Fileid AS floor(RAND()*(99999-10000)+10000)
FROM
test.a2;
You mixed up alias with column ref.
It should be:
INSERT INTO table test.a1
SELECT
Floor(RAND()*(99999-10000)+10000) as fileid
FROM
test.a2;

Access INSERT with nested SELECT

Why does the following SQL statement not work?
INSERT INTO dialog (speaker, dialog_text) VALUES (
(
SELECT FIRST(id)
FROM FIGURE
WHERE char_name="Doe" AND forename="John"
),
"Some text"
);
It produces this error:
Query input must contain at least one table or query.
The single SELECT statement works.
An Access SQL INSERT ... VALUES statement will not let you use a subquery for one of the VALUES
Switching to an INSERT ... SELECT statement, as Piotr suggested will work.
Or you could use an Access Domain Aggregate function, instead of a subquery, in your INSERT ... VALUES statement:
INSERT INTO dialog (speaker, dialog_text)
VALUES (
DMin("id", "FIGURE", "char_name='Doe' AND forename='John'"),
'Some text'
);
Following works:
INSERT INTO dialog (speaker, dialog_text)
SELECT FIRST(id), "Some text"
FROM FIGURE
WHERE char_name="Doe" AND forename="John"