首先让我解释一下我正在尝试做什么,然后我是如何做的,然后是问题所在。 我正在尝试创建一个mssql查询来从数据库中读取发票。每张发票基本上是按发票ID组合在一起的各个费用的汇编。问题在于不同的收费可能来自不同的容器(收费与容器相关)。我需要做的是按发票ID对费用进行分组,并将发票中所有费用的容器名称连接起来,并使用“|”之类的分隔符。 我创建了一个临时表@t,它包含生成发票所需的所有费用和容器名称。在网上看了很长一段时间之后,我找到了STUFF命令,看起来像我想要的那样(出现,并且确实)。所以这是我的查询的SQL:
select invoice_id, [Total Paid] RefNumCount,PayCount, MIN([rowNumber]) as rowNumber, Vendor, Due, [Invoice Number], Company, MAX(Location) as Location,
SUM([Funded Amount]) as [Funded Amount],
[Company ID], [Invoice ID], provider_reference_number, reconcile_date, all_providers
, STUFF(
(
select '|' + t1.[Container Name]
from @t t1
where t1.[Invoice Number] = t.[Invoice Number]
order by t1.[Container Name]
for XML PATH(''), TYPE
).value('.', 'nvarchar(100)')
,1,1,''
) as [Container Name]
from @t t
group by invoice_id, [Total Paid], RefNumCount, PayCount, Vendor, Due, [Invoice Number], Company,
[Company ID], [Invoice ID], provider_reference_number, reconcile_date, all_providers
当我运行它是SQL Server Management Studio 2008时,它运行得很好。但是,当我在我的PHP应用程序中使用此代码时,查询不起作用,最终我通过尝试读取空结果集而得到致命错误。 有谁知道为什么会出现这个问题?或者,如果有另一种方法来连接字符串?
以下是用于调用查询的PHP代码段
try{
$q = cms_database::instance();
$result = $q->query($sql_statement);
if (!$result) {
throw new Exception("Database Error [{$q->errno}] {$q->error}");
}
$invoices = array();
while($row=$result->fetch_assoc()) {
$invoiceX = array();
$invoiceX["rowNum"] = $row["rowNumber"];
$invoiceX["Vendor"] = $row["Vendor"];
$invoiceX["Due"] = $row["Due"];
$invoiceX["Acct"] = $row["Container Name"];//$row["Account Number"];
$invoiceX["container_id"]=$row["container_id"];
$invoiceX["provider_id"] =$row["provider_id"];
$invoiceX["Invoice"] = $row["Invoice Number"];
$invoiceX["Location"] = $row["Company"];
$invoiceX["FundedAmt"] = $row["Funded Amount"];
$invoiceX["InvoiceID"] = $row["Invoice ID"];
$invoiceX["Notes"] = $row["Notes"];
//merge the notes with notes from payment history that matches the invoiceid
$invoices[] = $invoiceX;
}
//filter out invoices with all reference numbers accounted for and remaining to be paid is $0.00
$returnPackage["data"] = $invoices;
//print $result;
} catch (Exception $e){
$returnPackage["success"]=0;
$returnPackage["message"]=$e.message;
}
我得到的错误是:
“
致命错误:在 D <:>
“
第116行是代码行
while($row=$result->fetch_assoc()) {
答案 0 :(得分:1)
虽然这可能会晚一点,但是请尝试这个SQL调用。
select invoice_id, [Total Paid] RefNumCount,PayCount, MIN([rowNumber]) as rowNumber,
Vendor, Due, [Invoice Number], Company, MAX(Location) as Location,
SUM([Funded Amount]) as [Funded Amount], [Company ID], [Invoice ID],
provider_reference_number, reconcile_date, all_providers ,
STUFF((select '|' + t1.[Container Name]
from @t t1
where t1.[Invoice Number] = t.[Invoice Number]
order by t1.[Container Name]
for XML PATH('')),1,1,'') as [Container Name]
from @t t
group by invoice_id, [Total Paid], RefNumCount, PayCount, Vendor, Due, [Invoice Number],
Company, [Company ID], [Invoice ID], provider_reference_number, reconcile_date,
all_providers
添加“|”时出错在下面的select语句中将删除xml标记,当您使用.value()尝试提取数据时会导致错误。
STUFF((select '|' + ... from ... for XML PATH(''), TYPE).value('.', 'nvarchar(100)'),1,1,'')