Когда я выполняю в php эту процедуру
EXEC vehicle_summary_combined_proc @service_id=?,@startDate=?,@endDate=? by following code
$qry="EXEC vehicle_summary_combined_proc @service_id=?,@startDate=?,@endDate=?";
// $vehicleSummary[]=select_query($qry);
$params = array
(
array(&$vehid, SQLSRV_PARAM_IN),
array(&$assigned_from, SQLSRV_PARAM_IN),
array(&$assigned_to, SQLSRV_PARAM_IN),
);
$summary = select_query_proc($qry,$params);
function select_query_proc($query,$params)
{
$dblink=connecttodbmssql();
$options = array("Scrollable"=>"buffered");
$qry=sqlsrv_query($dblink,$query,$params,$options);// or die( $query . " ". mysql_error());
//print_r($qry);
if( $qry === false )
{
return [];
//die( print_r( sqlsrv_errors(), true));
}
$num=sqlsrv_num_rows($qry);
//print_r($num);
$num_field=sqlsrv_num_fields($qry);
//print_r($num_field);
$metaData =sqlsrv_field_metadata($qry);
//print_r($metaData);
$count=count($metaData);
for($i=0;$i<$count;$i++)
{
$fname[]=$metaData[$i]["Name"];
}
//print_r($fname);
for($i=0;$i<$num;$i++){
$result=sqlsrv_fetch_array($qry);
//print_r($result);
foreach($fname as $key => $value ) {
//print_r($result[$value]);
$arr[$i][$value]=$result[$value];
}
}
//print_r($arr);
return $arr;
// Close the connection.
sqlsrv_close($dblink);
}
Это дало мне следующие ошибки, и оно успешно работает в visual studio
Array
(
[0] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
)
[1] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_odometer'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_odometer'.
)
[2] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time1'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time1'.
)
[3] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'lat1'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'lat1'.
)
[4] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'long1'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'long1'.
)
[5] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time2'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time2'.
)
[6] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
)
[7] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'address1'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'address1'.
)
[8] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time1'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'time1'.
)
[9] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
)
[10] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
)
[11] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'i2'.
)
[12] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'gps_speed'.
)
[13] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_temperature'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_temperature'.
)
[14] => Array
(
[0] => 42S22
[SQLSTATE] => 42S22
[1] => 207
[code] => 207
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_temp2'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid column name 'tel_temp2'.
)
)
Мой код процедуры: -
ALTER procedure [dbo].[vehicle_summary_combined_proc]
@service_id nvarchar(20),@startDate nvarchar(100),@endDate nvarchar(100)
as
begin
--SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
--BEGIN TRAN
set nocount on
declare @tbl_tel varchar(20)
declare @offset integer
declare @tname varchar(20)
set @tname='tbl_temp_report'
--set @tbl_tel='tbl_telemetry_mar20'
--set @tname='temp_report_' +(left(DATENAME(MONTH,@endDate),3))
set @tbl_tel='tbl_telemetry_apr20'+(left(DATENAME(month,@endDate),3))+(right(DATENAME(year,@endDate),2))
set @offset=(select tz.utc_offset from tbl_users u inner join tbl_timezone tz
on u.timezone_id=tz.id inner join tbl_services s
on s.sys_user_id=u.id where s.id=@service_id)
set @startDate=convert(varchar(max),(DATEADD(minute,@offset,convert(datetime,@startDate))),121)
set @endDate=convert(varchar(max),(DATEADD(minute,@offset,convert(datetime,@endDate))),121)
create table #temp(
startdate date null,
[sys_service_id] int null,
start_location varchar(max) null,
end_location varchar(max) null,
[total_idle_time] nvarchar(100) null,
[total_running_time] nvarchar(100) null,
[total_halt_time] nvarchar(100) null,
[total_distance] nvarchar(100) null,
[max_idle] nvarchar(100) null,
[idle_latitude] float null,
[idle_longitude] float null,
[idle_address] varchar(max) null,
[idle_start_time] datetime null,
[idle_end_time] datetime null,
[max_halt] nvarchar(100) null,
[halt_latitude] float null,
[halt_longitude] float null,
[halt_address] varchar(max) null,
[halt_start_time] datetime null,
[halt_end_time] datetime null,
[avg_speed] float null,
[max_speed] float null,
start_lat float null,
start_long float null,
end_lat float null,
end_long float null,
[avg_temp] float null,
[min_temp] float null,
[max_temp] float null,
[avg_temp2] float null ,
[min_temp2] float null,
[max_temp2] float null,
[status] varchar(10))
truncate table #temp;
if convert(date,@startDate)=convert(date,GETDATE())
begin
set @startDate=convert(varchar(max),(DATEADD(minute,-@offset,@startDate)),121)
set @endDate=convert(varchar(max),(DATEADD(minute,-@offset,@endDate)),121)
insert into #temp
exec daily_console_report_latest @service_id,@startDate,@endDate
end
else
begin
declare @enddate1 varchar(max);
declare @startdate1 varchar(max);
if convert(date,@startDate)=convert(date,@endDate)
begin
set @endDate1=convert(varchar(max),(DATEADD(minute,-@offset,@endDate)),121)
end
else
begin
set @endDate1=convert(varchar(max),(DATEADD(minute,-@offset,convert(varchar(max),(convert(date,@startDate)))+' 23:59:00')),121)
end
set @startDate=convert(varchar(max),(DATEADD(minute,-@offset,@startDate)),121)
--print @startDate +' ' +@endDate1
begin try
insert into #temp
exec daily_console_report_monthly @service_id,@startDate,@endDate1
end try
begin catch
end catch
update #temp set startdate=CONVERT(date,@endDate1) where startdate is null
set @startDate=convert(varchar(max),(DATEADD(minute,@offset,@startDate)),121)
declare @diff int;
set @diff = DATEDIFF(day,@startDate,@endDate) -1 ;
--print @diff
while(@diff >0)
begin
insert into #temp
select top 1 startdate, sys_service_id,start_address,end_address,total_idle_time,total_running_time,total_halt_time,
total_distance,max_idle,idle_latitude,idle_longitude,idle_address,idle_start_time,idle_end_time,max_halt,halt_latitude,
halt_longitude,halt_address,halt_start_time,halt_end_time,avg_speed,max_speed,null as start_lat,null as start_long,
null as end_lat,null as end_long,avg_temp,min_temp,max_temp,avg_temp2,min_temp2,max_temp2,
status from tbl_vehicle_summary where sys_service_id=@service_id and startdate=convert(date,DATEADD(DAY,@diff,@startDate))
set @diff = @diff -1;
end
set @startdate1=convert(varchar(max),(DATEADD(minute,-@offset,convert(varchar(max),(convert(date,@endDate)))+' 00:00:00')),121)
if convert(date,@endDate)>=convert(date,GETDATE())
begin
set @endDate=convert(varchar(max),(DATEADD(minute,-@offset,convert(datetime,@endDate))),121)
insert into #temp
exec daily_console_report_latest @service_id,@startdate1,@endDate
end
else
begin
set @endDate=convert(varchar(max),(DATEADD(minute,-@offset,convert(datetime,@endDate))),121)
if @endDate1 <> @endDate
begin
--print 'hi'
insert into #temp
exec daily_console_report_monthly @service_id,@startDate1,@endDate
end
update #temp set startdate=CONVERT(date,@endDate) where startdate is null
end
end
declare @total_idle int,@total_running int,@total_halt int,@total_distance float,@max_idle varchar(max),
@max_halt varchar(max),@avg_Speed float,@max_speed float;
set @total_idle = (select sum(dbo.to_minutes(total_idle_time)) from #temp where total_idle_time is not null);
set @total_running = (select sum(dbo.to_minutes(total_running_time)) from #temp where total_running_time is not null);
set @total_halt = (select sum(dbo.to_minutes(total_halt_time)) from #temp where total_halt_time is not null);
set @total_distance = (select sum(convert(float,total_distance)) from #temp where total_distance is not null);
set @max_idle = (select max(dbo.to_minutes(max_idle)) from #temp where max_idle is not null);
set @max_halt = (select max(dbo.to_minutes(max_halt)) from #temp where max_halt is not null);
set @avg_Speed = (select AVG(avg_speed) from #temp where avg_speed is not null);
set @max_speed = (select max(max_speed) from #temp);
if @total_distance = 0
begin
set @max_speed = 0
end
insert into #temp values(null,
@service_id,null,null,
dbo.to_hour(@total_idle),
dbo.to_hour(@total_running),
dbo.to_hour(@total_halt),
convert(varchar(max),@total_distance),
dbo.to_hour(@max_idle),
(select top 1 idle_latitude from #temp where max_idle=dbo.to_hour(@max_idle)),
(select top 1 idle_longitude from #temp where max_idle=dbo.to_hour(@max_idle)),
(select top 1 idle_address from #temp where max_idle=dbo.to_hour(@max_idle)),
(select top 1 idle_start_time from #temp where max_idle=dbo.to_hour(@max_idle)),
(select top 1 idle_end_time from #temp where max_idle=dbo.to_hour(@max_idle)),
dbo.to_hour(@max_halt),
(select top 1 halt_latitude from #temp where max_halt=dbo.to_hour(@max_halt)),
(select top 1 halt_longitude from #temp where max_halt=dbo.to_hour(@max_halt)),
(select top 1 halt_address from #temp where max_halt=dbo.to_hour(@max_halt)),
(select top 1 halt_start_time from #temp where max_halt=dbo.to_hour(@max_halt)),
(select top 1 halt_end_time from #temp where max_halt=dbo.to_hour(@max_halt)),
@avg_Speed,@max_speed,(select top 1 start_lat from #temp),
(select top 1 start_long from #temp),
(select top 1 end_lat from #temp),
(select top 1 end_long from #temp),
null,null,null,null,null,null,null)
select * from #temp order by startdate
--COMMIT TRANSACTION
end
** Выше приведен мой код хранимой процедуры, который он успешно выполняет в Visual Studio. Не знаю, почему возникла эта проблема, пока я нажимаю на PHP Функции sqlsrv, пожалуйста, помогите мне решить эту проблему. была отлажена процедура несколько раз, но не удалось отследить единственную ошибку, почему она застряла при выполнении той же процедуры с помощью php