How to find the error value in sql table - sql

I am inserting values from one table to another table. There are 100000 records in the table. But when I start to insert value from one table to another table there is a problem in any row in my data. I don't know where exactly is that. So how can I know that in which row value is error because the insert statement not complete? After error this stop nothing insert in table.
This is simple statement that I use:
INSERT INTO Person (FirstName, LastName,Email)
SELECT FirstName, LastName, Email
FROM Person.Contact

Without any additional information on the constraints imposed on the target table a very general, slow solution would be
a cursor loop over the selected rows
insert any row within try/catch
on exception output row and error message
For a specific answer to your problem provide more information.
An alternative method is to use SSIS: here you can provide a separate channel for the erroneous rows. This channel can be lead to a separate table collecting all rows causing an error.

quesion is hard to understand, but the usual suspects are:
Attempting to insert Null into a non-nullable column
Foreign Key violations
Constraint violations.
Attempting to insert wrong datatype.
Please edit you question to add error message.

I guess Person is the name of your schema, so maybe you mean
INSERT INTO Person.Person (FirstName, LastName,Email)
SELECT FirstName, LastName, Email
FROM Person.Contact

Related

ORA-01722! Why can't I INSERT a NUMBER into a NUMBER data field without getting this error?

Can someone tell me what is going on here?
So I have a simple table location and it only has two columns; one is a number and the other varchar2.
I'm simply trying to insert some data into the locations table so I can get cracking with the other larger datasets but keep getting this damn error every time.
Error starting at line : 7 in command -
INSERT INTO location
VALUES (1, 'Head Office')
Error report -
ORA-01722: invalid number
NOTE: Before down-voting, YES - I have seen others posting about this but is usually for something less obvious than my situation where they are trying to enter a string into a number field or a number into a string field!
In my case however, the data in the INSERT statement is a number AND the data type is also NUMBER!
DATA STRUCTURE:
CREATE TABLE location(
locID NUMBER(4) NOT NULL,
locName VARCHAR2(100) NOT NULL
);
INSERT STATEMENT:
INSERT INTO location
VALUES (1, 'Head Office');
The error code can be seen above there where I first mentioned it.
Thanks in advance.
P.S. It may be worth mentioning that the ID field in 'location' table is being used as a FOREIGN KEY in a separate table 'employees'. I have however, checked that the data types matched!
EDIT #1: I'm using ORACLE SQL Developer
Always include the columns when doing an insert:
INSERT INTO location (locId, locname)
VALUES (1, 'Head Office');
From your description of the problem, this should not actually fix it. This is just a good habit.
The above is correct SQL for your table. If the error continues to happen it is probably coming from a trigger on the table.
Think like its stupid, you are getting number error from "head office" not from 1. Actually you are trying to insert string into number.
If you dont want to write column names to insert you should totally define all values in insert in place as located in table. I assume your table structure is
locId|locNumber
So your insert should be like below
insert into table values (1,'head office')
I hope you understand shortcut logic

assistance needed with sql statements/expressions

New to sql statements etc and I have an issue with what i am doing using squirrelSQL on linux machine
I Created a table and used the following sql statements:-
INSERT INTO FIRSTTABLE VALUES
(11,'TEN','STEVE'),(21,'TWENTY','JO'),(31,'THIRTY','KIDS')
ALTER TABLE FIRSTTABLE
ADD SURNAME VARCHAR(15);
this works fine however when i attempt to insert data/values into the the surname row i keep experiencing errors, the SQL statement i am using is:-
INSERT INTO FIRSTTABLE (SURNAME)
VALUES ('THOMAS'),('THOMAS'),('THOMAS'),('THOMAS');
This particular statement returns the following error:-
Error: Column 'ID' cannot accept a NULL value.
SQLState: 23502
ErrorCode: 30000
I only wish to add data/values into the surname column,after creating a new column with the alter table statement, i have tried many different combinations including using a SELECT statement prior to the INSERT statement above which also gives errors any guidance will be greatly appreciated,
You are inserting into Surname, without assigning a value to the other fields. You are getting this error message because ID is blank, and should not.
Understand that INSERT creates new rows. If you wish to modify existing rows, use UPDATE
In this case you could use UPDATE FIRSTTABLE SET SURNAME='THOMAS';
Omitting the WHERE clause affects all the fields in the table.
Hope it helps, and good luck in your learning process!
The approach is wrong, you need to:
UPDATE FIRSTTABLE SET SURNAME='THOMAS' WHERE ID IN (11, 21, 31)
Inserting will add a new row to the table. So you need to update a row using
UPDATE FIRSTTABLE SET SURNAME="THOMAS" WHERE ID=11

SQL Statement Error table to table

Trying insert table data into another table,
But I'm getting the following error:
Msg 2627, Level 14, State 1, Line 4
Violation of PRIMARY KEY constraint 'PK___4__10'. Cannot insert duplicate key in object 'dbo.tbl_Diagnosis_Table'.
Appears to be a duplicate primary key between both tables. Both tables have the same fields and data types, different data. What query can resolve this issue?
INSERT INTO tbl_Diagnosis_Table
SELECT *
FROM tbl_Holding_Diagnosis_Table
INSERT INTO tbl_Diagnosis_Table(Code, [Description], Comments, Discontinued)
(SELECT
Code, [Description], Comments, Discontinued
FROM
tbl_Holding_Diagnosis_Table);
Assuming Code is the primary key this should eliminate the duplicate rows from the insert:
INSERT INTO tbl_Diagnosis_Table (Code, [Description], Comments, Discontinued)
SELECT Code, [Description], Comments, Discontinued
FROM tbl_Holding_Diagnosis_Table
WHERE tbl_Holding_Diagnosis_Table.Code NOT IN
(SELECT Code FROM tbl_Diagnosis_Table)
If the primary key is some other column, or a composite key, you might need to use a join instead.
You might want to look at the MERGE statement if you want to update existing rows and only insert new.
You need a WHERE with an IN Clause To filter the records to insert, but first you need to know wich fields form the primary key.
If what you're saying is correct i.e. all the values are unique, it leaves only one option. Make sure that if there is an identity column in table tbl_diagnosis_table, you are setting the IDENTITY_INSERT to ON on this table and providing values manually in the select. It might have been possible that the seed and increment was reset in the past. In case you were wrong, you have to use a where clause as suggested by others.
I was going to suggest using a Merge query to do insert or update until I noticed the two inserts in the sample code both do the same insert. The error also says the error is on line 4 which is where the second insert occurs. If the two inserts aren't two examples of the problematic code, then the resolution may be as simple as removing one of the inserts.
Otherwise the other answers are correct, the duplicate rows need to be filtered and the IDENTITY_INSERT has to be turned on for the table.
SET IDENTITY_INSERT tbl_Diagnosis_Table ON -- if it is necessary to have the same primary key
MERGE tbl_Diagnosis_Table AS target
USING (SELECT Code, Description, Comments, Discontinued FROM tbl_Holding_Diagnosis_Table) AS source (Code, Description, Comments, Discontinued)
ON (target.Code = source.Code)
WHEN MATCHED THEN
UPDATE SET Description = source.Description,
Comments = source.Comments,
Discontinued = source.Discontinued
WHEN NOT MATCHED THEN
INSERT (Code, Description, Comments, Discontinued)
VALUES (source.Code, source.Description, source.Comments, source.Discontinued)
END; -- missing semicolons causes errors
SET IDENTITY_INSERT tbl_Diagnosis_Table OFF
Do your homework though. There are some very good reasons not to use Merge.
Use Caution with SQL Server's MERGE Statement
Indexed views and Merge
Optimizing Merge Statement Performance

Oracle SQL Trigger insert/update

Ok so my question should be an easy one i think.
I am just learning Triggers, and I am trying to figure out a homework question.
I have three tables,
Movies (title, year, length, genre, studioName, producer)
StarsIn (movieTitle, starName)
MovieStar (name, address, gender, birthdate)
So basically i need to write a trigger for assuring that at all times, any star appearing in StarsIn also appears in MovieStar. I need to make the trigger for both insert and update events.
UPDATE:
Ok so i changed my statement a little but i still can't figure this out
CREATE OR REPLACE TRIGGER movieTrigger
AFTER UPDATE OR INSERT ON STARSIN
FOR EACH ROW
WHEN(new.STARNAME NOT IN(SELECT "NAME" FROM MOVIESTAR))
BEGIN
INSERT INTO MOVIESTAR("NAME")
VALUES(new.STARNAME)
END;
Now I am getting the error
Error report:
ORA-02251: subquery not allowed here
02251. 00000 - "subquery not allowed here"
*Cause: Subquery is not allowed here in the statement.
*Action: Remove the subquery from the statement.
I just learned that oracle does not support a subquery in the when clause...
So i am trying to figure this out with limited knowledge. But if anyone has a clever way of doing this i would really like to know :-).
Thanks again
You created a statement-level trigger. It will fire once for each insert or update statement. But a single insert or update statement can insert/update many rows in one go. Your code however needs a single row and assumes that only a single row is inserted or updated.
What you need are rowlevel triggers ("FOR EACH ROW") if you want to follow this path.
Your trigger should be fired before(in this case) the insert of any row (currently it fires once for multiple rows inserted once)
I would recommend reading http://docs.oracle.com/cd/A97630_01/appdev.920/a96590/adg13trg.htm
Its always better to use a foreign key constraint for ensuring that the value that appears in table b is present in table a, in this case there should be a foreign key in your table StarsIn for column starName referencing name in MovieStar table

Getting INSERT errors when I do UPDATE?

At work we have a SQL Server database. I don't know the db that well. I have created a new column in the table for some new functionality....straight away I have started seeing errors
My statement was this:
ALTER TABLE users
ADD locked varchar(50) NULL
GO
The error is:
Insert Error: Column name or number of supplied values does not match table definition
I have read that the error message appears when during an INSERT operation either the number of supplied column names or the number of supplied values does not match the table definition.
But I have checked so many times and i have changed the PHP code to include this columns data yet I still receive the error.
I have run the SQL query directly on the db and still get the error.
Funny enough the query which gets the error is an Update.
UPDATE "users"
SET "users"."date_last_login" = GETDATE()
WHERE id = 1
Have you considered it could be a trigger causing it? 
This is the error message you would get.
If its an Update action causing it check trigger actions that Updates on that table run.
Do it with:
#sp_helptrigger Users, 'UPDATE';
This will show triggers occuring with ‘update’ actions.
If there is a trigger, grab the triggers name and run the below (but replace TriggerNameHere with real trigger):
#sp_helptext TriggerNameHere;
This will give you any SQL that the trigger runs and could be the INSERT the error message is referring to.
Hope this helps
Aside from TRIGGERS,
the reason for that is because you are using implicit type of INSERT statement. Let's say your previous number of columns on the table is 3. You have this syntax of INSERT statement,
INSERT INTO tableName VALUES ('val1','val2','val3')
which executes normally fine. But then you have altered the table to add another column. All of your INSERT queries are inserting only three values on the table which doesn't matches to the total number of columns.
In order to fix the problem, you have to update all INSERT statements to insert 4 values on the table,
INSERT INTO tableName VALUES ('val1','val2','val3', 'val4')
and it will normally work fine.
I'll advise you to use the EXPLICIT type of INSERT wherein you have to specify the columns you want to insert values with. Eg,
INSERT INTO tableName (col1, col2, col3) VALUES ('val1','val2','val3')
in this ways, even if you have altered your tables by adding additional columns, your INSERT statement won't be affected unless the column doesn't have a default value and which is non-nullable.