2016-06-20 2 views
0

SQL 프로 시저를 실행하고 PHP에서 값을 반환하려고합니다. 내 코드는 다음과 같습니다 :SQL Server 프로 시저가 PHP에서 출력을 반환하지 않습니다.

<?PHP 
include "conn.php"; 
    $myquery= "exec CR_Report @System='Automated Forms'"; 
    $fetched=sqlsrv_query($conn,$myquery); 
    if($fetched === false) { die(print_r(sqlsrv_errors(), true));} 
     while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC)) 
     { 
      echo $res['ID']; 
     } 
?> 

절차 :

CREATE procedure [dbo].[CR_Report] 
    @Department varchar(60) = Null, 
    @System varchar(30) = Null, 
    @Fromdate date = Null, 
    @Todate date = Null 
AS 
IF @System is Null and 
@Fromdate is Null and 
@Todate is Null 
BEGIN 
Create table #Temp_ConfigRequest_Department 
(
ID int, 
Form_No varchar(25), 
App_Date smalldatetime, 
System varchar(30) 
) 

delete #Temp_ConfigRequest_Department 

insert into #Temp_ConfigRequest_Department 

SELECT * FROM [SUAF].[dbo].[Config_Request] where Admin_Department = @Department 

select * from #Temp_ConfigRequest_Department 

DROP TABLE #Temp_ConfigRequest_Department 

end 
IF @Department is Null and 
@Fromdate is Null and 
@Todate is Null 
BEGIN 

Create table #Temp_ConfigRequest_System 
(
ID int, 
Form_No varchar(25), 
App_Date smalldatetime, 
System varchar(30) 
) 

delete #Temp_ConfigRequest_System 

insert into #Temp_ConfigRequest_System 

SELECT * FROM [SUAF].[dbo].[Config_Request] where System = @System 

select * from #Temp_ConfigRequest_System 

DROP TABLE #Temp_ConfigRequest_System 

end 
IF @Department is NULL and 
@System is NULL 
BEGIN 
Create table #Temp_ConfigRequest_Date 
(
ID int, 
Form_No varchar(25), 
App_Date smalldatetime, 
System varchar(30) 
) 

delete #Temp_ConfigRequest_Date 

insert into #Temp_ConfigRequest_Date 

SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate 

select * from #Temp_ConfigRequest_Date 

DROP TABLE #Temp_ConfigRequest_Date 

end 
IF @Department is not Null and 
    @System is not Null and 
    @Fromdate is not Null and 
    @Todate is not Null 
    BEGIN 
Create table #Temp_ConfigRequest_All 
(
ID int, 
Form_No varchar(25), 
App_Date smalldatetime, 
System varchar(30) 
) 

delete #Temp_ConfigRequest_All 

insert into #Temp_ConfigRequest_All 

SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate and Admin_Department = @Department and System = @System 

select * from #Temp_ConfigRequest_All 

DROP TABLE #Temp_ConfigRequest_All 

end 

그것은 어떤 가치와도 오류가 오류 로그에 기록되지 않습니다를 반환하지 않습니다. 데이터베이스에서 SQL 쿼리를 테스트하고 레코드를 반환합니다. 연결을 테스트하기 위해 동일한 파일에 다른 프로 시저에 대한 코드를 작성하고 값을 반환합니다. 두 번째 코드는 다음과 같습니다 :

<?PHP 
$myquery="exec Configuration_Request @IDtemp='4275'"; 
    $fetched=sqlsrv_query($conn,$myquery) ; 
    if($fetched === false) { die(print_r(sqlsrv_errors(), true));} 
     while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC)) 
     { 
      echo $res['Form_No']; 
      echo $res['Administrator']; 
     } 
?> 

절차 :

create procedure Configuration_Request (@IDtemp int) 
as 
begin 
select * from Config_Request where [email protected] 
end 

문제가 무엇입니까? 첫 번째 코드에서 값이 반환되지 않는 이유는 무엇입니까? 도움이나 제안을 부탁드립니다.

+0

첫 번째 저장 프로 시저의 쿼리가 'ID'라는 열을 반환합니까? –

+0

@SimonSellick 그렇습니다. :) – SR1092

+1

저는 PHP에 익숙하지 않았지만 저장된 proc 이름을 제외하면 볼 수있는 유일한 차이점은 conn.php가 두 번째 예제에 포함되어 있지 않다는 것입니다. (들여 쓰기는 실행에 영향을주는 것이 아니라 약간의 엇갈림으로 보입니다.) 아무도 더 좋은 제안이 없으면 두 저장 프로 시저의 소스를 게시 할 수 있습니까? –

답변

1

대단히 간단하게 할 수 있습니다. 목적이없는 임시 테이블을 작성 및 삭제하지 않기 때문에 동일한 작업을 수행하지만 더 빠릅니다.

CREATE procedure [dbo].[CR_Report] 
    @Department varchar(60) = Null, 
    @System varchar(30) = Null, 
    @Fromdate date = Null, 
    @Todate date = Null 
AS 

IF @System is Null 
    and @Fromdate is Null 
    and @Todate is Null 

    SELECT * FROM [SUAF].[dbo].[Config_Request] where Admin_Department = @Department 

IF @Department is Null 
    and @Fromdate is Null 
    and @Todate is Null 

    SELECT * FROM [SUAF].[dbo].[Config_Request] where System = @System 

IF @Department is NULL 
    and @System is NULL 

    SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate 

IF @Department is not Null 
    and @System is not Null 
    and @Fromdate is not Null 
    and @Todate is not Null 

    SELECT * FROM [SUAF].[dbo].[Config_Request] where convert(date, App_Date, 103) Between @Fromdate and @Todate and Admin_Department = @Department and System = @System 
+0

제가 프로 시저에서 제공 한 매개 변수가 잘못되었거나 무엇인지 알 수 없습니다! 그러나 이것은 완전히 트릭을했습니다. :) 정말 고마워요! temp 테이블은 데이터베이스에서 데이터를 검색하는 더 빠른 방법 이었지만 나는 틀렸다고 생각합니다. :) 감사합니다. – SR1092

+0

임시 테이블은 빠르지 만 효과적으로 데이터를 선택하여 임시 테이블을 삭제하기 만하면됩니다. 다행히 도움이되었습니다. –

관련 문제