I have an INSERT query that works fine, but when I add an extra field I get the oh-so-helpful:
Invalid object name ‘optimizations’
Optimizations being the table name. The query is below… I am totally lost as to why this wouldn’t work. If I cut & paste the query into MSSQL Management Studio, the query works fine.
INSERT INTO [optimizations]
([opt_date],[opt_concept_id],[opt_pallet],[opt_turnable],
[opt_sideupok],[opt_endupok],[opt_flatok],[opt_notstickingout],
[opt_notinoverhang],[opt_itemsloaded],[opt_loadedweight],
[opt_pweightutil],[opt_pvolumeutil],[opt_request_xml],
[opt_response_xml],[opt_images],[opt_primary_stage_id])
VALUES
(GETDATE(),'775','20ft','true','false','false','true',
'true','true','10','5952.6','18.6','48.9',
'<xml><herpa><derpa>xml</derpa></herpa></xml>',
'<xml><herpa><derpa>xml</derpa></herpa></xml>',
'img1.jpg,img2.jpg,img3.jpg','98');
For some reason, if I include the last field in the query [opt_primary_stage_id] it does not work… if I omit this field it works fine. Interestingly enough if I add [opt_primary_stage_id] to the list of fields being passed, and pass it a value of NULL the query also works fine. The [opt_primary_stage_id] field’s data type is INT, I have tried including the last entry of the VALUES() section both as a straight number (3) and with quotes ('3')… neither works.
The server executing the code is running PHP 4.3.9, under IIS and with and MSSQL.
This is a version of the query, with the SAME data that will work.
INSERT INTO [optimizations]
([opt_date],[opt_concept_id],[opt_pallet],[opt_turnable],
[opt_sideupok],[opt_endupok],[opt_flatok],[opt_notstickingout],
[opt_notinoverhang],[opt_itemsloaded],[opt_loadedweight],
[opt_pweightutil],[opt_pvolumeutil],[opt_request_xml],
[opt_response_xml],[opt_images])
VALUES
(GETDATE(),'775','20ft','true','false','false','true','true',
'true','10','5952.6','18.6','48.9',
'<xml><herpa><derpa>xml</derpa></herpa></xml>',
'<xml><herpa><derpa>xml</derpa></herpa></xml>',
'img1.jpg,img2.jpg,img3.jpg');
EDIT: Fixed XML to have proper bracketing & added working query.
Adding a field won’t cause failure on it’s own: there is a valid reason.
You say it runs in SSMS OK then it shows that issue is in PHP or in the call.
First thoughts:
How long is the command. Is it being truncated somewhere in PHP? Is this dynamic SQL IRL?
Is the user context the same from the client? You may have the same table in 2 different schemas for example, with differing permissions.
Wrong database in the connection. What does SELECT DB_NAME() say? Sounds obvious but no object = not there. Don’t dismiss this idea and assume it is the correct context: check.