我在 MS SQL Management Studio 中为 SQL Server 2012 创建了一个查询,该查询使用 common_table_expression 自加入表。它在 MS SQL Studio 中正常工作,但在 Codeigniter 中没有返回结果或错误。
查询:
WITH rows AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY u.[PatientID], u.[CreateDate]) AS rn
FROM (
SELECT a.[PatientID], a.[BGValue], a.[CreateDate], a.[HospitalUnit], 'MSHA' as 'Hospital', h.[system_name], f.[facility_code], f.[facility_name], 'IV' as 'Treatment'
FROM [Analytics].[dbo].[MSHA_IVTreatment] a
JOIN [Analytics].[dbo].[MSHA_Patients] p
ON p.[PatientID] = a.[PatientID]
JOIN [Analytics].[dbo].[Facilities] f
ON f.[facility_code] = p.[facility_code]
JOIN [Analytics].[dbo].[Hospitals] h
ON f.[hospital] = h.[hospital]
WHERE a.[CreateDate] BETWEEN '2013-05-01' AND '2013-05-31'
UNION ALL
SELECT a.[PatientID], a.[BGValue], a.[CreateDate], a.[HospitalUnit], 'MSHA' as 'Hospital', h.[system_name], f.[facility_code], f.[facility_name], 'SubQ' as 'Treatment'
FROM [Analytics].[dbo].[MSHA_SubQTreatment] a
JOIN [Analytics].[dbo].[MSHA_Patients] p
ON p.[PatientID] = a.[PatientID]
JOIN [Analytics].[dbo].[Facilities] f
ON f.[facility_code] = p.[facility_code]
JOIN [Analytics].[dbo].[Hospitals] h
ON f.[hospital] = h.[hospital]
WHERE a.[CreateDate] BETWEEN '2013-05-01' AND '2013-05-31'
) u
)
SELECT mc.[PatientID], mc.[BGValue], mc.[CreateDate], mc.[Hospital], mc.[system_name] as 'System', mc.[facility_code], mc.[facility_name], mc.[HospitalUnit], DATEDIFF(second, mp.[CreateDate], mc.[CreateDate])/60 as 'Interval', mc.[Treatment]
FROM rows mc
JOIN rows mp
ON mc.rn = mp.rn + 1 and mc.[PatientID] = mp.[PatientID]
ORDER BY mc.[CreateDate] ASC;
当我将其放入变量并尝试在 codeigniter 中检索结果时,没有返回任何内容:
$result = $this->db->query($query);
CI_DB_sqlsrv_result Object
(
[conn_id] => Resource id #30
[result_id] => Resource id #41
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] => -1
[row_data] =>
)
我曾尝试将查询放入事务中,但得到了相同的结果。
任何见解都非常感谢。