Solved

check constraint

Posted on 2010-11-25
6
1,107 Views
Last Modified: 2012-05-10
I have two tables:

tbl_ProjectEmployee
PK-QualficiationID (Part of composite PK)

tbl_ProjectQualification
PK-QualificationID (Part of Composite PK)
Qualification Count

I want to place a check constraint on tbl_ProjectEmployee that does not exceed the QualificationCount in the second table.

Here's my code but I'm stuck:

ALTER TABLE dbo.tbl_ProjectEmployee ADD CONSTRAINT CK_QualificationID
    CHECK (QualificationID <=  (Select QualificationCount from dbo.tbl_ProjectQualification pq
    where pk.QualificationID = QualificationID)

Error:

Msg 1046, Level 15, State 1, Line 2
Subqueries are not allowed in this context. Only scalar expressions are allowed.


0
Comment
Question by:Glen_D
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34212216
as the error indicates: you cannot do that with a subquery.

however, as from sql 2008, you can workaround by creating a function:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2008/02/07/using-a-udf-in-a-check-constraint-to-check-validity-of-history-windows-start-end-date-windows.aspx
0
 

Author Comment

by:Glen_D
ID: 34212327
I thought a function may work better but stuck with this as well:

Create Function (fn_QualificationCount) (@qualificationID int)
as
Begin
Declare @pe int
Declare @q1 int
Declare @q2 int
Declare @count int
set @pe = (select QualificationID from dbo.tbl_ProjectEmployee)
set @q1 = (select QualificationCount from dbo.tbl_ProjectQualification
                  where QualificationID = @pe)
set @q2 = (select COUNT (QualificationID) from dbo.tbl_ProjectEmployee
                  where QualificationID = @pe)
set @count = (@q1 - @q2)

If @count <= @p1
then

Else

            
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34212375
let's see:
Create Function dbo.fn_QualificationCount (@qualificationID int) as int
as
Begin
 return (select QualificationCount 
              from dbo.tbl_ProjectQualification
             where QualificationID = @qualificationID
            )
end

Open in new window


and your constraint goes like this:
ALTER TABLE dbo.tbl_ProjectEmployee ADD CONSTRAINT CK_QualificationID
    CHECK (QualificationID <= dbo.fn_QualificationCount (QualificationID))

Open in new window

0
 

Author Comment

by:Glen_D
ID: 34212419
Angel...that was great except for a minor syntax error in the function:

Create Function dbo.fn_QualificationCount (@qualificationID int) returns int
as
Begin
 return (select QualificationCount
              from dbo.tbl_ProjectQualification
             where QualificationID = @qualificationID
            )
end

Everything wa sgreat...

Thanks

Glen
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34212441
glad I could help

sorry for the type, I am working in so many programming languages that sometimes I mess up ...

CHeers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question