despite none NULL value in column preptime2 (value = 7), SQLite is throwing exception:
NOT NULL constraint failed.
The excerpt below is showing
(1) value in menucard_meal.preptime2 (value = 7)
and then
(2) the try to INSERT INTO same data in table orders_meal which throws the error.
sqlite> select preptime2 from menucard_meal where id = 32;
7
sqlite> INSERT INTO orders_meal SELECT * from menucard_meal where id = 32;
Error: NOT NULL constraint failed: orders_meal.preptime2
sqlite>
Has someone any idea what happens here? Thnx
In this statement:
INSERT INTO orders_meal
SELECT * from menucard_meal
where id = 32;
there are 2 tables involved: orders_meal and menucard_meal.
In the above statement you do not list the columns of either table.
This is not required but it is a good practice and usually it saves you from problems like (I suspect) this one.
List the columns for both tables and make sure that the corresponding columns are in the same order, like:
INSERT INTO orders_meal(column1, column2, columnforpreptime2, ...)
SELECT col1, col2, preptime2, ...
from menucard_meal
where id = 32;
Related
If I have an SQL table with all default columns (e.g. identity column + any number of columns all with default values), what is the SQL statement to insert a row with no explicit values given?
insert MyTable /* ( doh, no fields! ) */
-- values( doh, no values! )
What's the trick?
This is a part of the INSERT syntax
INSERT INTO TableName DEFAULT VALUES
Read more here:
https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql
You can use the DEFAULT keyword.
The accepted answer only works for one row, not for multiple rows.
Let us assume you know how many rows to insert, but you want all default values. You cannot do the following, for instance
INSERT MyTable
SELECT DEFAULT VALUES -- Incorrect syntax near the keyword 'DEFAULT'.
FROM SomeQueryOrView;
-- or
INSERT MyTable
DEFAULT VALUES -- Incorrect syntax near the keyword 'FROM'.
FROM SomeQueryOrView;
Instead we can hack MERGE to do this
MERGE INTO myTable
USING (SELECT SomeValue FROM SomeQueryOrView) s
ON 1 = 0 -- never match
WHEN NOT MATCHED THEN
INSERT DEFAULT VALUES;
A bonus benefit is that we can OUTPUT data from columns which are not being inserted:
MERGE INTO myTable
USING (SELECT SomeValue FROM SomeQueryOrView) s
ON 1 = 0 -- never match
WHEN NOT MATCHED THEN
INSERT DEFAULT VALUES
OUTPUT inserted.Id, s.SomeValue;
I have a table reportFilters which has the following column names:
The reportFilterId is auto increment. I want to insert a row in the table with the script below:
IF OBJECT_ID(N'ReportFilters', N'U') IS NOT NULL
BEGIN
IF NOT EXISTS (SELECT * FROM [ReportFilters]
WHERE ReportId IN (SELECT ReportId FROM [Reports] WHERE ReportType = 'Operational Insights Command Staff Dashboard') )
BEGIN
INSERT INTO [ReportFilters] Values(1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );
END
END
GO
But I am getting the following error:
Column name or number of supplied values does not match table definition.
Can I please get help on this ? Thanks in advance.
I think the problem is on inserted columns can't match with inserted data because that will instead by your table column order which is ReportFilterId instead of ReportId
So that there are 11 columns in your table but your statement only provides 10 columns.
I would use explicitly specify for inserted columns (inserted columns start from ReportId except your PK ReportFilterId column)
INSERT INTO [ReportFilters] (ReportId,ReportFilterName,ReportFilterTitle....)
Values (1, 'SelectView', 'Select Views', 13, 'Views','Views', 'SelectView', 'a', 'b', 'c' );
I have below two table for which when i query table TEST_RUA:
select CLASS, ID_LL, ID_UU, TKR from TEST_RUA where ID_UU= 'GV9999B12M0'
it returns:
CLASS ID_LL ID_UU TKR
Bond (null) GV9999B12M0 WIB
When i query table TEST_RUA_MER:
select CLASS, ID_LL, ID_UU, TKR from TEST_RUA_MER where ID_UU= 'GV9999B12M0'
it returns:
CLASS ID_LL ID_UU TKR
Bond (null) GV9999B12M0 WIB
You can see both the values are same for table where ID_UU= 'GV9999B12M0'. The table TEST_RUA_MER has unique index on columns ID_LL, ID_UU, TKR.
Now i have below merge query which throws error as ORA-00001: unique constraint violated and i dont understand how can i avoid this error as both the table values are same then in this case this merge query should try to update and not to insert in table TEST_RUA_MER .
merge into TEST_RUA_MER h using (
select distinct r.CLASS, r.ID_LL, r.ID_UU, r.TKR from TEST_RUA r ) s
on (s.ID_LL=h.ID_LL and s.ID_UU=h.ID_UU and s.TKR=h.TKR) when matched then
update set h.CLASS = s.CLASS, h.ID_LL = s.ID_LL, h.ID_UU = s.ID_UU, h.TKR = s.TKR
when not matched then insert values (s.CLASS, s.ID_LL, s.ID_UU, s.TKR);
Looks like NULL causes problems; it isn't "equal" to anything, so this:
on (s.ID_LL=h.ID_LL
fails.
Try with
on (nvl(s.ID_LL, -1) = nvl(h.ID_LL, -1)
(depending on what ID_LL column's datatype is; I presumed it is a number).
Yes, As mentioned in the other answer also, cause of the error is s.ID_LL=h.ID_LL. You can update it as
( s.ID_LL=h.ID_LL OR (s.ID_LL is null and h.ID_LL is null) )
OR
( s.ID_LL=h.ID_LL OR coalesce(s.ID_LL, h.ID_LL) is null )
I am trying to execute the query:
UPDATE USER SET ATTEMPTS = ATTEMPTS + 1 WHERE USER_ID = "abc"
here ATTEMPTS is Numeric datatype
But I am receiving the error ORA-01722:Invalid Number
There are probably two things going on. user_id is numeric and in your post, you did not copy paste the offending sql statement, but replaced 'abc' with "abc".
The following snippet reproduces your error, and it is caused not by attempts not being numeric, but rather by user_id being numeric and compared to a string:
create table tq84_n (
attempts number,
user_id number
);
insert into tq84_n values (1, 1);
update tq84_n set attempts = attempts + 1 where user_id = 'abc';
drop table tq84_n purge;
I have very simple SQLite table:
CREATE TABLE IF NOT EXISTS `settings` (`Name` TEXT PRIMARY KEY, `Value` TEXT);
I use 2 following queries:
SELECT `Value` FROM `settings` WHERE `Name` LIKE 'MainTabControl.active';
(returns 1 row)
and
SELECT `Value` FROM `settings` WHERE `Name` = 'MainTabControl.active';
(returns 0 rows)
Row with Name column value MainTabControl.active definitely exists (I see it when do SELECT *), doesn't have any spaces at beginning and end, and has the same case of all characters.
What is reason of different behavior of equality operator and LIKE?
You may be running into sqlite's type system. Full details are on http://www.sqlite.org/datatype3.html, but the relevant bit is this:
A TEXT value is less than a BLOB value
My guess is that you (intentionally or not) stored the name column as a BLOB rather than TEXT. This will not be = to the text value, but will be LIKE it.
$ sqlite3
SQLite version 3.7.15 2012-10-15 18:02:57
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table settings (name, value) ;
sqlite> insert into settings values ('MainTabControl.active','textname') ;
sqlite> insert into settings values (cast ('MainTabControl.active' as blob),'blobname') ;
sqlite> select value from settings where name = 'MainTabControl.active' ;
textname
sqlite> select value from settings where name like 'MainTabControl.active' ;
textname
blobname
sqlite> select value, typeof(name) from settings where name like 'MainTabControl.active' ;
textname|text
blobname|blob
sqlite>
The case sensitivity of the LIKE operator is determined
by the PRAGMA case_sensitive_like command.
The default behavior of the LIKE operator is to ignore case for
ASCII characters.
sqlite> SELECT 'A' = 'a';
0
sqlite> SELECT 'A' LIKE 'a';
1
sqlite> PRAGMA case_sensitive_like = TRUE;
sqlite> SELECT 'A' LIKE 'a';
0
If we set it to TRUE, the result is equal to the = operator.
see http://www.sqlite.org/pragma.html#pragma_case_sensitive_like