0

Im using microsoft sql server and Im learning basics of sql... I have a table that contains name of patients and their primary doctors. I want to check that a doctor can not be a primary doctor for more than 4 patients here is what I have tried:

CREATE ASSERTION assertion12
(not exist(
select DOCTOR.SSN
from Patient, DOCTOR
where Patient.primaryDoctor_SSN = DOCTOR.SSN
group by DOCTOR.SSN
having count(primaryDoctor_SSN) >= 5
);

but I get the error in the first line: "could not find stored procedure" . (actually a red line comes under "CREATE" and shows this message )

What should I do?! I have check some sites and I saw writing assertions is like what I have done and I don't know why do I get this error.

Kadaj13
  • 1,423
  • 3
  • 17
  • 41
  • If I'm reading this correctly, you're trying to enforce that there's a maximum number of patients for any particular doctor? There *is* a way to do this declaratively in [SQL Server](http://stackoverflow.com/q/23510713/15498) but I'm still not sure I'd recommend it. I'd probably still go down the trigger route to enforce such a constraint. – Damien_The_Unbeliever Dec 05 '14 at 09:35
  • ALso see http://stackoverflow.com/q/6368349/27535 – gbn Dec 05 '14 at 09:52

2 Answers2

2

SQL Server does not support CREATE ASSERTION.

In fact, no current SQL products support CREATE ASSERTION properly. Sybase SQL Anywhere supports it but reportedly has problems allowing constraints to sometimes be violated. Rdb did support it when it was looked after by DEC (who apparently were influential in the early days of the SQL standards, probably the reason why CREATE ASSERTION is in the standard in the first place) but that product is no more.

STOP PRESS: according to their documentation, HyperSQL (HSQLDB) supports assertions (though I'm left wondering why they don't shout about it on their list of features).

Reference Does SQL Server 2008 support the CREATE ASSERTION syntax?

Community
  • 1
  • 1
Rajesh
  • 1,600
  • 5
  • 33
  • 59
  • Thanks a lot. Do you know how can I check that this situation should not happen?! (without the command create assertion) – Kadaj13 Dec 05 '14 at 09:40
1

SQL Server doesn't support Create Assertion , you can use a Check Constraint or Create Trigger as:

IF OBJECT_ID ('assertion12','TR') IS NOT NULL
   DROP TRIGGER assertion12;
GO
-- This trigger prevents a row from being inserted in the Patients table
-- when current patient's primary doctor already has patients = 4 (max 4)


CREATE TRIGGER assertion12 ON Patient
AFTER INSERT
AS
IF EXISTS ( select count(P.primaryDoctor_SSN)
            from Patient P 
            JOIN inserted AS i 
            ON p.primaryDoctor_SSN = i.primaryDoctor_SSN 
            group by P.primaryDoctor_SSN
            having count(P.primaryDoctor_SSN) =4
          )
BEGIN
RAISERROR ('Primary Doctor Assigned to the Patient already has 4 patients.', 16, 1);
ROLLBACK TRANSACTION;
RETURN 
END;
GO

-- This statement attempts to insert a row into the Patient table
-- The AFTER INSERT trigger is fired and the INSERT transaction is rolled back.

INSERT INTO Patient values (5,1)
GO

DEMO

Deepshikha
  • 9,896
  • 2
  • 21
  • 21