I have a table AUDITS
CREATE TABLE AUDITS
(
Audit_ID uniqueidentifier not null,
LocationID char(10)
FOREIGN KEY REFERENCES LOCATIONS (LocationID) not null,
DateChangement date not null,
NomColonneChangé varchar (20) not null,
AncienneValeur varchar (20) not null,
NouvelleValeur varchar (20) not null,
CONSTRAINT pk_AUDIT PRIMARY KEY (Audit_ID)
)
Now I have an update trigger that inserts into this table but I want the Audit_ID to be generated automatically by SQL Server.
I thought this way is right :
if (......)
....
...
INSERT INTO [MultiLocation].[dbo].[les_AUDITS]
([LocationID]
,[DateChangement]
,[NomColonneChangé]
,[AncienneValeur]
,[NouvelleValeur])
VALUES
(@LocationID ,
@DateChangement ,
@NomColonneChangée ,
@AncienneValeur,
@NouvelleValeur )
But it says it's impossible to insert a NULL value into Audit_ID.
How should I do it?
When you create a table, you can create a column with uniqueidentifier data type as you did and with the default DEFAULT newid() as below
CREATE TABLE AUDITS
(
Audit_ID uniqueidentifier not null DEFAULT newid(),
LocationID char(10) FOREIGN KEY REFERENCES LOCATIONS (LocationID) not null,
DateChangement date not null,
NomColonneChangé varchar (20) not null,
AncienneValeur varchar (20) not null,
NouvelleValeur varchar (20) not null,
constraint pk_AUDIT primary key(Audit_ID)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With