How to create and Oracle trigger to return some DB Error code, not the Custom Exception but real DB ERROR? - sql

I would like to know is it possible to return a DB ERROR code from trigger, rather than Custom exception created with RAISE_APPLICATION_ERROR. I have some requirement where i need to return an DB ERROR code like 1 for Unique Constraint. I have a purpose to ask this, because for some reason with some hash based partition we are not creating unique constraints on table and rather have one trigher with which i want to return Unique constraint as an error code after checking if the record exists in DB.

You can use:
RAISE DUP_VAL_ON_INDEX;
Only in this case you can't pass any arguments to this error and you will get:
ORA-00001: unique constraint (.) violated

Related

Oracle Constraint Letters Only SQL REGEXP

I already have my tables full of data, I am trying to add a constraint to only allow letters to be entered into the attribute I have named as studentfname. I have tried various different REGEXP but cannot seem to get any to work without getting an error when inserting them.
Im not sure if its a syntax error or what!
Edit: Here is what I have tried and the error I am getting:
ALTER TABLE STUDENT
ADD CONSTRAINT
check_name
CHECK(regexp_like(studentfname,'^[A-Za-z''-]+$'));
Error:
QL Error: ORA-02293: cannot validate (JEIGH7.CHECK_NAME) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious
Apparently you can try to run this below query to check if any non integral data resides in the above mentioned column which is preventing you from applying a check constraints. Once you get the output from this query try UPDATING/DELETING the row and then apply the constraint. Hope this helps.
SELECT studentfname nm FROM STUDENT a
WHERE NOT regexp_like(a.nm,'^[A-Za-z''-]+$');

Synchronization Bulk Insertion Exception

Any one please help me out. I am syncing sql server to client using MS Sync Framework. I am getting this exception at syncOrchestrator.Synchronize();
Failed to execute the command 'BulkInsertCommand' for table ‘xyz'; the transaction was rolled back. Ensure that the command syntax is correct.
Inner Exception is:
Violation of PRIMARY KEY constraint 'PK__#B1CF471__ECD9228532A93525'. Cannot insert duplicate key in object 'dbo.#changeTable'. The duplicate key value is (0). The data for table-valued parameter \"#changeTable\" doesn't conform to the table type of the parameter. The statement has been terminated.
There is a store procedure that using above object 'dbo.#changeTable', but I don't know what to do with that??

How to delete a mysql-enum-like column in sql server?

in orther to get a column similar to the mysql ENUM type, I wrote a sql query as follows
ALTER TABLE [DbName].[dbo].[MediaContent]
ADD MediaType nvarchar(50)
check(MediaType in ('audio','video','song','other'))
this worked as wished(for test): But now I want to delete this column without success. It seems like there no way to directly delete a column which has a constraint up on it.
How can I solve this issue? I want to delete this column and create another one.
here is the error message I get while the deletion
The object 'CK__MediaCont__Media__14270015' is dependent on column 'MediaType'.
ALTER TABLE DROP COLUMN MediaType failed
because one or more objects access this
column. (Microsoft SQL Server, Error: 5074)
The object referenced in the error message is the name of the constraint. You should be able to use the follow:
ALTER TABLE [DbName].[dbo].[MediaContent]
DROP CONSTRAINT CK__MediaCont__Media__14270015
You need to first drop the check constraint mentioned in the error message since that's stopping you from dropping the column. Following that you may drop the column.
Drop the constrain first then drop the column ,it will work

Insert statement attempted to include a duplicate key

Trying to run
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8');
in Oracle SQL Developer. Ive had it running but when I close it, then reopen it I get this error:
Error starting at line 1 in command:
INSERT INTO BOOKING_EXTRAS (BOOKING_ID, EXTRAS_, EXTRAS_PRICE) VALUES ('1','Phone call: 1.80','1.8')
Error report:
SQL Error: ORA-00001: unique constraint (COURSEWORK_XE.BOOKING_EXTRAS_PK) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
how would I fix this? Its happening to every table I run!
You need to either clear the tables or insert new information, a database doesn't want duplicate rows because that makes it impossible to find the correct rows later.
In addition, if that BOOKING_ID (currently serves as primary key if I guess right) doesn't actually mean something for you, you can set it as AUTO INCREMENT in your schema, then afterwards you don't need to insert a value for BOOKING_ID, the system will automatically find a value which is not duplicate for you. This might save you a lot effort.
INSERT INTO BOOKING_EXTRAS (EXTRAS_, EXTRAS_PRICE) VALUES ('Phone call: 1.80','1.8');
If your unique column is being populated by a sequence, check to make sure that your sequence has the same 'last value' as your highest-value unique column.
I just encountered a problem where the sequence had a 'last value in' that was much lower than the highest value in my unique column. The DBA ran a script to update the sequence to what the number should have been, and my error went away.
Now to find out why the sequence was so wrong...

Avoid duplicate rows in phonegap database

I have a database with a single column and create it like this
function populateDB(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS RESULT(result, UNIQUE(result))');
}
however, when I want to write objects into the table, I get the error:
could not execute statement (19 constraint failed)
The error only appears when I add the NOT NULL and PRIMARY KEY to the set-Up. Without it, it keeps writing duplicates in my database (I fill the database with data from a webservice on startup of a phonegap iOS application, so I want to catch duplicate rows on database level)
At first I had only "PRIMARY KEY" in it, and because the error says something about constraints, I was assuming the primary key has to be "NOT NULL", adding it to the code, I still got the same error.
how can I solve this. thanks in advance
edit: Maybe it´s important for you to know that I write stringyfied JSON Objects into the results column.
edit: I insert the object in this function. Basically I do it one by one:
this.save = function(){
db.transaction (function(transaction){
object = $.toJSON(self);
object = encodeURI(object);
transaction.executeSql('INSERT INTO RESULT (result) VALUES ("'+object+'")');
},
errorCB,
successCB
);
}
edit: I updated my set up table code. The error doesnt occur on every row, but the objects are actually unique.
"IF NOT EXISTS" is the catch here. You get constraint failed when you use conditions like "IF NOT EXISTS" or "REPLACE", "IGNORE" etc in SQLite queries.
Suppose, you're trying to create a table that already exists, then the constraint (here not creating the table again if it already exist) fails and hence the query fails (which you actually want - you dont want to duplicate the tables right?) and gives you the error constraint failed.
You may get a similar error on INSERT or REPLACE, INSERT or IGNORE, etc...