Access INSERT with nested SELECT - sql

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"

Related

Insert a column with single quote or Apostrophe in Oracle

I am trying to insert into Table Users from Person table.
However, The first_name column in the person table contains apostrophe in the name (Eg- Rus'sell) which is preventing me from successful insertion. How do I fix this?
INSERT INTO USERS VALUES (SELECT FIRST_NAME,.........FROM PERSON);
INSERT INTO USERS VALUES (SELECT FIRST_NAME,.........FROM PERSON);
First of all, your insert statement is syntactically incorrect. It will raise ORA-00936: missing expression. The correct syntax to insert multiple records from source table is:
INSERT INTO table_name SELECT columns_list FROM source_table;
The VALUES keyword is used to insert a single record into table using following syntax:
INSERT INTO table_name(columns_list) VALUES (expressions_list);
If you already have the value stored in another table, then simple INSERT INTO..SELECT FROM should work without any issues. However, if you are trying to INSERT INTO..VALUES having single quotation marks, then the best way is to use Quoting string literal technique The syntax is q'[...]', where the "[" and "]" characters can be any of the following as long as they do not already appear in the string.
!
[ ]
{ }
( )
< >
You don't have to worry about the single-quotation marks within the string.
create table t(name varchar2(100));
insert into t values (q'[Rus'sell]');
insert into t values (q'[There's a ' quote and here's some more ' ' ']');
select * from t;
NAME
-----------------------------------------------
Rus'sell
There's a ' quote and here's some more ' ' '
I don't think your question is showing the complete details, because I can execute the following statements without any problem:
create table person( first_name varchar2(100));
create table users( first_name varchar2(100));
insert into person values ('Rus''sell');
insert into users select first_name from person;
Apologies for the obscurity if any in the question. The query I was working with was a long insert query with multiple joins.
To sum it was a stored proc where I was doing an insert, for which the data is given by long select query with multiple joins. One of the column is the FIRST_NAME column which had some values with Apostrophe in it (Rus'sell, Sa'm).
The Insert statement values were being generated as below which was causing an 'ORA-00917: missing comma' error.
INSERT INTO TABLE_NAME values (314159,0,'Rus'sell','Parks','...........)
I fixed this by Replacing the column in the select from a single quote to two single quotes, before giving it to the insert statement which basically solved the issue.
REPLACE(FIRST_NAME,'''','''''') AS FIRST_NAME
Hope it helps.

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

Missing select keyword in Oracle SQL

I am getting this error
SQL Error: ORA-00928: missing SELECT keyword
00928. 00000 - "missing SELECT keyword"`
when I am trying to insert like this
create table certf
(
certificate_id integer primary key,
certificate_name varchar(100) not null,
certificate_content varchar(300) not null
);
insert into certf (&certificate_id, &certificate_name, &certificate_content);
You are missing values actually:
insert into certf (certificate_id, certificate_name, certificate_content)
values (&certificate_id, &certificate_name, &certificate_content);
Notice that I also added the column list to the insert. This is a best practice.
If those are supposed to be values you're providing as substitution values, then you're missing the values keyword:
insert into certf values (&certificate_id,&certificate_name,&certificate_content);
But you need the string values to be in quotes:
insert into certf values (&certificate_id,'&certificate_name','&certificate_content');
and you should supply the column names too:
insert into certf (certificate_id,certificate_name,certificate_content)
values (&certificate_id,'&certificate_name','&certificate_content');
With you current code the parser is seeing that first list of - possible, but actually invalid in this case - identifiers, i.e column names; because it hasn't seen that values keyword yet. It's treated as something like:
insert into certf (42,some_name,some_content);
And having done that, and when it still doesn't see a values keyword or values list, it's expecting this to be an insert ... select construct instead. You could do it that way:
insert into certf (certificate_id,certificate_name,certificate_content)
select &certificate_id,'&certificate_name','&certificate_content' from dual;
But you aren't doing that. So it doesn't see the select either, and it throws the error you see.

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.

multiple select inserts

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;