/*
<Params>
pCompanyName=ftString
pStreet=ftString
pCity=ftString
pPostCode=ftString
pTelephone=ftString
pEmail=ftString
</Params>
*/

DECLARE CompanyID INTEGER;

SET CompanyID = COALESCE((SELECT top 1 CompanyCounter FROM Company WHERE (CompanyName = :pCompanyName) ),-1) ;



IF CompanyID = -1 THEN
INSERT INTO Company
(
  CompanyName,
  Address1,
  Town,
  PostCode,
  MainTelephone,
  EmailAddress
)
VALUES
(
:pCompanyName,
:pStreet,
:pCity,
:pPostCode,
:pTelephone,
:pEmail
);


SET CompanyID = (SELECT LASTAUTOINC FROM #Dummy);

END IF;

SELECT CompanyID FROM Company WHERE EmailAddress=:pEmail;