I am trying to run the following query to insert a number of nodes with an id that auto-increments as nodes are loaded into the table.
However I get the error, ORA-02287: sequence number not allowed here whenever I run it.
INSERT INTO V1144Engine.T_NODES VALUES
(
(SELECT V1144ENGINE.S_PK_NODES.NEXTVAL FROM dual),
1,
'Chemistry of Life',
0,1,
SYSDATE,
NULL,
'CON.3.1',
NULL
);
I have tried running
SELECT V1144ENGINE.S_PK_NODES.NEXTVAL from dual
This works fine and returns the number that I want.
How do I get around this? I am running on Oracle 11g.
Also it would be much appreciated if the query were still runnable on one line as I am making these in a spreadsheet and would like to still be able to do so.
There is no need to have the inner SELECT. Simply
INSERT INTO V1144Engine.T_NODES
VALUES(V1144ENGINE.S_PK_NODES.NEXTVAL,
1,
'Chemistry of Life',
0,
1,
SYSDATE,
null,
'CON.3.1',
null);
In general, though, you want to list the columns that you are providing values for in your INSERT statement. That not only documents the columns so that a future developer doesn't have to look up the order of columns in a table, it protects you if new columns are added to the table in the future.
INSERT INTO V1144Engine.T_NODES( <<list of columns>> )
VALUES(V1144ENGINE.S_PK_NODES.NEXTVAL,
1,
'Chemistry of Life',
0,
1,
SYSDATE,
null,
'CON.3.1',
null);
Related
Please help me. I want to insert record in my database that prevents duplicate entries while inserting a record. Here's my code:
INSERT INTO Items_Tbl(Room_ID, Date_Purchased,Pc_Number)
Values('1', '2019-01-01', '1')
WHERE NOT EXISTS(Select PC_Number FROM Items_Tbl WHERE Room_ID = 1)
I don't want PC_Number to accept same entries.
One way to do it is as such:
IF NOT EXISTS(Select PC_Number FROM Items_Tbl WHERE Room_ID = 1)
INSERT INTO Items_Tbl(Room_ID, Date_Purchased,Pc_Number)
Values('1', '2019-01-01', '1')
However, since you are using SQL Server, it's much better to create a unique, non-clustered index on the target table for the field PC_Number. You can also create a query/stored procedure separately to check if the PC_Number exists, if you need to reuse it elsewhere.
USE DATABASE
insert into #Table -- Previously created temp table
(
Name
Number
Date
)
select 'Joe', 5, 'January 9th'
union all select 'Sam', 3, 'January 4th'
union all select 'Eleanor', 4, 'January 5th'
union all select 'Joseph', 1, 'January 6th'
My question is what is the scope of the USE statement when insert into select is not specifying a 'from' statement that clearly denotes what table in the database the information is coming from?
I've encountered an insert into select statement similar to this one and what I am struggling to understand is if the data is being created in the four select statements or if it is being searched for and found in the DATABASE. If it is being searched for how does Microsoft SQL Server Management studio know what table is being referred to?
Everything following USE database will be run in the context of the database chosen.
In this case, the USE accomplishes nothing, because your #temp table is stored in tempdb regardless of which database you're using, and your select is not accessing any tables, so it will return the hardcoded values regardless of database context as well.
If there is no FROM clause, then the data isn't coming from any table in any database. It is hard-coded into the INSERT query. If you're asking how does it know what table to insert the data into, that is specified in the INSERT INTO clause, and is in the context of the USE statement.
I'm suspicious of your question, because you insert into a temp table, which you comment as "previously created" and the scope of temp tables is an entirely different matter.
I've created sequence by following statement:
CREATE SEQUENCE MAIN.MY_SEQUENCE START WITH 1 INCREMENT BY 1 CACHE 50;
And table by following statement:
CREATE TABLE MAIN.EMPLOYEES(
ID INTEGER NOT NULL,
NAME VARCHAR(512),
EMAIL VARCHAR(254),
PRIMARY KEY (ID)
)
Now when I try to insert a new record by using following statement:
INSERT INTO MAIN EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1#example.com') UNION ALL
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2#example.com')
I get an error:
"NEXTVAL FOR MAIN.MY_SEQUENCE.NEXTVAL" cannot be specified in this context.. SQLCODE=-348, SQLSTATE=428F9, DRIVER=4.17.30
When I try to insert a single row everything works fine.
I have found a list of restrictions on using NEXT VALUE here but here not mentioned my case or I couldn't find it.
My question is it possible to insert multiple rows by using ID from sequence, and if yes, how can I achieve it?
It does list your case. The documentation contains this:
The NEXT VALUE expressions cannot be specified in the following contexts:
...
•SELECT statement for which the outer SELECT is combined with another SELECT statement using a set operator such as UNION, EXCEPT, or INTERSECT
....
(emphasis mine) This statement isn't exhaustive, and because UNION ALL is considered a set operation, the operation is excluded.
This should be fixable - I'm a little surprised you wrote the statement the way you did; DB2 allows you to comma-separate data rows. That is, the following should be valid:
INSERT INTO MAIN.EMPLOYEES (ID, NAME, EMAIL)
VALUES (MAIN.MY_SEQUENCE.NEXTVAL, 'Name 1', 'email1#example.com'),
(MAIN.MY_SEQUENCE.NEXTVAL, 'Name 2', 'email2#example.com')
I have 3 SQL-Statements that I would like to combine into just one so I dont have to make multiple requests to my database from my programm (java).
My DB is PostgreSQL 9.4
First one creates a new user in umgmt_users
INSERT INTO umgmt_users ("user") VALUES ('test1')
Second one gets the id of that user (db is postgres and id data type is serial, so it get assigned automatically with me/the programm not knowing what id the user will get
SELECT umgmt_users.id
FROM umgmt_users
WHERE umgmt_users.user = 'test1'
Thrird is to add the just created user with his id (which I need the second statement for) and some other values into a different table
INSERT INTO
umgmt_user_oe_fac_role ("user_id", "oe_id", "fac_id", "role_id")
VALUES ('ID OF USER test1 created in first statement', '1', '2', '1');
Is there a way to get all three Statements into one?
create user
look up the ID he got assigned
insert his ID + other values into a different table
I'm not that good at SQL, I tried to put brackets around the select and put it into the insert & also looked at UNION and WITH but can not get it to work...
EDIT: Ended up using this solution from a_horse_with_no_name
with new_user as (
INSERT INTO umgmt_users ("user") VALUES ('test1')
returning id
)
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
SELECT id, 1, 2, 1
FROM new_user;
All you need is two inserts:
INSERT INTO umgmt_users ("user") VALUES ('test1');
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
VALUES (lastval(), 1, 2, 1);
In order for lastval() to work correctly there must be no other statement between the two inserts and the have to be run in a single transaction (so autocommit needs to be turned off)
Alternatively you can use a data modifying CTE which is then executed as a single statement:
with new_user as (
INSERT INTO umgmt_users ("user") VALUES ('test1')
returning id
)
INSERT INTO umgmt_user_oe_fac_role (user_id, oe_id, fac_id, role_id)
SELECT id, 1, 2, 1
FROM new_user;
Please don't put numbers in single quotes.
The answer to this is : It's impossible to combine these into a single plain vanilla ANSI SQL statement.
The first and third ones talk about two different tables altogether.
The second one is a Select Statement which is a different type of statement from the other two.
I created a table in Oracle SQL :
create table t1
(
empno number(6) PRIMARY KEY,
empname varchar(30),
hiredate date,
basic number(8),
deptno number(4)
);
And now I am inserting values into the table using a single query:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235)
(131310,'HT','20-APR-14',120020,1234));
But this shows error:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235),
*
ERROR at line 1:
ORA-00907: missing right parenthesis
How do I correct this?
An INSERT VALUES statement always inserts exactly 1 row. If you want to insert multiple rows with hard-coded values, the most common approach would simply be to execute two separate INSERT statements.
insert into t1 values(131309,'HP','20-FEB-04',2000000,1235);
insert into t1 values(131310,'HT','20-APR-14',120020,1234);
If you really wanted to, you could select your hard-coded values from dual and then do an INSERT SELECT
insert into t1
select 131309, 'HP', '20-FEB-04',2000000,1235 from dual
union all
select 131310,'HT','20-APR-14',120020,1234 from dual
Or you could do an INSERT ALL
insert all
into t1 values(131309,'HP','20-FEB-04',2000000,1235)
into t1 values(131310,'HT','20-APR-14',120020,1234)
select * from dual
Personally, I'd just use two statements.
Although this isn't related to your question, a couple of comments
Always, always list out the columns in your insert statement. You'll make your SQL much more robust so that if you add new columns in the future that allow NULL values your statements will still work. And you'll avoid lots of bugs when the column list is right there rather than hoping that someone remembers the order of columns in the table.
If you're inserting a value into a date column, use a date not a string literal that represents a date. Relying on implicit data type conversion is a source of many bugs. Use an explicit to_date or use ANSI date literals. And use 4-digit years.