I'm using following code to update foreign key value in Residence table on update of ProvinceID, but I get this error:
update statement conflicted with reference constraint "FK_Residence_Province". The conflict occurred in database '', table "dbo,Residences", column "ResidenceState".
Code:
CREATE trigger [dbo].[UPOnProvinceDelete]
ON [seniors].[dbo].[Provinces]
FOR UPDATE AS
SET NOCOUNT ON
IF UPDATE([ProvinceID])
BEGIN
UPDATE seniors.dbo.Residences
SET [Residences].[ResidenceState] = inserted.[ProvinceID]
FROM [Residences], deleted, inserted
WHERE deleted.[ProvinceID]= [Residences].[ResidenceState]
AND deleted.[ProvinceID]= inserted.[ProvinceID]
END;
Related
I have a SSIS workflow where I fill a SQL Server table (260 data rows). In this SSIS workflow I have also implemented a lookup that checks whether the original row has changed. If so it should update it in the SQL Server table via a stored procedure.
This is my stored procedure:
ALTER PROCEDURE [dbo].[Update_MC_Countries]
#alphacode3char NVARCHAR(10),
#alphacode NVARCHAR(10),
#numcode NVARCHAR(10),
#german NVARCHAR(150),
#english NVARCHAR(150),
#region NVARCHAR(10),
#qmr NVARCHAR(10)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[MasterData_EDL_MC_Countries]
SET [alphacode3char] = #alphacode3char,
[alphacode] = #alphacode,
[numcode] = #numcode,
[german] = #german,
[english] = #english,
[region] = #region,
[qmr] = #qmr
I get this error:
An OLE DB record is available.
Source: "Microsoft SQL Server Native Client 11.0"
Hresult: 0x80040E2F
Description: "Violation of PRIMARY KEY constraint 'PK_MasterData_EDL_MC_Countries'. Cannot insert duplicate key in object 'dbo.MasterData_EDL_MC_Countries'. The duplicate key value is (DEU).".
Why does the code want to insert this row instead of updating it?
I don't get it. Any help is appreciated!
It's difficult to tell without seeing the dataset being updated, but perhaps you are updating a row with when there is already a row with as its primary key? I would question why you would be updating a unique key also, this would be a PK conflict. Could you show the Table Definition?
I am running into what seems to be a permission issue.
I have a database table that I created with a trigger that runs after a row update. These are the commands I used to create the trigger:
CREATE TRIGGER [dbo].[my_table_u]
ON [dbo].[my_table]
after UPDATE
AS
BEGIN
SET nocount ON;
UPDATE my_table
SET
last_updated_by = ( Host_name() + Suser_name() ),
last_updated_dt = Getdate()
FROM my_table
INNER JOIN inserted ON my_table.id = inserted.id
END
ALTER TABLE [dbo].[my_table] ENABLE TRIGGER [my_table_u]
GO
I am able to insert and delete rows from my_table, but whenever I go to update my_table, I get the following SQL error:
Msg 1088, Level 16, State 13, Procedure my_table_u, Line 16
[Batch Start Line 0] Cannot find the object "my_table"
because it does not exist or you do not have permissions.
However, if I drop and recreate the trigger without the last ENABLE TRIGGER command, like so:
CREATE TRIGGER [dbo].[my_table_u]
ON [dbo].[my_table]
after UPDATE
AS
BEGIN
SET nocount ON;
UPDATE my_table
SET
last_updated_by = ( Host_name() + Suser_name() ),
last_updated_dt = Getdate()
FROM my_table
INNER JOIN inserted ON my_table.id = inserted.id
END
/*** NO ENABLE TRIGGER COMMAND HERE ***/
GO
Then I am able to update rows without error.
Would anyone have any ideas what is happening here? Maybe some tips where I can start investigating?
Answering my own question.
The issue was that the ALTER TABLE [dbo].[my_table] ENABLE TRIGGER [my_table_u] line in the original CREATE TIGGER command ended up being part of the update trigger. That is, whenever this trigger ran, it also tried to run ALTER TABLE...ENABLE TRIGGER as well, and my user didn't have ALTER permissions granted.
This was confirmed when I granted the user ALTER permissions on my_table, and the problem went away.
The "correct" solution, as pointed out by Dale K in the comments, is to leave out the ENABLE TRIGGER command completely, or, alternatively, make sure it comes after the GO statement that creates the trigger.
I am trying to re-create a foreign key constraint that got deleted recently and SQL Server is not letting me. Here's the DDL that SQL Server Management Studio gave me:
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CleansingOperations SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CleansedData ADD CONSTRAINT
FK_CleansedData_CleansingOperations FOREIGN KEY
(
CleansedOperationID
) REFERENCES dbo.CleansingOperations
(
CleansingOperationID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.CleansedData SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
and here is the error that I get when I run it:
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_CleansedData_CleansingOperations". The conflict occurred in database "NetVis203", table "dbo.CleansingOperations", column 'CleansingOperationID'.
Msg 3902, Level 16, State 1, Line 1
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
What does it all mean? As far as I can see, it is saying that the constraint is 'conflicting' with itself!
It means that there's a row in CleansedData that doesn't have a corresponding row in CleansingOperations
SELECT CleansingOperationId FROM dbo.CleansedData
EXCEPT
SELECT CleansingOperationId FROM dbo.CleansingOperations
should return no rows for your statement to work.
There's a NOCHECK keyword, which gets around the error, but better to understand what rows would violate the foreign key.
The reason for the error message, is that SQL Server imagines the foreign key is in place and then asserts that all rows satisfy the constraint before committing the statement. The reason for the second error message has to do with error handling (which I can never get right either).
I did a search over the net but I couldnt find my answer
in oracle , if we to specify for the trigere if its insert or update , we write like this :
create or replace trigger TRG_LOGS
after INSERT or update or delete
ON TABOE_LOGS
FOR EACH ROW
DECLARE
V_USERNAME VARCHAR2(100);
BEGIN
if inserting then
insert into long_log(NAME) VALUE (:new.NAME)
ELSE if UPDATING THEN
insert into long_log(NAME) VALUE (:OLD.NAME)
END;
END;
Is throwing an error on Incorrect syntax near the keyword 'insert'.
For Sybase, each action is a seperate trigger:
create trigger TRG_LOGS_INS on TABOE_LOGS
for INSERT
as
DECLARE #V_USERNAME varchar(100)
BEGIN
insert into long_log
select NAME from INSERTED
END
....
create trigger TRG_LOGS_UPD on TABOE_LOGS
for UPDATE
as
DECLARE #V_USERNAME varchar(100)
BEGIN
insert into long_log
select NAME from DELETED
END
Not sure if my syntax is exactly right, but should get you pointed in the right direction. The INSERTED table (similar to Oracles new) stores the new records on either an insert or update action. The DELETED table (similar to Oracles old) stores the old records on either an update or delete action.
More information and examples can be found in the Sybase T-SQL Users Guide: Triggers
I have inside a stored procedure:
INSERT INTO SitesCategories(SiteID, CategoryID) VALUES(#SITEID, #TempCId);
This insert could throw exceptions because I have this constraint on the table:
ALTER TABLE dbo.SitesCategories
ADD CONSTRAINT UniqueSiteCategPair
UNIQUE (SiteId,CategoryID);
I made this constraint so that I would not have to check when inserting the uniques of the pairs (#SITEID, #TempCId). But I do not want that an SQl exception is thrown when this gets executed inside the stored procedure. How can I "catch" the exception inside the stored procedure and continue operations inside the procedure ?
BEGIN TRY
INSERT INTO SitesCategories(SiteID, CategoryID) VALUES(#SITEID, #TempCId);
END TRY
BEGIN CATCH
END CATCH;
If you create your UNIQUE CONSTRAINT with the IGNORE_DUP_KEY = ON setting, then duplicates will be silently ignored, e.g. they won't be inserted into your table, but no exception will be thrown, either:
ALTER TABLE dbo.SitesCategories
ADD CONSTRAINT UniqueSiteCategPair
UNIQUE (SiteId,CategoryID) WITH (IGNORE_DUP_KEY = ON);