tadoquery post: "cannot update non-nullable column with null" on identity column - sql-server

I am trying to use a TAdoquery with MS SQL Server in a legacy delphi project.
The dataset contains a field that represents identity column in the SQL table. It has AutogenerateValue = arAutoInc and ProviderFlags [pfInWhere, pfInKey]. It also has Required = false.
After doing adoquery.Append I prefill fields and try to do adoquery.Post but get this error:
Non-nullable column cannot be updated to Null
All non-nullable fields are set to non-null before post, so the identity column is the only suspect. The TADOQuery has no joins (simple Select * from my_table), but it has look up fields and calculated fields, which may be null. For lookup fields and calculated fields I removed provider Flags to ensure those fields do not appear in the insert or update statement.
The identity field is NULL immediately before post, I see no insert command firing on the server in the Profiler, instead I just get this error.
Is it possible to preview the sql statement generated by the Tadoquery to insert a new row ?

I solved it, the issue was of course unrelated to the identity column.
The initial sql was select * from myTable, which meant that it also loaded a couple of extra columns that were not bound to any grid column in my DBGridEh.
After replacing * with an explicit list of the columns that I needed the INSERT worked.

Related

Cannot delete column with only null values when BlockOnPossibleDataLoss=true

I'm using a blue-green deployment strategy with expand contract database pattern. To achieve that on my database deploy schema I've setted the property BlockOnPossibleDataLoss=true because on Expand phase I can modify my database without any break change with the old version.
I had a column that is not necessary anymore so I followed those steps:
I've changed this column to allow null values
Then my new records don't fill this column anymore
I ran a script that setted null for this column to all table records
Now I need to delete this column, but even with all records with NULL value for this column I can't because I got this error:
Rows were detected. The schema update is terminating because data loss
might occur.'
How can I delete this column even using BlockOnPossibleDataLoss=true?
Create the table with the new schema (without the column you wan't to drop) with a temporary name. Something like tmp_YourTable (Not a temporary table)
Insert all data from the source table, to the newly created table
Drop the source table
Rename the new table, to the old table name. EXEC sp_rename 'tmp_YourTable', 'YourTable';

SQL Server Computed Column that doesn't affect existing data

is that possible to do a computed column without affecting existing data?
I have a unique index column named
"BookingNo"
For newly insert Column I want it to be following this format
(format([CreationTime],'yyMMddHHmmssff'))
I tried used Computed Column but it modified all my existing data.
my existing BookingNo format was
201800123
Is there anyway to generate via database? Or we have insert via code?
You could just add a default constraint:
ALTER TABLE TableName
ADD CONSTRAINT DF_BookingNo DEFAULT(format(SYSDATETIME(),'yyMMddHHmmssff'))
FOR BookingNo
This way you will get the value only for newly created rows.
Please note that if this column has a unique constraint on it some inserts might fail if they are executed at the same time.

EF - pass null value to SQL column so default value gets inserted

SQL Server table T2 has 2 columns:
Id INT NOT NULL
CreateDate DateTime NOT NULL, default = (getdate())
This statement inserts the CreateDate value correctly because it uses (getdate()) as default.
Insert T2 (Id)
Values (1)
So far so good. The problem is when I use Entity Framework to insert a row and still wish to use the default (getdate()) value.
Because the CreateDate is defined as NOT NULL, I cannot leave it blank or leave out of the Insert statement when using EF. But I want SQL to generate the timestamp on the server/database side.
Is there a way to handle this?
Thanks to squillman's reference to another SO post, I was able to find the answer.
Go to EDMX diagram, and you can set the StoreGeneratedPattern property to achieve what I am trying to do.
There are three Database Generated Options
Computed : The database generates a value when a row is inserted or updated.
Identity : The database generates a value when a row is inserted.
None : The database does not generate values.
EDIT: Although the picture shows Identity, I had to change it to Computed. The reason is that Identity option only works if the row is Inserted only. If the row is ever updated (other columns updated), then it caused an error. The Computed option seems to work fine with Insert (runs the default script) and Updates (to other columns, default script does not run again).

SQL Server : copy the whole column to another column error

I am trying to move all the data from fælge column in the test table into the fælgen column in table test to with this query
INSERT INTO [dbo].[test2] ([Fælgen])
SELECT Fælge
FROM [dbo].[test];
but I am getting a error with it saying that it cant insert the null into column ET which is not that column i am trying to insert my data to
Msg 515, Level 16, State 2, Line 2
Cannot insert the value NULL into column 'ET', table 'OminiData.dbo.test2'; column does not allow nulls. INSERT fails.
While you are going to Insert data to table you need to make sure that all the constraints are passed and nothing violate them.
You need to make sure that all Non-Null columns have value while inserting the new record. In fact you can not Insert value to single column in a table without considering the constraints and providing value for non-null-able columns.
Finally to overcome this error you have two choice:
Update the destination column(If there is a one-one or one-many relation between Test and Test2, This will need using Update based on Join, other wise a simple Update will be the solution)
Provide non-null value for ET
INSERT INTO [dbo].[test2] ([Fælgen], [ET])
SELECT Fælge , '' as ET
FROM [dbo].[test];
Press right mouse button on the table, chose Script table as... -> INSERT. There will be a script template for insertion. All columns mentioned in this script should have values determined by there datatypes.
I was working on same error . found this from this link SQL Server: copy data from one column to another column?
Using Merge I was able to copy data from one column to another in two different table.
Make sure data type for both columns need to get copied, and row count for both tables.
You cannot insert values to a single DB columns. You always insert entire rows. So your statement doesn't really mean insert some values to this column. What it really means is insert some values to this column and null/default values to all other columns. If any of those other columns does not allow nulls and doesn't have a non-null default, insert will fail.

i started this replication wizard it adds GUID FIELD, how should i add data to this manually in future?

using replication in sql srv causes the addition of this guid field, it also adds a value to it
but when i insert new records to the db, i have to give somthing or the guid field
it should be like aaaaa-aaa-something and unique!!
this is a problem for me , how am i supposed to do this keeping it unique every time?
should sql srv automatically add some yhing?
The ROWGUIDCOL added by Merge Replication should be populated with... guids:
ROWGUIDCOL does not enforce uniqueness
of the values that are stored in the
column and does not automatically
generate values for new rows that are
inserted into the table. To generate
unique values for each column, either
use the NEWID function on INSERT
statements or specify the NEWID
function as the default for the
column.

Resources