Based on this topic I've encountered a problem with insertions.
My Tests table contains:
TestID Name
1 test_insert_film
2 test_insert_writer
3 test_insert_location
4 test_delete_film
5 test_delete_writer
6 test_delete_location
I want to insert into my TestTables the id's of the tests with the following sequence:
INSERT INTO TestTables(TestID)
SELECT TestID
FROM Tests
But I get:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'TableID', table 'FilmS.dbo.TestTables'; column does not allow nulls. INSERT fails. The statement has been terminated.
TestTables contains 4 columns, one of them being TestID. Why isn't this working?
The column TableID (!) in your table TestTables is not allowed NULL values! This column is not in the list of columns to be filled upon the INSERT, so the default value assumed is NULL. This is why you get the error.
You may need something like:
INSERT INTO TestTables(TestID, TableID)
SELECT TestID, '' FROM Tests
To fill the TableID column with a default value. Maybe also other columns in the TestTables table are affected and need to be treated similarly.
PS: You could also modify the the TestTables definition to provide a default value for the respective columns. If you do so you can leave the above statement as it is.
Related
i got this error with Oracle SQL with Insert Select query and don't where the error comes from
the SQL Query is:
insert into GroupScenarioAction (ID, ID_UUID, GPSCENARIO_UUID, ACTION, VERSION)
(select DEFAULT , '0', ACTION.ID_UUID, '5310AFAA......', '1', ACTION_ID, '0'
from ACTION where ACTION.id not in (select ACTION FROM GroupScenarioAction where
GPSCENARIO = '1'));
the error is ORA-00936: missing expression Position 129
It is difficult to assist because
you posted relevant data as images (why do you expect us to type all of that so that we could try it?) instead of code (which can easily be copy/pasted and used afterwards)
code you posted (the insert statement itself) uses columns that don't exist in any tables whose description you posted
for example, insert inserts into GroupScenarioAction, but there's no such table there; maybe it is goroohscenarioaction? Or, there's no action_id column in the action table
you're inserting values into 5 columns, but select statement contains 7 columns; that raises ORA-00913: too many values error, you don't even come to the missing expression error
Shortly, as if you tried to do everyhing you could to prevent us from helping you.
One of comments you posted says
It's the primary key so where are those values supposed to come from?
That's the default keyword in
insert into GroupScenarioAction (ID, ...)
(select DEFAULT, ...
-------
this
Looks like the ID column is created as an identity column whose value is autogenerated (i.e. Oracle takes care about it), which also means that you're on Oracle 12c or above (there was no such an option in lower versions). On the other hand create table goroohscenarioaction statement doesn't suggest anything like that.
Anyway: if you do it right, it works. I created two sample tables with a minimum column set, just to make insert work. Also, as I'm on 11gXE (which doesn't support identity columns, I'm inserting a sequence value which is, basically, what identity column uses in the background anyway):
SQL> create table groupscenarioaction
2 (id number,
3 id_uuid raw(255),
4 gpscenario_uuid raw(255),
5 action number,
6 version number
7 );
Table created.
SQL> create table action
2 (id_uuid raw(255),
3 id number
4 );
Table created.
SQL> create sequence seq;
Sequence created.
Insert you posted; I commented out columns that either don't exist or are superfluous. It works; though, didn't insert anything as my table(s) are empty, but it doesn't raise any error:
SQL> insert into GroupScenarioAction
2 (ID, ID_UUID, GPSCENARIO_UUID, ACTION, VERSION)
3 (select 1 /*DEFAULT*/ , '0', ACTION.ID_UUID, '5310AFAA......', '1' --, id /*ACTION_ID*/, '0'
4 from ACTION
5 where ACTION.id not in (select ACTION FROM GroupScenarioAction
6 where gpscenario_uuid/*GPSCENARIO*/ = '1'));
0 rows created.
Beautified:
SQL> insert into groupscenarioaction
2 (id, id_uuid, gpscenario_uuid, action, version)
3 (select seq.nextval, '0', a.id_uuid, '5310AFAA......', '1'
4 from action a
5 where a.id not in (select g.action
6 from groupscenarioaction g
7 where g.gpscenario_uuid = '1'));
0 rows created.
SQL>
Now that you know a little bit more about what's bothering use to help you, and if what I wrote isn't enough, consider editing the original question you posted (simply remove everything that's wrong and write something that is true and we can use).
I have a function that INSERT INTO a table some data. I do not know how much columns the table has (let it be N).
My func gets a list of N-1 params and then
INSERT INTO ... VALUES( *N-1 params* )
The last N-th column is a standart ID column, which i want to set DEFAULT (ID column has the default value = "max" and is auto incremented)
The main problem: due to the fact that i do not know names of the columns i am working with, i can't manually enter data into the database.
How to insert a row of data, if I don't know the number of columns, but I know that I have 1 less than it should be and the last column is auto incremented / has the default value?
If you know that the order of the N-1 params is exactly the same as the order of the columns in the definition of the table (the CREATE stamenet), then there is no need to enumerate the names of the columns in the INSERT statement.
Add, 1 more NULL value to the list for the ID column (at the end of the list if, as you say it is defined last):
INSERT INTO tablename VALUES(param1, param2, ..., paramN-1, NULL)
By passing NULL for the ID, it will be incremented since it is defined as AUTOINCREMENT.
See a simplified demo.
I want to insert data on table dbo.batch but but I have an error message when i tried to insert the data
error message
Msg 110, Level 15, State 1, Line 3
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
You have 10 columns in your statement but 11 values to enter. Therefore the error message
You have 10 columns and you are inserting 11 values check for the valid columns values you need and remove other.
I guess the last 2nd column value is the one you should remove
I am trying to make two inserts one after another like:
INSERT INTO tbl_tours (TimeFrom)
OUTPUT inserted.tourId, DispatchingId, TimeFrom, TimeTo INTO tbl_tourData (tour_fk, dispatchId, timeFrom, timeTo)
SELECT TimeFrom
FROM #tmpTable
SELECT * FROM tbl_tours
SELECT * FROM tbl_tourData
But I get an error:
Msg 207 Level 16 State 1 Line 13
Invalid column name 'DispatchingId'.
Msg 207 Level 16 State 1 Line 13
Invalid column name 'TimeFrom'.
Msg 207 Level 16 State 1 Line 13
Invalid column name 'TimeTo'.
You can check full code at this fiddle:
https://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=c10f9886bcfb709503007f18b24eabfd
How to combine these inserts?
The output clause can only refer to columns that are inserted. So this works:
INSERT INTO tbl_tours (TimeFrom)
output inserted.tourId, inserted.TimeFrom into tbl_tourData(tour_fk, timeFrom)
SELECT TimeFrom FROM #tmpTable;
Here is the revised db<>fiddle.
If you want additional information, you need to join back to another source.
When you do an insert ... output, the "output" part can only output whatever was inserted by the "insert" part. You can't reference data from the "inserting" table.
You do insert into tbl_tours(TimeFrom). So you're only inserting a single column - the TimeFrom column, and the tour_id column will be automatically inserted, so that's available too. But then you try to use 4 columns in the output list. Where would these extra two columns come from?
One way to do this in a single step is to use the merge statement, which can get data from the "inserting" source, not just the "inserted" table. Since you know you always want to do an insert, you can join on 1 = 0:
merge tbl_tours
using #tmpTable tmp on 1 = 0
when not matched then
insert (TimeFrom)
values (tmp.TimeFrom)
output inserted.tourId,
tmp.dispatchingId,
inserted.timeFrom, -- or tmp.timeFrom, doesn't matter which
tmp.TimeTo
into tbl_tourData (tour_fk, dispatchId, timeFrom, timeTo);
I should add: This is only possible because you don't actually have a foreign key defined from tbl_tourData to tbl_Tours. You probably do intend to have one given your column name. An output clause can't output into a table with a foreign key (or a primary key with a foreign key to it), so this approach won't work at all if you ever decide to actually create that foreign key. You'll have to do it in two steps. Either per Gordon's answer (insert and join), or by creating a whole new temp table matching the schema of tbl_tourData, outputting everything into that using merge, and then dumping the second temp table into the real tbl_tourData.
I wanna write insert command in sql 2005.
I have 10 Columns, some of them can be null.
I use this command:
Insert Into TableName Values(x,y)
since the others can be null, I don't bring them in command.
cause, number of null-able columns are different, I can't bring exact null values.
but I've got this error:Column name or number of supplied values does not match table definition.
what can I do?
1 - Accept some of the past answers to your questions.
2 - Supply which fields you are inserting. In a 5 column table, you can say
INSERT INTO Table (col2, col4)
VALUES (col2value, col4value)