問題描述
我正在將一些舊的 PHP 頁面轉(zhuǎn)換為使用 PDO.
下面是兩個簡化的查詢(不是我的實際查詢),以幫助理解我遇到的問題...
SELECT afield INTO #temptable FROM atable WHERE anotherfield = 'somevalue';SELECT afield,anotherfield,onemorefield FROM atableWHERE afield NOT IN (SELECT * FROM #temptable);
上面的查詢拋出標題中描述的錯誤(更完整的是它拋出致命錯誤:未捕獲異常'PDOException',消息為'SQLSTATE[IMSSP]:查詢的活動結果不包含字段.'")>
如果我像這樣更改查詢...
with (SELECT afield INTO #temptable FROM atableWHERE anotherfield = 'somevalue') AS temptable;SELECT afield,anotherfield,onemorefield FROM atablewhere afield NOT IN (SELECT * FROM temptable);
這似乎解決了錯誤,但這個版本的查詢效率低得可怕,因為它似乎對另一個查詢中的每個字段比較運行臨時查詢.
有沒有辦法讓第一個表單(創(chuàng)建一次臨時表)與 PDO 一起工作?
它在使用 mssql 的舊頁面上運行良好.
我知道我可以通過創(chuàng)建一個真實的表以凌亂"的方式做到這一點,在 php 中運行它,然后運行第二個查詢(在單獨的 php 調(diào)用中),然后運行第三個查詢以刪除第一個表.但我寧愿不必訴諸那個!:)
如果您使用的是存儲過程,請使用
SET NOCOUNT ON
問題是存儲過程返回的結果包含受影響的行數(shù)作為第一個結果.
微軟文檔
I am in the process of converting some old PHP pages to use PDO.
Below are two simplified queries (not my actual queries) to aid understanding of the problem I'm having...
SELECT afield INTO #temptable FROM atable WHERE anotherfield = 'somevalue';
SELECT afield,anotherfield,onemorefield FROM atable
WHERE afield NOT IN (SELECT * FROM #temptable);
The above query throws the error described in the title (more completely it throws "Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: The active result for the query contains no fields.'")
If I alter the query like this...
with (SELECT afield INTO #temptable FROM atable
WHERE anotherfield = 'somevalue') AS temptable;
SELECT afield,anotherfield,onemorefield FROM atable
where afield NOT IN (SELECT * FROM temptable);
This seems to get around the error, but this version of the query is horribly horribly inefficient because it appears to run the temptable query for every single field comparison in the other query.
Is there a way to make the first form (which creates a temporary table once) work with PDO?
It worked fine on the old page which used mssql.
EDIT: I know I can probably do this in a 'messy' way by creating a real table, run it in php, then run the second query (in a separate php call) , then run a third query to drop the first table. But I'd rather not have to resort to that! :)
If you are using a stored procedure then use
SET NOCOUNT ON
The problem is that the stored procedure returns a result containing the number of rows affected as the first result.
Microsoft Documentation
這篇關于“活動結果不包含任何字段"在 MS SQL 中使用 PDO的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!