简体   繁体   中英

getting error: “could not find stored procedure” while making assertions

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM