Oracle 9.2 migrated to 11g - Default values - oracle11g

We recently migrated from Oracle version 9.2 to 11g.
But some bugs appeared with the application using the database. We get the following exception:
ORA-01400: cannot insert null into...
I looked for information about the error and especially about some new features in version 11g that might be the cause.
And here I'm. I know that the old way of defining table columns with default values could not work, but I don't why, and more important how to resolve the issue.
Here's the definition of the table I can see using SQL Developer:
COLUMN_NAME DATA_TYPE NULLABLE DATA_DEFAULT COLUMN_ID
--------------------------------------------------------------------------
...
REP_DOC_RECEIVED CHAR(1 BYTE) No 'n' 12
...
When I try to insert a row with a null value for that column, the exception is thrown.

A default value only applies, when no value is provided in the insert statement. So I guess your insert makes some insert into ... values ( ... , NULL, ...).
See for example http://sqlfiddle.com/#!4/cd58c/7 to show the difference:
-- ok
insert into a (n,REP_DOC_RECEIVED) values (1,'A');
-- ok, default is applied
insert into a (n) values (1);
-- not ok, NOT NULL constraint violated
insert into a (n,REP_DOC_RECEIVED) values (1,'');
You have to do either NVL(...,'n') in your insert statement or to leave out the column. And no, this didn't change in 11g.

Related

myBatis "SELECT" fails with uppercase tablename on Linux

I have created a table:
CREATE TABLE IF NOT EXISTS `tablename` (
..
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then I run a query via myBatis (as defined in the mapper.xml-file):
SELECT .. FROM `tablename`;
This query fails with
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'databasename.TABLENAME' doesn't exist
How can I force myBatis to query with the specified lowercase name "tablename" instead of doing an uppercase-translation?
I found this: https://mybatis.org/generator/configreference/table.html where it is said, that myBatis gets the info on how to deal with case sensitivity from the database itself.
So I checked with
SHOW VARIABLES;
the settings and got:
lower_case_file_system OFF
lower_case_table_names 0
This means if I understand https://mariadb.com/kb/en/identifier-case-sensitivity/ right, that the database indicates that the tablenames are case sensitive. Why does myBatis then force to given lowercase names to uppercase before issuing the query?
Thanks in advance.
Sorry, completely my fault. Had changed my code and committed - but not pushed. So my changes in the mapping.xml did not become active in deployment.
The query, the way I had described above works.

override default script for initialization of flyway metadata table

I'm working with the Microsoft Parallel Data Warehouse appliance and attempting to use flyway to handle table migrations in that environment. The issue I'm running into is that the default script for establishing the schema_version table fails.
Here is the default script as far as I can tell that is being executed upon calling baseline().
CREATE TABLE [dbo].[dbresult_migration] (
[installed_rank] INT NOT NULL,
[version] NVARCHAR(50),
[description] NVARCHAR(200),
[type] NVARCHAR(20) NOT NULL,
[script] NVARCHAR(1000) NOT NULL,
[checksum] INT,
[installed_by] NVARCHAR(100) NOT NULL,
[installed_on] DATETIME NOT NULL DEFAULT GETDATE(),
[execution_time] INT NOT NULL,
[success] BIT NOT NULL
);
ALTER TABLE [dbo].[dbresult_migration] ADD CONSTRAINT [dbresult_migration_pk] PRIMARY KEY ([installed_rank]);
CREATE INDEX [dbresult_migration_s_idx] ON [dbo].[dbresult_migration] ([success]);
Specifically the Microsoft Parallel Data Warehouse (MS PDW or APS as it is now known) doesn't support expressions with default constraints.
Msg 104338, Level 16, State 1, Line 1
An expression cannot be used with a default constraint. Specify only constants for a default constraint.
Which causes an error when GETDATE() is used as the default for the installed_on column.
The ALTER TABLE statement will also fail as PRIMARY KEYs and INDICES are managed differently in the environment.
Is there a way to override the default initialization script for the schema_version?
UPDATE
Further investigation reveals that the next failure occurs when attempting to insert records into the schema_version table. Specifically the current implementation attempts to identify the current user based on a call to dbSupport.getCurrentUserFunction(). For SQL Server this function is SUSER_SNAME(). While this function is available on both the standard SQL Server and the Parallel Data Warehouse the current implementation of the Parallel Data Warehouse does not allow for function calls within the values portion of an insert statement. As such, the following error is returned:
Insert values statement can contain only constant literal values or variable references.
When the query that is attempted is logged as:
INSERT INTO [dbo].[dbresult_migration] ([installed_rank],[version],[description],[type],[script],[checksum],[installed_by],[execution_time],[success]) VALUES (#P0, #P1, #P2, #P3, #P4, #P5, SUSER_SNAME(), #P6, #P7)
UPDATE 2
I now have a fork of flyway-core that correctly identifies if you are connecting to SQL Server vs SQL Server parallel data warehouse. Another issue that I have identified is that SQL Server PDW does not allow DDL within transactions and so an attempt to baseline fails as this appears to be attempted from within a transaction template. Ultimately this is evolving from a question of understanding how to modify an initialization script to a need for support of a new database platform. I've submitted this as a new issue on the flyway repo on github here.

SQLite all columns specified with DEFAULT in DDL cause error. Can't insert any data now

It's been a while since I used SQL, and I am relearning a lot. However, I cannot figure out why my DEFAULT values are causing errors in my SQL with SQLite. I have created 4 tables, and most of them use DEFAULT values of some sort.
Before, I was able to create test data in my database (with these default values). Now, for some reason, these cause errors, and I cannot figure out why.
DDL
Here is one of my tables. Look at the last 4 columns in the SQL (i.e. is_usdr_created, is_continuous_play, is_random_play, repeat_all_play)
CREATE TABLE IF NOT EXISTS playlists (
playlist_id INT NOT NULL,
category_id INT NOT NULL,
name TEXT NOT NULL CHECK (trim(name) != ''),
video_count INT CHECK (video_count > 0),
is_user_created BOOLEAN DEFAULT (0),
is_continuous_play BOOLEAN DEFAULT (1),
is_random_play BOOLEAN DEFAULT (0),
repeat_all_play BOOLEAN DEFAULT (0),
PRIMARY KEY (playlist_id),
FOREIGN KEY (category_id) REFERENCES categories (category_id)
);
Error
[1] [SQLITE_ERROR] SQL error or missing database (near "DEFAULT": syntax error)
If I remove all the DEFAULT keywords and values, then everything works fine. Why is that?
NOTE: I just edited this once I realized this problem is now more widespread to all my tables with default values.
I also decided to create a new project, just in case this was an issue with the previous project; however, this issue is still occuring with the new project.

convert column data to xml format; query fails because all columns types are currently not supported

I am trying to convert column data to xml format, but I get this error message:
The query fails because all columns types are currently not supported.
CREATE TABLE EMP(NAME VARCHAR2(10 BYTE))
INSERT INTO EMP VALUES ('C');
INSERT INTO EMP VALUES ('A');
INSERT INTO EMP VALUES ('T');
SELECT xmlelement("NAME",NAME) FROM EMP;
I am using:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
SQLTools 1.5.0 Beta build 9 as EDITOR
Why is this error arising??? What is the solution for this?
I've found the answer:
select dbms_xmlquery.getxml('select * from EMP') from dual;
This is more of a workaround and not a solution.
I was having the same problems as sam - also running a SELECT xmlelement statement, also using SQLTools. One difference is that I was running Oracle DB version 11.2.0.2.0.
I found that if I ran the statement in SQLPlus, it was able to display the result.
SQL> SELECT XMLELEMENT("name",ename) FROM scott.emp WHERE ROWNUM < 3;
XMLELEMENT("NAME",ENAME)
--------------------------------------------------------------------------------
<name>SMITH</name>
<name>ALLEN</name>
If I ran the statement in SQL Developer, it tried to display the results, but only showed (XMLTYPE).

Copied database from live server doesn't allow me to perform inserts

After I copied the SQL Server database from the server and changed the connection string, the database appears to be read-only, but I try inserting a new node it gives me this error:
Cannot insert the value NULL into column 'ID', table
'Theshuk.dbo.Banners'; column does not allow nulls. INSERT fails. The
statement has been terminated.
This does not occur when I use the live server's database.
Any ideas?
Thanks
i have seen some weird things like that in the identity not sticking around in a database migration. not sure the cause, but it sounds like you are probably just missing an identity that was there in the other location. check all your tables to see that you have identities specified where you expect them.
The error just means: you cannot insert the values that you're trying to insert. As the error clearly states:
Cannot insert the value NULL into column 'ID', table
'Theshuk.dbo.Banners'; column does not allow nulls.
So it seems you're trynig to insert something into that table, and the table requires a value for ID and you're not supplying one.
That's all there is - add that column and a value for it to your INSERT statement and you should be fine.

Resources