SQL insert into fails using substring from same table - sql

I would like to create process using substring from table1.column2 and insert results into table1.column6. I am trying the query below but my insert fails. It tries to insert into another column (column1) which is the id column. I specify column6 but it does not work.
Insert into table1(column6)
Select substring(column1,1,3)
from table1
Error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'Column1ID', table 'dbo.table1'; column does not allow nulls. INSERT fails.

If your table contain data , but column6 is empty (null), you must use update command
update table1 set column6 = substring(column1,1,3)
that use data stored into table1 and update column6

There is a fundamental difference between the insert and update statements - insert creates a brand new row, whereas update updates a row that already exists (even if data in a particular column for a row does not yet exist)
What are differences between INSERT and UPDATE in MySQL? - i realize you're sql server but same principle applies.
It sounds like you may actually be trying to update a column value, not insert a new row with a value in a column.
Given the following table and data:
table1
----
Id int primary key identity(1,1)
column1 varchar(50) not null
column6 varchar(50) null
id column1 column6
-----
1 some value null
2 some other value null
note that if you were to attempt your query:
insert into table1(column6) select substring(column1,1,3) from table1
this would attempt to take "some value" and "some other value" into new rows 3 and 4, with null in column1, and the substring values into column6. Note that this would fail because you're attempting to insert rows into table1 with null values (column1) in a not nullable column.
what you (probably) actually want is something like:
update table1
set column6 = substring(column1,1,3)
which will update the table for (in this case all rows) setting column6s value to the substring values of column1

I think you are trying to update the table. Use ISNULL to avoid null values.
UPDATE table1 SET column6 = ISNULL(SUBSTRING(column1,1,3), '')
If you insist on insert. Do check all the columns accept null other than Column6. And Query will insert a bulk of data.
Insert into table1(column6)
Select ISNULL(SUBSTRING(column1,1,3), '')
from table1

Related

INSERT INTO a table with Computed and Default values

I have a TableA with several columns in it, one of which is a Computed value, and another is a DateTime with a default value of GETDATE().
Then I have another table, TableA_Staging, which I want to use as a raw dumping table for bulk inserts. This table looks a lot like TableA with a few expected differences, one of which being it doesn't have the Computed or the DateTime column in it.
Once I've done a bulk insert into TableA_Staging, I now need to move data from TableA_Staging to TableA. I'm running into a snag with those two columns. Let's assume TableA looks like this:
TableA
-----------
TableAId (INT non-unique, non-auto-incrementing PK)
Column1 (String PK)
Column2
ColumnComputed
ColumnDateTime
And...
TableA_Staging
-----------
TableAID (this value populated in C# code)
Column1
Column2
Now, I'm trying to do this:
INSERT INTO TableA
SELECT TableAID, Column1, Column2 FROM TableA_Staging WHERE TableAID > X
But I get this error:
Column name or number of supplied values does not match table definition.
I assume it's complaining because I am not providing anything for ColumnComputed or ColumnDateTime? But if so, I didn't think I would need to provide values for them, as one is computed, and the other has a default value.
You should always include the columns in your insert statement that you are inserting from your select, otherwise you always have to provide the same number of columns in your insert as your select.
Also if your TableID is autoincrement/identity you do not need to include that.
INSERT INTO TableA (TableBID, Column1, Column2)
SELECT TableAID, Column1, Column2
FROM TableA_Staging WHERE TableAID > X

SQL Server : INSERT INTO SELECT doesn't insert into the correct column

I'm using SQL Server 2012 to try to take the values of one column in a table and put them into the values of another column table in another. If I try to run the following query:
INSERT INTO table2 (column3)
SELECT column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name
For table2, column3 is the same type (an int), NULL values are allowed. But when I try to execute the query, it returns:
Cannot insert the value NULL into column 'column1', table 'dbo.table2';, column does not allow nulls. INSERT fails.
But I'm not trying to insert into column1... Is it just a syntax thing where the order of the columns HAVE to match?
You are inserting into column1. Remember, you are inserting entire rows of values, so you should really have a value for all columns. Your query is equivalent to:
INSERT INTO table2 (column1, column2, column3)
SELECT NULL, NULL, column3
FROM table1
WHERE (ScopeID IS NOT NULL)
ORDER BY Name;
(and so on for all the columns in the table.)
I am guessing that you actually want an update, but your question doesn't provide enough information to give further guidance.

Update a column of each and every newly inserted (same row) record on a table depends on another column value without Trigger and default constraint

I have a table with an identity column (say Column1) and an integer column Column2 (nullable)
While inserting a row into this table, if the value passed to the Column2 is null, copy the Column1 value. If not null value is passed as argument for column2, retain the given value.
Only after the every insert, this check and update should be done.
But as per the instruction given to me, should not use default constraint and trigger (after insert).
This is for audit purpose. The record from this table will be moved/switched from one table to another table. The volume and transaction is very high (billions of records).
Can you help me on this?
Thanks,
Baskaran
Insert into tablename (column1, columns2)
Values (value1, ISNULL(Value2,value1))
I think this what you are expecting.. No trigger just on insert
If you really want to do this in a trigger, try something like this:
CREATE TRIGGER TrgAfterINsert
ON dbo.YourTable
AFTER INSERT
AS
UPDATE t
SET Column2 = i.Column1
FROM dbo.YourTable t
INNER JOIN Inserted i ON i.Column1 = t.Column1
WHERE t.Column2 IS NULL
This basically updates the YourTable table after an INSERT, and any rows that were inserted with a NULL in column2 are updated so that column2 is equal to the column1 value for that row

ORA-00947 - not enough values: Occurs in one server but not another

I am work on a project which has to add one column to the exist table.
It is like this:
The OLD TBL Layout
OldTbl(
column1 number(1) not null,
column2 number(1) not null
);
SQL TO Create the New TBL
create table NewTbl(
column1 number(1) not null,
column2 number(1) not null,
**column3 number(1)**
);
When I try to insert the data by the SQL below,
on one oracle server,it was successful executed,
but on another oracle server, I got "ORA-00947 error: not enough values"
insert into NewTbl select
column1,
column2
from OldTbl;
Is there any oracle option may cause this kind of difference in oracle?
ORA-00947: not enough values
this is the error you received, which means, your table actually has more number of columns than you specified in the INSERT.
Perhaps, you didn't add the column in either of the servers.
There is also a different syntax for INSERT, which is more readable. Here, you mention the column names as well. So, when such a SQL is issued, unless a NOT NULL column is missed out, the INSERT still work, having null updated in missed columns.
INSERT INTO TABLE1
(COLUMN1,
COLUMN2)
SELECT
COLUMN1,
COLUMN2
FROM
TABLE2
insert into NewTbl select
column1,
column2
from OldTbl;
The above query is wrong, because your new table has three columns, however, your select has only two columns listed. Had the number and the order of the columns been same, then you could have achieved it.
If the number of the columns, and the order of the columns are different, then you must list down the column names in the correct order explicitly.
I would prefer CTAS(create table as select) here, it would be faster than the insert.
CREATE TABLE new_tbl AS
SELECT column1, column2, 1 FROM old_tbl;
You could use NOLOGGING and PARALLEL to increase the performance.
CREATE TABLE new_tbl NOLOGGING PARALLEL 4 AS
SELECT column1, column2, 1 FROM old_tbl;
This will create the new table will 3 columns, the first two columns will have data from the old table, and the third column will have value as 1 for all rows. You could keep any value for the third column as per your choice. I kept it as 1 because you wanted the third column as data type NUMBER(1).

how to insert a row back in same table with a column count increase by +1

how to insert a row back in same table with a column count increase by +1
insert into Columns
select columns
where count of column 3 increase by 1
Do you mean something like this:
insert into YourTable (column1, column2, column3, column4)
select column4, column1, column2, column3 from YourTable
In that case data will be copied into YourTable and data from first column will be in second column, from second in third ... data from the last column will be in first.
If you just want to take a column and increase the value of one column by one, I don't see why you'd take it out and insert it again when you can just UPDATE the data:
UPDATE sometable
SET somecolumn = somecolumn + 1
WHERE someothercolumn = somevalue
Based on your comment, maybe you just want an identity column.
If you create a table like this:
CREATE TABLE myTable (
id INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
)
This should make it so every time you insert a row into myTable, the new row has an id which is one greater than any other in the table.
Presumably, you can ALTER a table to add an identity column too.
I used DB2 syntax because that's what this is tagged as. If you're using another database, the syntax will be much simpler.