Solved

check constraint

Posted on 2010-11-25
6
1,098 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now