SQL Server trigger not acting as expected - sql

I have these two tables:
CREATE TABLE [dbo].[Test_Table1]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[f_id] [int] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Test_Table2_Tbl]
(
[f_id] [int] IDENTITY(1,1) NOT NULL,
[text] [varchar](500) NULL
) ON [PRIMARY]
And a trigger:
CREATE TRIGGER [dbo].[trg_Test_Trigger_Delete]
ON [dbo].[Test_Table2_Tbl]
AFTER DELETE
AS
INSERT INTO Test_Table2_Tbl (text)
(SELECT id FROM deleted)
UPDATE Test_Table1_Tbl
SET f_id = NULL
WHERE f_id IN (SELECT id FROM deleted)
GO
The keen observer will realize that 'id' does not exist in 'deleted'.
SQL catches that error in the INSERT, but without the INSERT it will let you add the trigger without any complaints.
Why doesn't it catch that error? (Invalid column name 'id'.)
My second question is, why does the UPDATE statement update EVERY column to NULL when id is not found in deleted?
I'm just very confused and need so clarity on why every record matches that WHERE clause.

Your error in the UPDATE is because the query is treated as a correlated subquery:
UPDATE Test_Table1_Tbl
SET f_id = NULL
WHERE Test_Table1_Tbl.f_id IN (SELECT Test_Table1_Tbl.id FROM deleted d);
id doesn't resolve in deleted, so SQL looks out at the next level.
These are the scoping rules for subqueries. That is why you should always use qualified table names when you have subqueries:
UPDATE Test_Table1_Tbl
SET f_id = NULL
WHERE Test_Table1_Tbl.f_id IN (SELECT d.id FROM deleted d);

Related

Why modification not taking

Here i'm Using Sql Server And i create two tables Like
Cutst
CREATE TABLE [dbo].[AuditTab](
[LastUpdate] [datetime] NULL
) ON [PRIMARY]
GO
Id CustName
1 John
AuditTab
CREATE TABLE [dbo].[TblCust](
[Id] [int] NULL,
[Name] [varchar](50) NULL
) ON [PRIMARY]
GO
LastUpdate (Datetime)--
//Here its takes date
I wrote simple trigger function in Cust
ALTER TRIGGER [dbo].[lmnTrigger]
ON [dbo].[TblCust]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
Insert into [dbo].[AuditTab]([LastUpdate]) values(GETDATE())
END
When I update cust column John-To Joe it is throwing an error
Lack of primary key on the table can cause an issue, try to add PK to the table and test it again.
ALTER TABLE TblCust
ADD CONSTRAINT pk_TblCust PRIMARY KEY (Id)
Other possibility is that data has been truncated, but seems like your columns size is correct.
Is any chance you can provide full error message?

Insert using stored procedure

I want to insert a record into multiple tables at single time using a stored procedure. But if it already exists, that record could not be inserted. How can it? I need help. I have link between the tables.
CREATE TABLE [dbo].[tblrole]
(
[roleid] [INT] IDENTITY(1, 1) NOT NULL,
[rolename] [VARCHAR](50) NULL,
PRIMARY KEY CLUSTERED ([roleid] ASC)
)
It's normal that you cannot insert a duplicate record if you have a unique primary key.
You have been talking about multiple tables, but you've schown us just one table definition.
I I've understood well your problem, you would something like this:
create proc insert_data
-- params are coming here
as
if not exists(select 1 from your_target_table1 where column = #condition)
-- your insert comes here
else
-- do nothing or log en error in an error table or do an update
if not exists(select 1 from your_target_table2 where column = #condition)
-- your insert comes here
else
-- do nothing or log en error in an error table or do an update
-- and soon

Insert only modified values and column names into a table

I have a sql server 2012 database. In which i have a changeLog table that contains
TableName, ColumnName, FromValue and ToValue columns. Which will be used to keep track of modified columns and data.
So if any update occur through application then only modified columns should insert into this table with its new and old value.
Can anyone help me in this.
For Example:
If the procedure updates all columns of property table (propertyName, address)
then if user update propertyName (but update also contains address column but with no data change) then only propertyName and its data will be inserted into ChangeLog table not address column and its data because address data does not contains any data change.
IF there is no other auditing requirement at all - you would not be thinking about Auditing in any way without this - then OK, go for it. However this is a very limited use of Auditing: User X changed this field at time Y. Generally this is interesting as part of a wider question: what did user X do? What happened to that customer data in the database to end up the way it is now?
Questions like that are harder to answer if you have the data structure you propose and would be quite onerous to reconstruct. My usual approach would be as follows. Starting from a base table like so (this from one of my current projects):
CREATE TABLE [de].[Generation](
[Id] [int] IDENTITY(1,1) NOT NULL,
[LocalTime] [datetime] NOT NULL,
[EntityId] [int] NOT NULL,
[Generation] [decimal](18, 4) NOT NULL,
[UpdatedAt] [datetime] NOT NULL CONSTRAINT [DF_Generation_UpdatedAt] DEFAULT (getdate()),
CONSTRAINT [PK_Generation] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
(I've excluded FK definitions as they aren't relevant here.)
First create an Audit table for this table:
CREATE TABLE [de].[GenerationAudit](
[AuditId] int identity(1, 1) not null,
[Id] [int] NOT NULL,
[LocalTimeOld] [datetime] NULL,
[EntityIdOld] [int] NULL,
[GenerationOld] [decimal](18, 4) null,
[UpdatedAtOld] [datetime] null,
[LocalTimeNew] [datetime] null,
[EntityIdNew] [int] null,
[GenerationNew] [decimal](18, 4) null,
[UpdatedAtNew] [datetime] NOT NULL CONSTRAINT [DF_GenerationAudit_UpdatedAt] DEFAULT (getdate()),
[UpdatedBy] varchar(60) not null
CONSTRAINT [PK_GenerationAudit] PRIMARY KEY CLUSTERED
(
[AuditId] ASC
)
This table has an *Old and a *New version of each column that can't change. The Id, being an IDENTITY PK, can't change so no need for an old/new. I've also added an UpdatedBy column. It also has a new AuditId IDENTITY PK.
Next create three triggers on the base table: one for INSERT, one for UPDATE and one for DELETE. In the Insert trigger, insert a row into the Audit table with the New columns selected from the inserted table and the Old values as null. In the UPDATE one, the Oldvalues come from the deleted and the new from the inserted. In the DELETE trigger, old from from deleted and the new are all null.
The UPDATE trigger would look like this:
CREATE TRIGGER GenerationAuditUpdate
ON de.Generation
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
insert into de.GenerationAudit (Id, LocalTimeOld, EntityIdOld, GenerationOld, UpdatedAtOld,
LocalTimeNew, EntityIdNew, GenerationNew, UpdatedAtNew,
UpdatedBy)
select isnull(i.Id, d.Id), d.LocalTime, d.EntityId, d.Generation, d.UpdatedAt,
i.LocalTime, i.EntityId, d.Generation, getdate(),
SYSTEM_USER)
from inserted i
full outer join deleted d on d.Id = i.Id;
END
GO
You then have a full before/after picture of each change (and it'll be faster than seperating out diffs column by column). You can create views over the Audit table to get entries where the Old value is different to the new, and include the base table Id (which you will also need in your structures!), the user who did it, and the time they did it (UpdatedAtNew).
That's my version of Auditing and it's mine!

Supposedly easy trigger

I've created a trigger which is to block inserted records with a date already existing in a table.
CREATE TRIGGER [dbo].[SpecialOffers_Insert]
ON [dbo].[SpecialOffers]
FOR INSERT,UPDATE
AS
SET NOCOUNT ON
IF EXISTS (SELECT * FROM inserted WHERE SPO_DateFrom IN (SELECT SPO_DateFrom FROM dbo.SpecialOffers))
BEGIN
RAISERROR('Error. ', 16, 1)
ROLLBACK TRAN
SET NOCOUNT OFF
RETURN
END
SET NOCOUNT OFF
It is added to a table:
CREATE TABLE [dbo].[SpecialOffers](
[SPO_SpoId] [int] IDENTITY(1,1) NOT NULL,
[SPO_DateFrom] [datetime] NOT NULL,
[SPO_DateTo] [datetime] NOT NULL)
The table is empty but when trying to insert such record:
INSERT INTO dbo.SpecialOffers (SPO_DateFrom, SPO_DateTo) VALUES ('2015-01-15','2015-01-15')
I got the Error from the trigger. How should I modify the trigger not to get the error?
If the goal is to block inserted records with date already existing in a table, you don't need a trigger - just create a unique constraint on the date field:
ALTER TABLE [dbo].[SpecialOffers]
ADD CONSTRAINT SpecialOffersUQ UNIQUE (SPO_DateFrom)
If you wanted a trigger to prevent overlaps, why didn't you say so:
CREATE TABLE [dbo].[SpecialOffers](
[SPO_SpoId] [int] IDENTITY(1,1) NOT NULL,
[SPO_DateFrom] [datetime] NOT NULL,
[SPO_DateTo] [datetime] NOT NULL,
constraint CK_SO_NoTimeTravel CHECK (SPO_DateFrom <= SPO_DateTo)
)
GO
CREATE TRIGGER NoOverlaps
on dbo.SpecialOffers
after insert,update
as
set nocount on
if exists (
select *
from dbo.SpecialOffers so1
inner join
dbo.SpecialOffers so2
on
so1.SPO_DateFrom < so2.SPO_DateTo and
so2.SPO_DateFrom < so1.SPO_DateTo and
so1.SPO_SpoId != so2.SPO_SpoId
inner join
inserted i
on
so1.SPO_SpoId = i.SPO_SpoId
)
begin
RAISERROR('No overlaps',16,1)
ROLLBACK
end
Examples:
--Works
INSERT INTO SpecialOffers (SPO_DateFrom,SPO_DateTo)
values ('20010101','20011231')
GO
--Fails (Trigger)
INSERT INTO SpecialOffers (SPO_DateFrom,SPO_DateTo)
values ('20010101','20011231')
GO
--Fails (Constraint)
INSERT INTO SpecialOffers (SPO_DateFrom,SPO_DateTo)
values ('20011231','20010101')
GO
--Fails (Trigger)
INSERT INTO SpecialOffers (SPO_DateFrom,SPO_DateTo)
values ('20020101','20021231'),
('20020701','20030630')
I also added a check constraint so that I didn't have to deal with nonsense data in the trigger.
You might have to change swap some of the <s for <=s or vice-versa, depending on what definition of intervals you want to use (i.e. are DateFrom and DateTo meant to be inclusive or exclusive endpoints for the interval they're describing?)
Since the trigger runs in the transaction context of the SQL statement that fired it, after this INSERT, there will be a row in your table dbo.SpecialOffers with the SPO_DateFrom values you've just inserted and the SELECT from the table will succeed ...
Therefore, the trigger will assume that there's already been a value - and it throws the error (as designed).
You could rewrite the trigger to not look at the newly inserted rows, but anything else - but as others have pointed out, a UNIQUE constraint does that much more simply
You should check if the rows you found are actually NOT the ones you have just inserted. Change the line
IF EXISTS (
SELECT * FROM inserted
WHERE SPO_DateFrom IN (
SELECT SPO_DateFrom
FROM dbo.SpecialOffers)
)
To
IF EXISTS (
SELECT * FROM inserted a
WHERE SPO_DateFrom IN (
SELECT SPO_DateFrom
FROM dbo.SpecialOffers b
WHERE a.SPO_SpoId <> b.SPO_SpoId)
)

How to add a unique constraint on several columns, with a condition?

Question:
I want to add a unique constraint on a mapping table (n:n).
I want that new values may be inserted, but only if TEST_FK_UID, TEST_DateFrom and TEST_DateTo are not equal to an already existing entry.
The problem is the status field.
Status 1 means active..
Status != 1 means inactive/deleted..
..
So one may of course insert a new entry with the same FK, DateFrom and DateTo, IF - and only if - the status of the existing entry (all existing entries, as you can insert, delete, insert, delete, insert, delete, etc.) is != 1
Here is what I have so far:
CREATE TABLE dbo._________Test
(
TEST_UID uniqueidentifier NOT NULL
,TEST_FK_UID uniqueidentifier NOT NULL
,TEST_DateFrom DateTime NOT NULL
,TEST_DateTo DateTime NOT NULL
,TEST_Status int NOT NULL
,UNIQUE(TEST_FK_UID, TEST_DateFrom, TEST_DateTo, TEST_Status)
);
You cannot. You can, however, create a unique index. It functions similarly, and I expect well enough for you.
CREATE UNIQUE INDEX MyIndex
ON _________Test
( TEST_FK_UID
, TEST_DateFrom
, TEST_DateTo )
WHERE TEST_Status = 1
The most important difference between a unique index and a unique constraint is that you cannot create a foreign key in another table that references a unique index. Edit: as Martin points out, this is not true, a foreign key can reference a nonfiltered unique index.
Use Instead Of trigger on INSERT,UPDATE operations..
and check the existing values with the values in the INSERTED table(which is created in the case of triggers)
If the status in the INSERTED table id 1 AND if it is unique, do the insertion operation or just abort with some messages..
It is very possible, like this
(basic credit goes to: https://stackoverflow.com/users/103075):
Edit:
OK, pedantically seen it's not a unique constraint, it's a check constraint, but WTF - it has the same effect and works on SQL-Server 2005 as well, and the (conditional) condition is configurable per customer (replace SET #bNoCheckForThisCustomer = 'false' with a select to a configuration table) - that's not possible with a unique index AFAIK ... ;)
Note this line:
AND ZO_RMMIO_UID != #in_ZO_RMMIO_UID
(ZO_RMMIO_UID is the unique primary key of the n:n mapping table)
It's important, since a check constraint seems to be similar to a onAfterInsert trigger.
If this line is missing, it checks on itselfs as well, which leads to the function always returning true...
IF EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]') AND parent_object_id = OBJECT_ID(N'[dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt]'))
ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt] DROP CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
GO
-- ========================================================================
-- Author: Me
-- Create date: 09.08.2010
-- Last modified: 09.08.2010
-- Description: Conditionally check if row is a duplicate
-- ========================================================================
-- PRE: UID, Valid RM_UID, Valid MIO_UID,
-- Valid datetime-from for db usr language, valid datetime-to for db usr language
-- POST: True/False
CREATE FUNCTION [dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt](#in_ZO_RMMIO_UID uniqueidentifier, #in_ZO_RMMIO_RM_UID AS uniqueidentifier, #in_ZO_RMMIO_MIO_UID as uniqueidentifier, #in_ZO_RMMIO_DatumVon AS datetime, #in_ZO_RMMIO_DatumBis AS datetime)
RETURNS bit
AS
BEGIN
DECLARE #bIsDuplicate AS bit
SET #bIsDuplicate = 'false'
DECLARE #bNoCheckForThisCustomer AS bit
SET #bNoCheckForThisCustomer = 'false'
IF #bNoCheckForThisCustomer = 'true'
RETURN #bIsDuplicate
IF EXISTS
(
SELECT
ZO_RMMIO_UID
,ZO_RMMIO_RM_UID
,ZO_RMMIO_MIO_UID
FROM T_ZO_AP_Raum_AP_Ref_Mietobjekt
WHERE ZO_RMMIO_Status = 1
AND ZO_RMMIO_UID != #in_ZO_RMMIO_UID
AND ZO_RMMIO_RM_UID = #in_ZO_RMMIO_RM_UID
AND ZO_RMMIO_MIO_UID = #in_ZO_RMMIO_MIO_UID
AND ZO_RMMIO_DatumVon = #in_ZO_RMMIO_DatumVon
AND ZO_RMMIO_DatumBis = #in_ZO_RMMIO_DatumBis
)
SET #bIsDuplicate = 'true'
RETURN #bIsDuplicate
END
GO
ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt] WITH NOCHECK ADD CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
CHECK
(
NOT
(
dbo.fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt(ZO_RMMIO_UID, ZO_RMMIO_RM_UID, ZO_RMMIO_MIO_UID, ZO_RMMIO_DatumVon, ZO_RMMIO_DatumBis) = 1
)
)
GO
ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt] CHECK CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
GO
And here a test case:
CREATE TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt](
[ZO_RMMIO_UID] [uniqueidentifier] NOT NULL, -- <== PRIMARY KEY
[ZO_RMMIO_RM_UID] [uniqueidentifier] NOT NULL,
[ZO_RMMIO_MIO_UID] [uniqueidentifier] NOT NULL,
[ZO_RMMIO_DatumVon] [datetime] NOT NULL,
[ZO_RMMIO_DatumBis] [datetime] NOT NULL,
[ZO_RMMIO_Status] [int] NOT NULL,
[ZO_RMMIO_Bemerkung] [text] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/*
DELETE FROM T_ZO_AP_Raum_AP_Ref_Mietobjekt
WHERE ZO_RMMIO_Status = 1
AND ZO_RMMIO_RM_UID = '2007B6F5-9010-4979-AB39-00057DA353C0'
AND ZO_RMMIO_MIO_UID = 'FFA177E9-971E-4500-805D-00116F708E7B'
*/
INSERT INTO T_ZO_AP_Raum_AP_Ref_Mietobjekt
(
ZO_RMMIO_UID
,ZO_RMMIO_RM_UID
,ZO_RMMIO_MIO_UID
,ZO_RMMIO_DatumVon
,ZO_RMMIO_DatumBis
,ZO_RMMIO_Status
,ZO_RMMIO_Bemerkung
)
VALUES
(
NEWID() --<ZO_RMMIO_UID, uniqueidentifier,>
,'2007B6F5-9010-4979-AB39-00057DA353C0' --<ZO_RMMIO_RM_UID, uniqueidentifier,>
,'FFA177E9-971E-4500-805D-00116F708E7B' --<ZO_RMMIO_MIO_UID, uniqueidentifier,>
,'01.01.2012' --<ZO_RMMIO_DatumVon, datetime,>
,'31.12.2999' --<ZO_RMMIO_DatumBis, datetime,>
,1 --<ZO_RMMIO_Status, int,>
,NULL--<ZO_RMMIO_Bemerkung, text,>
)
GO
INSERT INTO T_ZO_AP_Raum_AP_Ref_Mietobjekt
(
ZO_RMMIO_UID
,ZO_RMMIO_RM_UID
,ZO_RMMIO_MIO_UID
,ZO_RMMIO_DatumVon
,ZO_RMMIO_DatumBis
,ZO_RMMIO_Status
,ZO_RMMIO_Bemerkung
)
VALUES
(
NEWID() --<ZO_RMMIO_UID, uniqueidentifier,>
,'2007B6F5-9010-4979-AB39-00057DA353C0' --<ZO_RMMIO_RM_UID, uniqueidentifier,>
,'FFA177E9-971E-4500-805D-00116F708E7B' --<ZO_RMMIO_MIO_UID, uniqueidentifier,>
,'01.01.2012' --<ZO_RMMIO_DatumVon, datetime,>
,'31.12.2999' --<ZO_RMMIO_DatumBis, datetime,>
,1 --<ZO_RMMIO_Status, int,>
,NULL--<ZO_RMMIO_Bemerkung, text,>
)
GO
SELECT [ZO_RMMIO_UID]
,[ZO_RMMIO_RM_UID]
,[ZO_RMMIO_MIO_UID]
,[ZO_RMMIO_DatumVon]
,[ZO_RMMIO_DatumBis]
,[ZO_RMMIO_Status]
,[ZO_RMMIO_Bemerkung]
FROM [T_ZO_AP_Raum_AP_Ref_Mietobjekt]