Hello,
I have the following table and Stored Procedure:
CREATE TABLE [dbo].[myChecks]( [CheckID] [int] IDENTITY(1,1) NOT NULL, [CustomerID] [int] NULL, [Category] int NULL, [CheckDescr] [nvarchar](50) NULL, [CheckResult] [bit] NULL ) GO INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (1,1,'Check1',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (1,1,'Check2',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (4,1,'Check3',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (4,1,'Check1',NULL) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (4,1,'Check2',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (1,1,'Check3',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (2,6,'Check1',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (2,6,'Check4',0) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (2,6,'Check5',1) INSERT INTO [dbo].[myChecks] (CustomerID,Category,CheckDescr,CheckResult) VALUES (2,6,'Check6',0)
CREATE PROCEDURE dbo.TestCursor
(@CustomerID int)
AS
BEGIN
DECLARE @CheckDescr as nvarchar(50);
DECLARE @CheckResult as bit;
DECLARE @Status int;
DECLARE @CheckCursor as CURSOR;
SET @CheckCursor = CURSOR FOR
SELECT CheckDescr,CheckResult
FROM dbo.myChecks
WHERE CustomerID=@CustomerID
OPEN @CheckCursor;
FETCH NEXT FROM @CheckCursor INTO @CheckDescr, @CheckResult;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Status= CASE @CheckResult
WHEN 1 THEN 1
WHEN 0 THEN 0
ELSE -1
END
IF @Status=1
FETCH NEXT FROM @CheckCursor INTO @CheckDescr, @CheckResult;
ELSE
BREAK;
END
CLOSE @CheckCursor;
DEALLOCATE @CheckCursor;
END
GO
With the above code I manage to get @Status=1 if all records have CheckResult=1, otherwise I get 0 when one of the records is 0 or -1 if one of the records is null.
The problem is that I want to check the records based on Category. If for example the Category=1 then I must have 3 rows with values (Check1, Check2, Check3) on CheckDescr and if that is true then to go and check the value of CheckResult.
Any kind of help would be appreciated.
Thank you in advance.