My php class creates the following command:
INSERT INTO contacts (firstName, lastName) VALUES ('jon', 'snow')
DECLARE @IDcontacts VARCHAR(200)
SET @IDcontacts = @@IDENTITY;
INSERT INTO emailAddresses (ownerId, emailAddress) VALUES (@IDcontacts, 'jon@thewall.com')
INSERT INTO emailAddresses (ownerId, emailAddress) VALUES (@IDcontacts, 'jon@winterfell.com')
INSERT INTO emailAddresses (ownerId, emailAddress) VALUES (@IDcontacts, 'thebastard@winterfell.com')
I create the connection, begin a transaction with, and execute the batch with sqlsrv_query()
.
After executing the batch, sqlsrv_errors()
gives me four errors: [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "@IDcontacts".
however, if I use exactly the same batch string in SQL Server Management Studio, it works fine and all four records are inserted with the correct identity value from the first insert.
I'm using SQL Server Express 2005, php version 5.4.3 and the most recent sqlsrv driver. I haven't been able to find anything about this on technet or the ms forums, and any help would be appreciated.