Issue with fluent nhibernate TriggerIdentity in SQL Server? - sql-server

When I tried to insert data into a table in SQL Server, which has an ID column specification as following, I'm getting the error,
Cannot insert NULL in RowID column
even though I've written insert trigger for specifying the value of RowID column.
mapping.Id(x => x.Id, "RowID").GeneratedBy.TriggerIdentity();

You are using a FOR trigger; this is a different way of specifying an AFTER trigger. And you need to switch this to an "INSTEAD OF" trigger.Please refer to this webpage.
The reason it is failing now is because SQL Server validation is occurring prior to your trigger executing and performing it's primary key creation.

Related

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).

Insert into without defining PK results in violation of PK Constraint

Iam running a sql statement that will fail in around 1 of every 100.000 requests (so very rarely).
My problem is that the error message makes no sense.
I have a table with a PK called ID, i make an
INSERT INTO log_db (col1,col2) OUTPUT inserted.ID SELECT col3,col4 FROM otherTable
The ID column is not specified so the SQLServer should generate this automatically and ensure no dublicates are created.
SQLServer Error message:
-2147217900 [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_log_db'. Cannot insert duplicate key in
object 'dbo.log_db'. The duplicate key value is (556943).
Requests are being made using ADO in VB6 against a SQLserver 2012.
When this error occurs, use DBCC CHECKIDENT ('log_db', NORESEED) to see if a value higher than the current identity value has been inserted.
The results of this vary, so you should ignore the standard message and just look at the numbers. If the first number is less than the second, a record has been inserted in this way and your identity value is out of sync with the data values.

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

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.

update null primary key in a trigger using SQL Server

I'm migrating our system from Oracle to SQL SERVER. In Oracle we have insert triggers that are resposnible for setting primary key if not set. Below you will find code from PL/SQL.
create or replace trigger trigg1
before insert on table1
for each row
when (new.ID_T1 is null) -- if primary key is null
begin
select OUR_SEQ.nextval into :new.ID_T1 from dual;
end trigg1;
Now I have to do something similar in T-SQL. I found the solution, but unfortunatelly I have to list all the columns for the table trigger is created. This is something I want to avoid (model for the system is still very dynamic).
Is it possible to implement such trigger without listing all the columns in trigger?
Marcin

Changing column constraint null/not null = rowguid replication error

I have a database running under Sql server 2005 with merge replication. I want to change some of the FK columns to be 'not null' as they should always have a value. SQL server won't let me do that though, this is what it says:
Unable to modify table. It is invalid to drop the default constraint
on the rowguid column that is used by
merge replication. The schema change
failed during execution of an internal
replication procedure. For corrective
action, see the other error messages
that accompany this error message. The
transaction ended in the trigger. The
batch has been aborted.
I am not trying to change the constraints on the rowguid column at all, only on another column that is acting as a FK. Other columns I want to set to be not null because the record doesn't make any sense without that information (i.e. on a customer, the customer name).
Questions:
Is there a way to update columns to be 'not null' without turning off replication then turning it back on again?
Is this even the best way to do this - should I be using a constraint instead?
Apparently SSMS makes changes to tables by dropping them and recreating them. So just needed to make the changes using T-SQL statement.
ALTER TABLE dbo.MyTable ALTER COLUMN MyColumn nvarchar(50) NOT NULL
You need to script out your change in T-SQL statements as SQL Server Management Studio will look to drop and re-create the table, as opposed to simply adding the additional column.
You will also need to add the new column to your Publications.
Please note that changing a column in this manner can be detrimental to the performance of Replication. Dependent on the size of the table you are altering, can lead to a lot of data being replicated. Consider that although your table modification can be performed in a single statement, if 1 million rows are affected then 1 million updates will be generated at the Subscriber, NOT a single update statement as is commonly thought.
The hands on, improved performance approach.......
To perform this exercise you need to:
Backup your Replication environment by scripting out your entire configuration.
Remove the table from Replication at
both Publishers/Subscribers
Add the column at each
Publisher/Subscriber.
Apply the Update locally at each
Publisher/Subscriber.
Add the table back into Replication.
Validate that transactions are being
Replicated.

Resources