I have simplified the below code (for example).
I am trying to perform 2 validations.
A, I need to ensure that no duplicates are entered into the main table.
That seems to work well so far.
The other validation I am trying to make is to ensure that certain fields are not left blank(they must have values) for data to be successully inserted.
So far, that is not working.
Can someone please take a looka at this code and tell me what is wrong with it?
Thanks in advance.
SELECT @Counter = Count(*)
FROM tblMain
WHERE @ClientID = ClientID
IF @Counter > 0
BEGIN
RAISERROR('Record already exists for this client',16,1)
ROLLBACK TRANSACTION
END
ELSE
BEGIN
/* Create and populate new tblAccidentEvent Record */
INSERT INTO tblAccidentEvent(
ClientID,
OrgCode ,
EmPID,
AccidentDate,
AccidentTime
)
VALUES(
@ClientID,
@OrgCode,
@Empid,
@ClientDate,
@ClientTIme
)
End
select @OrgCode = OrgCode, @EmpID = EmpID
from tblMain
where @ClientID = ClientID
If @OrgCode is null or @Empid is null
raiserror("Orgcode and empid must be selected", 16,1)
End
END
I am trying to perform 2 validations.
A, I need to ensure that no duplicates are entered into the main table.
That seems to work well so far.
The other validation I am trying to make is to ensure that certain fields are not left blank(they must have values) for data to be successully inserted.
So far, that is not working.
Can someone please take a looka at this code and tell me what is wrong with it?
Thanks in advance.
SELECT @Counter = Count(*)
FROM tblMain
WHERE @ClientID = ClientID
IF @Counter > 0
BEGIN
RAISERROR('Record already exists for this client',16,1)
ROLLBACK TRANSACTION
END
ELSE
BEGIN
/* Create and populate new tblAccidentEvent Record */
INSERT INTO tblAccidentEvent(
ClientID,
OrgCode ,
EmPID,
AccidentDate,
AccidentTime
)
VALUES(
@ClientID,
@OrgCode,
@Empid,
@ClientDate,
@ClientTIme
)
End
select @OrgCode = OrgCode, @EmpID = EmpID
from tblMain
where @ClientID = ClientID
If @OrgCode is null or @Empid is null
raiserror("Orgcode and empid must be selected", 16,1)
End
END