Looking for help solving an INSERT INTO error - sql

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

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');

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;

sql query to create a table at runtime and insert the values in it from the select statement from the database

what i am tryin to do is make a table(#tbl) runtime and insert the data from the select statement from the database,as what i have done so far is
declare #tbl TABLE (
Item int
)
begin
insert into #tbl values select cid from tbl_custumer where cus_ph like '%'+'987'+'%'
select * from #tbl
end
as "select cid" statement returns multiple records
I think you might want the code to look like this:
begin
declare #tbl TABLE (
Item int
);
insert into #tbl(Item)
select cid
from tbl_custumer
where cus_ph like '%'+'987'+'%';
select *
from #tbl;
end;
Notes:
The begin/end block is not really necessary, but I'm guessing you want it for other reasons (a stored procedure, if, or something similar).
The values keyword is not needed when using insert . . . select.
Use semicolons at the end of each SQL statement. Although they are optional, they make the code easier to follow.

ERROR: syntax error at or near "SELECT"

I am really new to postgres. The question looks very simple but I just cant see where I got wrong.
I a table created as follows:
CREATE TABLE IF NOT EXISTS t(
tn VARCHAR(30) NOT NULL,
PRIMARY KEY(tn)
);
I want to insert an instance if the instance does not exist. Here is my code:
INSERT INTO t (tn)
VALUES
(SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q')) ;
And the psql console keeps giving me the error
ERROR: syntax error at or near "SELECT"
I have checked every piece of code individually, for instance both
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');
and
INSERT INTO t (tn) VALUES ('p');
run without error. But error occurs when I put them together.
Does anyone know where I got wrong..?
Lose VALUES and the brackets...
INSERT INTO t (tn)
SELECT 'q' WHERE NOT EXISTS (SELECT * FROM t WHERE tn = 'q');

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"