Constraint on a column based on values at another column in another table - sql-server

I have table A which has two columns policy and rule. Table values are as below:
policy | rule
-------------
1 | A
-------------
1 | B
-------------
2 | C
-------------
3 | D
-------------
I have another table B with a column (id).
I want to write a constraint on this table B at column (id) such that if a number of distinct policies in table A are equal to 1, then the value of (id) should always be NULL.
I am using SQL Server and I am looking at a query-based solution (not via GUI steps).

Add CHECK Constraints into the table
create table tableA (policy int , [rule] char(1))
create table tableB (Id int , policy int )
GO
Function to check the constraint
CREATE FUNCTION CheckFnctn(#policy int) -- Adapt if necessary
RETURNS bit
AS
BEGIN
DECLARE #retval bit = 1
IF EXISTS(SELECT TOP 1 1 FROM tableA WHERE policy = #policy and policy = 1)
BEGIN
RETURN 0
END
RETURN #retval
END;
GO
Add the constraint into the table
ALTER TABLE tableB ADD CONSTRAINT ck_id_policy_1 CHECK (dbo.CheckFnctn(policy) = 1)
Test
INSERT tableA Values
(1,'A')
,(1,'B')
,(2,'B')
,(2,'D')
GO
(4 row(s) affected)
INSERT tableB (Id, policy) values (1,2)
GO
(1 row(s) affected)
INSERT tableB (Id, policy) values (1,1)
GO
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the CHECK constraint "ck_id_policy_1". The conflict occurred in database "XXXX", table "dbo.tableB", column 'policy'.
The statement has been terminated.
If you need to check if policy 1 exists you can never insert an Id different from null. Use this function and constraint
CREATE FUNCTION CheckFnctn(#Id int)
RETURNS bit
AS
BEGIN
DECLARE #retval bit = 1
IF EXISTS(SELECT TOP 1 1 FROM tableA WHERE policy = 1 AND #Id IS NOT NULL)
BEGIN
RETURN 0
END
RETURN #retval
END;
GO
ALTER TABLE tableB ADD CONSTRAINT ck_id_policy_1 CHECK (dbo.CheckFnctn(Id) = 1)
INSERT tableB (Id, policy) values (1,2)
GO
Msg 547, Level 16, State 0, Line 3
The INSERT statement conflicted with the CHECK constraint "ck_id_policy_1". The conflict occurred in database "XXXXX", table "dbo.tableB", column 'Id'.
The statement has been terminated.
INSERT tableB (Id, policy) values (NULL,1)
GO
(1 row(s) affected)

Related

I would like to sum all of the data in a specific table column, if the appropriate ID of that table exists in another one

I would like to SUM a value called amount from table 1, the considered, to be summed, values should only be the ones presenting in table 2. Meaning that, for the amount of row 1 in table 1 to be considered in the sum. the ID of that row 1 should be present in table 2.
Thanks,
This might be the answer but you really should have put some example tables in your example. I fancied helping as have 10 mins, this example you can run.
You can see that table 1 is referenced twice from Table2 and 3 just the once ,so the result ignores multiple occurrences, hence the WHERE EXISTS syntax.
This sums up all the numbers in Table1 that are referenced in Table2.
BEGIN TRANSACTION
BEGIN TRY
CREATE TABLE #Table1 (
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
[Number] DECIMAL NOT NULL
)
INSERT INTO #Table1 ([Number])
VALUES('1'),
('1'),
('1'),
('1')
SELECT * FROM #Table1
CREATE TABLE #Table2 (
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Table1Id INT NOT NULL,
CONSTRAINT FK_Table2_Table1 FOREIGN KEY (Table1Id) REFERENCES #Table1 (Id)
)
INSERT INTO #Table2 ([Table1Id])
VALUES('1'),
('1'),
('3')
SELECT * FROM #Table2
SELECT SUM(T1.Number) AS SummedNumbersThatAreReferencedByTable2
FROM #Table1 AS T1
WHERE EXISTS(
SELECT TOP 1 *
FROM #Table2 AS T2
WHERE T2.Table1Id = T1.Id
)
ROLLBACK TRANSACTION
END TRY
BEGIN CATCH
PRINT 'Rolling back changes, there was an error!!'
ROLLBACK TRANSACTION
DECLARE #Msg NVARCHAR(MAX)
SELECT #Msg=ERROR_MESSAGE()
RAISERROR('Error Occured: %s', 20, 101,#msg) WITH LOG
END CATCH
If this is the answer then please mark it as so, cheers

Throw error on invalid insertion in SQL Server

I have a SQL Server 2012 database with two tables:
CREATE TABLE Products
(
Id INT IDENTITY(1, 1) NOT NULL,
Code NVARCHAR(50) NOT NULL,
Name NVARCHAR(50) NOT NULL,
CONSTRAINT PK_Product
PRIMARY KEY CLUSTERED (Id ASC)
);
CREATE TABLE BlockedProductCodes
(
Code NVARCHAR(50) NOT NULL,
ReasonCode INT NOT NULL
CONSTRAINT PK_BlockedProductCodes
PRIMARY KEY CLUSTERED (Code ASC)
);
I want to be able to prevent products being inserted into the Products table if their product code exists in the BlockedProductCodes table.
The only way I could think of doing this was with a BEFORE INSERT trigger:
CREATE TRIGGER trg_Products_BEFORE_INSERT
ON Products
INSTEAD OF INSERT AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT Code
FROM BlockedProductCodes BPC
INNER JOIN inserted I ON BPC.Code = I.Code)
BEGIN
RAISERROR('The product has been blocked!', 16, 1);
END
ELSE
BEGIN
INSERT Product (Id, Code, Name)
SELECT Id, Code, Name
FROM INSERTED
END
SET NOCOUNT OFF;
END
But this caused an error with the identity column:
Cannot insert explicit value for identity column in table 'Products' when IDENTITY_INSERT is set to OFF
Can anyone suggest a way to fix this or a better approach?
Please note, this check is also made at the application level, but I want enforce that at the data table level.
Thanks.
Update: using check constraint
I have tried the following that seems to work..
CREATE FUNCTION dbo.IsCodeBlocked
(
#code nvarchar(50)
)
RETURNS BIT
WITH SCHEMABINDING
AS
BEGIN
DECLARE #ret bit
IF (#Code IN (SELECT Code FROM dbo.BlockedProductCodes))
SET #ret = 1
ELSE
SET #ret = 0
RETURN #ret
END
GO
ALTER TABLE Products
ADD CONSTRAINT CheckValidCode
CHECK (dbo.IsCodeBlocked(Code) = 0);
GO
insert Products (Code, Name) values ('xyz', 'Test #1')
go
insert Products (Code, Name) values ('abc', 'Test #2')
-- Fails with "The INSERT statement conflicted with the
-- CHECK constraint 'CheckValidCode'."
go
I am not sure if it is particularly 'safe' or performant. I will also test out the indexed view approach suggested by Damien.
One way you can implement this is by abusing an indexed view:
CREATE TABLE dbo.Products (
Id INT IDENTITY (1, 1) NOT NULL,
Code NVARCHAR(50) NOT NULL,
Name NVARCHAR(50) NOT NULL,
CONSTRAINT PK_Product PRIMARY KEY CLUSTERED (Id ASC)
);
GO
CREATE TABLE dbo.BlockedProductCodes (
Code NVARCHAR(50) NOT NULL,
ReasonCode INT NOT NULL
CONSTRAINT PK_BlockedProductCodes PRIMARY KEY CLUSTERED (Code ASC)
);
GO
CREATE TABLE dbo.Two (
N int not null,
constraint CK_Two_N CHECK (N > 0 and N < 3),
constraint PK_Two PRIMARY KEY (N)
)
GO
INSERT INTO dbo.Two(N) values (1),(2)
GO
create view dbo.DRI_NoBlockedCodes
with schemabinding
as
select
1 as Row
from
dbo.Products p
inner join
dbo.BlockedProductCodes bpc
on
p.Code = bpc.Code
inner join
dbo.Two t
on
1=1
GO
CREATE UNIQUE CLUSTERED INDEX IX_DRI_NoBlockedCodes on dbo.DRI_NoBlockedCodes (Row)
And now we attempt to insert:
INSERT INTO dbo.BlockedProductCodes (Code,ReasonCode) values ('abc',10)
GO
INSERT INTO dbo.Products (Code,Name) values ('abc','def')
And we get:
Msg 2601, Level 14, State 1, Line 42
Cannot insert duplicate key row in object 'dbo.DRI_NoBlockedCodes' with unique index 'IX_DRI_NoBlockedCodes'. The duplicate key value is (1).
The statement has been terminated.
So if that error message is acceptable to you, this could be one way to go. Note, that if you have a numbers table, you can use that instead of my dummy Two table.
The trick here is to construct the view in such a way so that, if there's ever a match between the Products and BlockedProductCodes tables, we produce a multi-row result set. But we've also ensured that all rows have a single constant column value and there's a unique index on the result - so the error is generated.
Note that I've used my convention of prefixing the table name with DRI_ when it exists solely to enforce an integrity constraint - I don't intend that anyone will ever query this view (indeed, as shown above, this view must always in fact be empty)

How to prevent a single date overlap in sql server 2012

I have a table EmployeeLeaves having columns:
EmployeeID int,
LeaveTypeID int,
LeaveDate datetime2,
IsHalfDay bit,
DateCreated datetime2,
Createdby varchar
I request you to help me out with a trigger that prevent same date for the column "LeaveDate". There are no date ranges like "Todate" and "FromDate" in the table.
Thanks in advance.
Please refer the sample demo,
CREATE TABLE EmployeeLeaves
(
EmployeeID INT,
LeaveTypeID INT,
LeaveDate DATETIME2,
IsHalfDay BIT,
DateCreated DATETIME2,
Createdby VARCHAR(50)
)
insert into EmployeeLeaves
values (1,1,'2016-01-01',0,getdate(),'Admin'),
(2,1,'2016-01-01',0,getdate(),'Admin'),
(3,1,'2016-01-01',0,getdate(),'Admin'),
(4,1,'2016-01-01',0,getdate(),'Admin'),
(5,1,'2016-01-01',0,getdate(),'Admin')
SELECT *
FROM EmployeeLeaves
METHOD-1 using Unique Constraint
--Introduce the unique constraint
ALTER TABLE EmployeeLeaves
ADD CONSTRAINT uq_employeeid_leavedate UNIQUE (EmployeeId, LeaveDate)
--Try to create overlap
insert into EmployeeLeaves
values (1,1,'2016-01-01',0,getdate(),'Admin')
--You will get the following error
--Msg 2627, Level 14, State 1, Line 1
--Violation of UNIQUE KEY constraint 'uq_employeeid_leavedate'. Cannot insert duplicate key in object 'dbo.EmployeeLeaves'. The duplicate key value is (1, 2016-01-01 00:00:00.0000000).
--The statement has been terminated.
--Insert proper date
insert into EmployeeLeaves
values (1,1,'2016-01-02',0,getdate(),'Admin')
--Check the result
SELECT *
FROM EmployeeLeaves
METHOD-2 using Instead of trigger
ALTER TRIGGER Trigger_Test
ON EmployeeLeaves
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS(SELECT TOP 1 1
FROM inserted I
INNER JOIN EmployeeLeaves e
ON i.EmployeeID = e.EmployeeID
AND i.LeaveDate = e.LeaveDate)
BEGIN
RAISERROR (N'Overlapping range.',16,1);
ROLLBACK TRANSACTION;
END
ELSE
BEGIN
INSERT INTO EmployeeLeaves
SELECT *
FROM inserted
END;
END
You can add a unique constraint on leave date field or you can check and raise exception using
if exist( select top 1 EmployeeID from table where EmployeeID = #emploeeid and
datediff(day,leavedate,#applydate)=0 and leavetypeid= #leavetypeid)
begin raise_error('leave already exist',1,1) end

SQL Server Identity Column Insert Fails

Hi guys I just have one quick question:
what happens when an insert statement fails on an identity column?
Is it possible that say for example that if I insert a row with an identity column, that identity column will be 1, and insert again but that fails and does not insert and data. Then try to insert again and that identity for that row is now 3?
Any advice will be much appreciated.
Thanks.
It depends on what the cause of the fail on data insert is. If for example the values are invalid (wrong types), then the identity value won't be incremented. However, if the first insert is successful, but is then removed (by a transaction failed and rolled back), then the identity value IS incremented.
-- Next identity value = 1
INSERT INTO Table1 (
field1)
VALUES ('a')
-- Next identity value = 2
BEGIN TRAN
INSERT INTO Table1 (
field1)
VALUES ('b')
-- Next identity value = 3
ROLLBACK TRAN
-- Next identity value = 3, although the insertion was removed.
INSERT INTO Table1 (
field1)
VALUES ('c')
-- Next identity value = 4
The first insert will have identity column value = 1, the second one fails, and the third one will have identity column value = 3.
Just because a column has an IDENTITY specification doesn't necessarily mean it's unique.
If you don't have a unique constraint (or a primary key constraint) on that column, you can definitely insert multiple identical values into rows for that column.
Typically, though, your IDENTITY columns will be the primary key (or at least have a UNIQUE constraint on them) and in that case, attempting to insert a value that already exists will result in an error ("unique constraint violation" or something like that)
In order to be able to insert specific values into an IDENTITY column you need to have the SET IDENTITY_INSERT (table name) ON - otherwise, SQL Server will prevent you from even specifying values for an IDENTITY column.
For illustration - try this:
-- create demo table, fill with values
CREATE TABLE IdentityTest (ID INT IDENTITY, SomeValue CHAR(1))
INSERT INTO IdentityTest(SomeValue) VALUES('A'), ('B'), ('C')
SELECT * FROM IdentityTest -- Output (1)
-- insert duplicate explicit values into table
SET IDENTITY_INSERT IdentityTest ON
INSERT INTO IdentityTest(ID, SomeValue) VALUES(1, 'Z'), (2, 'Y')
SET IDENTITY_INSERT IdentityTest OFF
SELECT * FROM IdentityTest -- Output (2)
-- add unique constraint
TRUNCATE TABLE dbo.IdentityTest
ALTER TABLE IdentityTest ADD CONSTRAINT UX_ID UNIQUE(ID)
INSERT INTO IdentityTest(SomeValue) VALUES('A'), ('B'), ('C')
SET IDENTITY_INSERT IdentityTest ON
INSERT INTO IdentityTest(ID, SomeValue) VALUES(1, 'Z') -- error message (3)
DROP TABLE IdentityTest
Output (1):
ID SomeValue
1 A
2 B
3 C
Output (2):
ID SomeValue
1 A
2 B
3 C
1 Z
2 Y
Error Message (3):
Msg 2627, Level 14, State 1, Line 9
Violation of UNIQUE KEY constraint 'UX_ID'. Cannot insert duplicate key in object 'dbo.IdentityTest'. The duplicate key value is (1).
One way is to prevent the insert if the row already exists.
IF (Not Exists (Select ID From Table Where SomeCol = #SomeVal)
Insert Into Table Values (#SomeVal)

How Can I avoid using Cursor for implementing this pseudo code - SQL Server

CREATE PROCEDURE p_processDataFor #accountId
BEGIN
for each item in
(select * from Accounts where accountId = #accountId and isProcessed = 0)
BEGIN
CASE current row
WHEN has x Condition THEN
exec p_x <Pass all data of current row>
WHEN has y Condition THEN
exec p_y <Pass all data of current row>
WHEN has z Condition THEN
exec p_z <Pass all data of current row>
END
END
END
You cannot normally avoid looping since you are calling EXEC, which cannot be done as a SET-based operation; it has to be done one by one.
If you just want to avoid CURSOR in general, you can implement it using a WHILE loop.
Otherwise, another option is to use a SELECT + FOR XML statement which builds the EXEC statements as a single NVARCHAR(MAX) statement into a variable, then EXEC just that dynamic SQL.
Okay, this example only does the insert for condition X, but hopefully shows you the way you could proceed:
create table T1 (
ID int IDENTITY(1,1) not null,
Val1 varchar(10) not null,
constraint PK_T1 PRIMARY KEY (ID)
)
go
create table T2 (
ID int not null,
Val2 varchar(10) not null,
constraint PK_T2 PRIMARY KEY (ID)
)
go
create table Val (
ID int IDENTITY(1,1) not null,
Val1 varchar(10) not null,
Val2 varchar(10) not null,
Processed bit not null,
CondX bit not null
)
go
Val is my table containing rows to be dealt with (in your example, Accounts). T1 and T2 are two tables that are currently inserted into/updated by your p_x procedure.
insert into Val(Val1,Val2,Processed,CondX)
select 'abc','def',0,1 union all
select 'ghi','jkl',0,0 union all
select 'mno','pqr',0,1
go
Just some sample data - I've got 3 rows, 2 of which match "condition x":
declare #Inter table (ValID int,T1ID int,Val2 varchar(10))
;merge into T1 using (select * from Val where CondX=1) Val on 1=0
when not matched then insert (Val1) values (Val.Val1)
output inserted.ID,Val.ID,Val.Val2 into #Inter (T1ID,ValID,Val2);
insert into T2(ID,Val2)
select T1ID,Val2 from #Inter
update Val set Processed = 1 where ID in (select ValID from #Inter)
go
For your actual work, you'd want 3 copies of the above - one for each of x, y and z. If it's inside the same stored proc, you'd need to use a different name for the #Inter table. The merge statement is being slightly abused, because you can't use an OUTPUT clause that references other tables from an insert statement. But we're using that in order to capture the generated IDENTITY values from T1, along with the corresponding data that's going to be inserted into other tables.
So now we'll use the table variable #Inter for a further insert into T2, and to eventually update Val to indicate that the rows have been processed. If there's a chain of tables where you need to insert and grab identity values, you'd need to introduce more merge statements and table variables.
select * from Val
select * from T1
select * from T2
And we get our results:
ID Val1 Val2 Processed CondX
----------- ---------- ---------- --------- -----
1 abc def 1 1
2 ghi jkl 0 0
3 mno pqr 1 1
(3 row(s) affected)
ID Val1
----------- ----------
1 abc
2 mno
(2 row(s) affected)
ID Val2
----------- ----------
1 def
2 pqr
(2 row(s) affected)
So we've performed all of our work for condition X, keeping the code set based throughout.

Resources