Syntax Error or Access Violation comes up when logging into the program - sql

our program connects to a db and when you sign into the program, it's throwing this error: "Syntax error or access violation" along with "Cannot insert the value NULL into column 'ID', table 'CADDB.dbo.AuditTrail'; column does not allow nulls. INSERT fails"
I've tried Microsoft's Fix to Use ANSI Nulls, Padding and Warnings in the ODBC connections, but it still occurs.
Can someone point me where to look? I believe it's corrupt data in the db... but not 100% sure. I will attach the error that gets logged with that message if you need me to.

The exception showed you the root cause that your app is trying to insert NULL value into column ID, a column not allowing NULL. If this is caused by your application's bug and you also want to insert a NULL value into ID column. Then you may change the table to allow NULL for column ID or use a special value representing NULL (not recommended) for the insertion.
Otherwise, it is more likely to be your application's issue.

You need to make sure the column from CADDB.dbo.AuditTrail allow nulls,check your table schema for that, or you could try insert into the column ISNULL(value,'NULL'), which is the string null not actual NULL.
ANSI_NULL query option only be took into effect when doing the value comparison. which is IS NULL/IS NOT NULL when it is set to ON or using =/!=/<> when set to OFF.

Use the ISNULL() function. Example ISNULL(column Name, '')
I'm not sure if you're using T-SQL script in your code to access the table, or you can alter the column to allow nulls.

Thank you everybody! The issue was the db was corrupt. I got a working backup of the same db and it works fine without any errors.

Related

HSQLDB user lacks privilege or object not found error when making select statements with where

I use SQuirrel SQL Client Version 3.5.3 and HSQLDB for my database. I have been able to specify the corresponding driver (In-memory) to it and create an Alias.
I have created a table
CREATE TABLE ENTRY(
NAME VARCHAR(100) NOT NULL,
DESC VARCHAR(500) NOT NULL,
PRIMARY KEY (NAME))
and added a few lines of data into it. While statements like these work:
select * from ENTRY
select NAME from ENTRY
select DESC from ENTRY
I always get Error: user lacks privilege or object not found"
when adding a where clause to my statement, e.g. select DESC from ENTRY where NAME=CAR
Any help is greatly appreciated as I can slowly feel my sanity waning
I had the same problem, but my table name and other things were ok except my query for VARCHAR were inside double quotes("") but it should be in single quotes('')
example:
assume you have table like this which flightId is primary key
now this query is wrong:
SELECT * FROM flights WHERE flightId="0f3ae9b3-6bb1-4c95-9394-6179555f5879"
while this one is ok:
SELECT * FROM flights WHERE flightId='0f3ae9b3-6bb1-4c95-9394-6179555f5879'
I was finally able to fix this myself. I had used a wrong table name for my select statements and after changing it to the real one it worked. The only thing that confuses me is that I also used the wrong table name for my insert statements but they were executed successfully and all data is showing up in them.
HSQLDB has default schema called PUBLIC. All SQL queries will be pointing to PUBLIC; If you have created your own schema like eg:OWNSCHEMA then edit the xxx.script and change the following line
SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
to
SET DATABASE DEFAULT INITIAL SCHEMA OWNSCHEMA
When I received the same exception the root cause was that I had a table in the SELECT clause that was not present in the FROM clause.
Your problem is:
I always get Error: user lacks privilege or object not found" when
adding a where clause to my statement, e.g. select DESC from ENTRY
where NAME=CAR
Yes, of course you do.
NAME is a field of the ENTRY table. CAR isn't a field of anything.
Perhaps your WHERE clause should look like this instead:
WHERE NAME='CAR'
Thereby comparing a field value with a literal string value instead of trying to compare it with a nonexistent other field value.

SQL insert row data, trough nulls are not allowed

I created a simple database in sql 2012 where I didnt allow to store null data, I checked out my program and I see that it still save empty row :( Is there option that can help me prevent save null in db (and why turning off "allow null" didnt stop it?) ?
You want a CHECK Constraint. This will throw an error when a statement attempts to put data in a field that doesn't match the criteria.
ALTER TABLE UserInfo ADD CONSTRAINT CK_UserInfo_Login CHECK (Login <> '')

sql server, composite keys - ignoring duplicate

Is there a way to prevent sql from throwing an error when I try to save a record that already exists. I've got a composite key table for a many-to-many relationship that has only the two values, when I update a model from my application, it tries to save all records, the records that already exist throw an error Cannot insert duplicate key is there a way of having the database ignore these, or do I have to handle it in the application?
you are calling an INSERT and trying to add duplicated keys. This error is by design, and essential. The DB is throwing an exception for an exceptional and erroneous condition.
If you are, instead, trying to perform an "upsert" you may need to use a stored procedure or use the MERGE syntax.
If, instead, you don't want to UPDATE but to just ignore rows already in the table, then you need to simply add an exception to your INSERT statement... such as
....
WHERE
table.Key <> interting.key
Try something like this with your insert statement.
insert into foo (x,y)
select #x,#y
except
select x,y from foo
This will add a record to foo, ONLY if it is not already in the table.
You could try creating your index with the IGNORE_DUP_KEY option so that you only get a warning when you have duplicate keys rather than a true error.
The other option and possibly the better one is to use the MERGE statement rather than insert. The MERGE statement let's you do Inserts, Updates and Deletes all in one statement and sounds like it should work out well for what you are trying to do.
Last but not least, as you said fix it in your app and only insert the rows that need to be added.

What kind of errors exists in SQL querys for ROLLBACK?

For example:
insert into table( a, b ) values ('a','b') could generate the following error:
**a-b duplicate entry**
BUT here I can ignore this error selecting the ID of this values, then use this ID:
select ID from table where a = 'a' and b = 'b'
insert into brother( table ) values (ID)
Finally I could COMMIT the PROCEDURE. Look that this error isn't relevant for rollback if I need the ID.
The question is: what kind of errors will doing me to ROLLBACK the PROCEDURE???
I hope you understand.
I think you're asking, "What kind of errors can an INSERT statement cause that will make MySQL rollback a transaction?"
An INSERT that violates any constraint will cause a rollback. It could be foreign key constraint like you've outlined, but it could also be a UNIQUE constraint, or a CHECK constraint. (A CHECK constraint would probably be implemented as a trigger in MySQL.)
Trying to insert values that aren't valid (NULL in nonnullable columns, numbers that are out of range, invalid dates) might cause a rollback. But they might not, depending on the server configuration. (See link below.)
An INSERT can also fail due because it lacks permissions. That will also cause a rollback.
Some conditions that would cause a rollback on other platforms don't cause a rollback on MySQL.
The options MySQL has when an error
occurs are to stop the statement in
the middle or to recover as well as
possible from the problem and
continue. By default, the server
follows the latter course. This means,
for example, that the server may
coerce illegal values to the closest
legal values.
That quote is from How MySQL Deals with Constraints.
One of my favorite quotes from the MySQL documentation, 1.8.6.2. Constraints on Invalid Data.
MySQL enables you to store certain
incorrect date values into DATE and
DATETIME columns (such as '2000-02-31'
or '2000-02-00'). The idea is that it
is not the job of the SQL server to
validate dates.
Isn't that cute?

Force SQL Server column to a specific value

Is it possible to force a column in a SQL Server 2005 table to a certain value regardless of the value used in an insert or update statement is? Basically, there is a bug in an application that I don't have access to that is trying to insert a date of 1/1/0001 into a datetime column. This is producing a SqlDateTime overflow exception. Since this column isn't even used for anything, I'd like to somehow update the constraints on the columns or something in the database to avoid the error. This is obviously just a temporary emergency patch to avoid the problem... Ideas welcome...
How is the value being inserted? If it's through a stored proc... you could just modify the Sproc to ignore that input parameter.
if it's through client-side generated SQL, or an ORM tool, otoh, then afaik, the only option is a "Before" Trigger that "replaces" the value with an acceptable one...
If you're using SQL 2005 you can create an INSTEAD OF trigger.
The code in this trigger wil run in stead of the original insert/update
-Edoode
I'd create a trigger to check and change the value
If it is a third party application then I will assume you don't have access to the Stored Procedure, or logic used to generate and insert that value (it is still worth checking the SPs for the application's database though, to see if you can modify them).
As Charles suggested, if you don't have access to the source, then you need to have a trigger on the insert.
The Microsoft article here will give you some in depth information on creating triggers.
However, SQL Server doesn't have a true 'before insert' trigger (to my knowledge), so you need to try INSTEAD OF. Have a look here for more information. In that article, pay particular note of section 37.7, and the following example (again from that article):
CREATE TRIGGER T_InsertInventory ON CurrentInventory
INSTEAD OF INSERT AS
BEGIN
INSERT INTO Inventory (PartNumber, Description, QtyOnOrder, QtyInStock)
SELECT PartNumber, Description, QtyOnOrder, QtyInStock
FROM inserted
END
Nick.
the simplest hack would be to make it a varchar, and let it insert that as a string into the column.
The more complicated answer is, you can massage the data with a trigger, but it would still have to be valid in the first place. For instance I can reset a fields value in an update/insert trigger, but it would still have to get through the insert first.