I've thought it out logically, but I need some syntax help since I am still a little feeble with t-sql, and debugging in mssql still mystifies me a bit. Once I get this working, I figured I'd schedule it as a once a month job or something.
--What needs to be done in plain English:
------------------------------------------
--SUMMARY: a branch that hasn't had placement associated with it for 1 year needs to be set to inactive.
--PROBLEM STATEMENT:
--for each branch whose most recent placement associated with it has an 'ExpirationDate' of longer than one year ago,
--set the 'Active' attribute of that particular branch to 'false'
--NOTE: One branch can have many placements.
Gurus, I request your insight, mostly with a good loop construction in tsql - i think i can handle the UDF parts - I know there's no FOR...EACH..NEXT in tsql, right? Any input you can give would be appreciated
--What needs to be done in plain English:
------------------------------------------
--SUMMARY: a branch that hasn't had placement associated with it for 1 year needs to be set to inactive.
--PROBLEM STATEMENT:
--for each branch whose most recent placement associated with it has an 'ExpirationDate' of longer than one year ago,
--set the 'Active' attribute of that particular branch to 'false'
--NOTE: One branch can have many placements.
Code:
--Refine #1 at Pseudo-TSQL:
---------------------------------------------------
--FOR EACH CompBranchID in this recordset:
--SELECT COMPANY.CompanyTypeID, COMPANY_BRANCH.CompBranchID
--FROM COMPANY_BRANCH
--INNER JOIN COMPANY ON COMPANY_BRANCH.CompanyID = COMPANY.CompanyID
--WHERE (COMPANY.CompanyTypeID = 2)
--IF IsItStagnant(@CompBranchID)=True THEN
--UPDATE COMPANY_BRANCH
--SET Active = FALSE
--WHERE (CompBranchID = @ReturnedProducerBranchID
--NEXT
--UDF IsItStagnant(@CompBranchID) RETURNS True/False
--IF GetExpirationDate(@CompBranchID) < GETDATE()-1Year THEN
--RETURN True
--ELSE
--RETURN False
--UDF GetExpirationDate(@CompBranchID) RETURNS ExpirationDate
--SELECT TOP (1) ProducerBranchID, ExpirationDate
--FROM PLACEMENT
--WHERE (ProducerBranchID = @CompBranchID
--ORDER BY ExpirationDate DESC
--RETURN ExpirationDate
Gurus, I request your insight, mostly with a good loop construction in tsql - i think i can handle the UDF parts - I know there's no FOR...EACH..NEXT in tsql, right? Any input you can give would be appreciated