I have a statement I’m using in PHP that will pull a number if it exists or, if it doesn’t, it will create a row, then pull the number from the created row.
The following code works but as the title says…not the second time. First let me clarify, it works everytime if the row exists. But when a row is created and the number returned, the next time through (again with new variables) will fail but then (using same new variables again) will work correctly the next time. This is true in both php and in sql environments.
SELECT id FROM table1 WHERE authID = 'DOEJON' AND periodID = 55
IF @@ROWCOUNT = 0 INSERT INTO table1 (authID, periodID) VALUES ('DOEJON', 55);
SELECT id FROM table1 WHERE timeID = SCOPE_IDENTITY()
there appears to be some sort of refresh related to the statement – how can I get the desired results every time? thoughts?
Thank you for your kind attention.
Try Below