Solved

check constraint

Posted on 2010-11-25
6
1,096 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
  • 3
  • 2
6 Comments
 
LVL 142

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 142

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 142

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now