Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Check if record exists

Author  Topic 

Tones
Starting Member

17 Posts

Posted - 2011-07-19 : 05:39:08
I have a small database. 2 main tables called CaseDetails and Checks.

Each record in CaseDetails can have 3 checks recorded against it in the Checks table. But can only have one check of each type (check types 1, 2, 3).

I have 3 linkbuttons which will take the user to the input form for each 'check'. If a 'check' already exists i want to hide the linkbutton that takes the user to that 'check type' input form (the 3 input forms have different fields).

How can i connct to the SQL database and see if a record exists for each 'check type' and hide the linkbutton to prevent users from entering another record with the same 'check type'?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-07-19 : 06:05:25
This is one way:


Declare @CaseDetail table (
CaseDetailId int identity(1,1),
CaseName varchar(10),
Primary Key (CaseDetailId)
)

Declare @Check table (
CheckId int identity(1,1),
CaseDetailId int,
CheckTypeId int,
CheckSuccess bit
)

Insert Into @CaseDetail Select 'Case 1'
Insert Into @CaseDetail Select 'Case 2'
Insert Into @CaseDetail Select 'Case 3'
Insert Into @CaseDetail Select 'Case 4'

Insert Into @Check Select 1, 1, 1
Insert Into @Check Select 1, 2, 1
Insert Into @Check Select 1, 3, 1
Insert Into @Check Select 2, 1, 1
Insert Into @Check Select 2, 3, 1
Insert Into @Check Select 3, 2, 1


Select
A.CaseDetailId,
A.CaseName,
Check1 = SUM(isnull(Case when B.CheckTypeId = 1 then 1 else 0 end,0)),
Check2 = SUM(isnull(Case when B.CheckTypeId = 2 then 1 else 0 end,0)),
Check3 = SUM(isnull(Case when B.CheckTypeId = 3 then 1 else 0 end,0))
From @CaseDetail A
Left Join @Check B
On A.CaseDetailId = B.CaseDetailId
Group By A.CaseDetailId, A.CaseName


Corey

I Has Returned!!
Go to Top of Page
   

- Advertisement -