2017-09-20 12 views
0

ローカル変数とループを含むクエリを実行しようとしています。 しかし、Fusio/Doctrineは実行時に空の/エラーを返します。Doctrine - Fusioで宣言とループを使ってsqlクエリを実行

例(空の結果またはブール):

$sum = $connection->fetchAll("declare @dateFrom DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))); 
declare @dateTo DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE())); 
declare @dateFromtmp [email protected]; 

declare @tmpDate table(Dates DATETIME); 

WHILE (@dateFromtmp <= @dateTo) 
BEGIN 
    insert into @tmpDate values(@dateFromtmp) 
    SET @dateFromtmp = DATEADD(DAY,1,@dateFromtmp) 
END; 

SELECT @dateFrom; 
"); 
$type = "is " . gettype($sum); 
$count = count($sum); 
return $response->build(200, [], [ 
    'type'=>$type, 
    'count'=>$count, 
    'summary' => $sum, 
]); 

戻り値:

{ 
    "type": "is array", 
    "count": 0, 
    "summary": [] 
} 

クエリの下に実行する問題がない。

$sum = $connection->fetchColumn("declare @dateFrom DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))); 
declare @dateTo DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE())); 
declare @dateFromtmp [email protected]; 

declare @tmpDate table(Dates DATETIME); 

SELECT @dateFrom; 
"); 
$type = "is " . gettype($sum); 
$count = count($sum); 
return $response->build(200, [], [ 
    'type'=>$type, 
    'count'=>$count, 
    'summary' => $sum, 
]); 

リターン:

{ 
    "type": "is string", 
    "count": 1, 
    "summary": "2017-08-01 00:00:00.000" 
} 

これはループに関連していると思われます。 Fusioでこのクエリを実行する方法はありますか?

おかげ

答えて

0

答えここで見つける:alternative solutions

$sum = $connection->fetchAll("declare @dateFrom DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))); 
declare @dateTo DATETIME=CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE())); 

WITH DateTable 
AS 
(
    SELECT CONVERT(date,@dateFrom) AS [DATE] 
    UNION ALL 
    SELECT CONVERT(date,DATEADD(dd, 1, [DATE])) 
    FROM DateTable 
    WHERE DATEADD(dd, 1, [DATE]) < @dateTo 
) 
SELECT dt.[DATE] 
FROM [DateTable] dt; 
"); 

$type = "is " . gettype($sum); 
$count = count($sum); 
return $response->build(200, [], [ 
    'type'=>$type, 
    'count'=>$count, 
    'summary' => $sum, 
]); 

リターン:

{ 
    "type": "is array", 
    "count": 30, 
    "summary": [ 
     { 
      "DATE": "2017-08-01" 
     }, 
     { 
      "DATE": "2017-08-02" 
     }, 
     { 
      "DATE": "2017-08-03" 
     }, 
     { 
      "DATE": "2017-08-04" 
     }, 
     { 
      "DATE": "2017-08-05" 
     }, 
     { 
      "DATE": "2017-08-06" 
     }, 
     { 
      "DATE": "2017-08-07" 
     }, 
     { 
      "DATE": "2017-08-08" 
     }, 
     { 
      "DATE": "2017-08-09" 
     }, 
     { 
      "DATE": "2017-08-10" 
     }, 
     { 
      "DATE": "2017-08-11" 
     }, 
     { 
      "DATE": "2017-08-12" 
     }, 
     { 
      "DATE": "2017-08-13" 
     }, 
     { 
      "DATE": "2017-08-14" 
     }, 
     { 
      "DATE": "2017-08-15" 
     }, 
     { 
      "DATE": "2017-08-16" 
     }, 
     { 
      "DATE": "2017-08-17" 
     }, 
     { 
      "DATE": "2017-08-18" 
     }, 
     { 
      "DATE": "2017-08-19" 
     }, 
     { 
      "DATE": "2017-08-20" 
     }, 
     { 
      "DATE": "2017-08-21" 
     }, 
     { 
      "DATE": "2017-08-22" 
     }, 
     { 
      "DATE": "2017-08-23" 
     }, 
     { 
      "DATE": "2017-08-24" 
     }, 
     { 
      "DATE": "2017-08-25" 
     }, 
     { 
      "DATE": "2017-08-26" 
     }, 
     { 
      "DATE": "2017-08-27" 
     }, 
     { 
      "DATE": "2017-08-28" 
     }, 
     { 
      "DATE": "2017-08-29" 
     }, 
     { 
      "DATE": "2017-08-30" 
     } 
    ] 
} 
関連する問題