Getting INSERT errors when I do UPDATE? - sql

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.

Related

Inserting values based on another column SQL

I've added the column DVDAtTime and I'm and trying to insert values using a subquery. Seems rather straight forward but I keep getting an error that I can't insert null into (I believe) an unrelated field in the table. Ultimately, DVDAtTime should be the number shown in MembershipType
My code is as follows:
Insert Into Membership(DVDAtTime)
Select LEFT(MembershipType,1)
FROM Membership
I suspect you want to update each existing row, not insert new rows:
update membership
set DVDAtTime = left(MembershipType, 1)

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

How do I identify the error row when using batch insert (INSERT INTO table2 SELECT * FROM table1)

I use the following SQL statement to batch insert large amount of data into another table, for example:
INSERT INTO table2 (col1, col2)
SELECT col1, col2
FROM table1
WHERE condition and some logics ...;
Normally, roughly 5000 rows are inserted into table2.
However, if 2 rows are invalid in a batch, and they causes some an error when inserting data.
SQL Server raises an error and stops (or rollback) the statement.
And therefore, no row are inserted into table2 because of an error.
My questions are:
How do I insert all valid data as much as possible into a target table if an error occurs in a batch insertion ?
In addition, how do I identify the rows which cause errors after SQL Server raises errors ?
I searched the Internet for any possible solution, but I can't find any hit about it.
In transactional database management system, constraints are made to maintain data integrity and can be extremely various depending on the table schema and how you wrote them.
INSERT is a standard SQL operation that allows you to put information into your database according to your schema. Hence, to answer your questions:
How do I insert all valid data as much as possible into a target table if an error occurs in a batch insertion ?
With INSERT, there is no way you can achieve it directly in your operation as the constraints can be broadly scoped. But there is MAXERRORS argument that comes with BULK INSERT command allowing you to let valid data through when inserting records before it hit the error threshold and stop the operation.
The only way you can achieve it using INSERT command is by specifying WHERE clause to your INSERT ... SELECT ... command according to the destination table's constraints.
In addition, how do I identify the rows which cause errors after SQL server raises errors ?
Unfortunately SQL Server does not specifically point out the row since in most, or may be all RDBMS, INSERT is plain record insertion command. There is another argument in BULK INSERT command named ERRORFILE that you can use to check failed row and the reason behind.
Read a complete BULK INSERT reference here.
To sum it up, INSERT is not the most sophisticated know-it-all command there is for inserting data into SQL database. This function should be handled in your application side rather than database side.
However, there is BULK INSERT command that can take you there from a different approach.
Here if you want to insert all valid data then you need to add where condition and filter out invalid data like below
INSERT INTO table2 (col1, col2)
SELECT col1, col2 FROM table1 WHERE col1 IS NOT NULL AND col12 IS NOT NULL
We may add other condition which will cause issue in Insert, also same way we will also able to know the invalid data, just reverse the where condition as below:
SELECT col1, col2 FROM table1 WHERE col1 IS NULL AND col12 IS NULL

Creating Oracle Trigger to copy/INSERT single record on UPDATE into new table?

I am attempting to setup a Trigger in my Oracle Database that fires every time a record is UPDATE'd in Table1. When the trigger executes (after?), I want to copy/insert the :old record values for all fields in the record which was updated into a HistoryTable. The only difference between the 2 tables is the inclusion of a [Table1_ID] field to reference all history records for that particular Table1.[ID]:
[Table1] - [ID][col1][col2][col3][etc.]
[HistoryTable] - [ID][Table1_ID][col1][col2][col3][etc.]
Does anyone know how to set this up? I've attempt to script it as shown below with no luck thus far, and haven't figured out how to specify the action exactly within the TOAD gui. I've already got a Sequence/Trigger pair in place so that the [ID] field for HistoryTable is auto-incremented each time a new record is inserted.
CREATE OR REPLACE TRIGGER SCHEMA1.ITEM_UPDATED_TRG
AFTER UPDATE ON TABLE1
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO SCHEMA1.HISTORYTABLE (Table1_ID, col1, col2, col3, etc.)
VALUES (:OLD.ID, :OLD.col1, :OLD.col2, :OLD.col3, :OLD.etc);
END;
ShOW ERRORS;
Output window comes up with Warning: compiled but with compilation errors - No errors....?

SQL Server INSERT trigger how to get data of current row

I've never created a trigger before and I'm trying to read online but am a little confused.
I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.
I'm not sure how to write the T-SQL to get the data from the columns..
insert into [othetTable]
values ([col1 from row that was inserted], [col5 from row that was inserted])
What would the syntax be to get those values?
thanks
Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.
Therefore, you need to use something like the following syntax:
insert into othertable
select col1, col5
from inserted
This will insert a row into othertable for each inserted row.