Kevin Kevin - 7 months ago 21
SQL Question

tSQL check if input ID is valid and if the date is in the current year

I am doing a

tSQL
assignment, and one of the requirements is to check if the inputted
charity ID
exists and if not return an error, Then I have to check If the
Tax Date
of the charity is in the current year. This is what I have tried

IF NOT EXISTS (SELECT CharityID FROM CharityTbl WHERE [CharityID] = '@CharityID')
begin
raiserror ('Sorry Invalid Charity ID', 16,1)
return 99
end
--if the TaxNoDateOfIssue for the charitytbl is not in the current year raise an error
IF NOT Exists (Select TaxNoDateOfIssue FROM CharityTbl WHERE YEAR(TaxNoDateOfIssue) = YEAR(getdate())


I am unsure how I should write the second if statement so any help would be appreciated

Answer

You just have to append the where clause in the same query. See below query.

IF NOT EXISTS (SELECT CharityID FROM CharityTbl WHERE [CharityID] = '@CharityID' or YEAR(TaxNoDateOfIssue) = YEAR(getdate())
begin
 raiserror ('Sorry Invalid Charity ID', 16,1)
 return 99 
end