In PHP I'm trying to insert multiple rows into two separate tables. Because these all need to succeed, I'm starting with:
mssql_query("BEGIN TRAN");
Then I run through the insert into the main table:
$insert_Proposal = 'INSERT INTO PROPOSALS( ';
$insert_Proposal .= 'ApprovedByDev, ';
$insert_Proposal .= 'EstFundingEndFiscalYear, ';
$insert_Proposal .= 'EstFundingStartDate, ';
$insert_Proposal .= 'ProjectDesc, ';
$insert_Proposal .= 'ProjectType, ';
$insert_Proposal .= 'ProposalComments ';
$insert_Proposal .= ') VALUES ( ';
$insert_Proposal .= sqlSafeVars($_POST['ApprovedByDev'], "varchar") .', ';
$insert_Proposal .= sqlSafeVars($_POST['EstFundingEndFiscalYear'], "bigint") .', ';
$insert_Proposal .= sqlSafeVars($_POST['EstFundingStartDate'], "varchar") .', ';
$insert_Proposal .= sqlSafeVars($_POST['ProjectDesc'], "varchar") .', ';
$insert_Proposal .= sqlSafeVars($_POST['ProjectType'], "bigint") .', ';
$insert_Proposal .= sqlSafeVars($_POST['ProposalComments'], "varchar") .' ';
$insert_Proposal .= ')';
What I need is to get the autonumbered ProposalID that will be created by this so I can use it to insert records into the other table, before I issue the COMMIT
I've found suggestions to use something like:
output inserted.pk
or
INSERT INTO Persons (FirstName) VALUES ('Joe');
SELECT ID AS LastID FROM Persons WHERE ID = @@Identity;
But these don't seem to work if you aren't committing the initial transaction. Thanks in advance for any help.