2016-09-09 55 views
-1

我有功能的腳本,我從SQL Server到Postgres的,現在轉換當我跑我得到一個錯誤如何從Postgres函數返回表格作爲表格?

ERROR: structure of query does not match function result type

我的函數獲得3個參數(siteid bigint, datefrom timestamp, dateto timestamp),並應返回我列入表中的功能在代碼中。我使用了「返回查詢」。

我執行我的功能是這樣的:

getrtbactivesiteplaces(1475, '2016-02-01', '2016-08-01') 

如何我能得到這個結果從我的功能表?

這是我的功能截圖

{ 
CREATE OR REPLACE FUNCTION "whis2011"."getrtbactivesiteplaces"(IN siteid int8, IN datefrom timestamp, IN dateto timestamp) RETURNS SETOF "varchar" 
AS $BODY$ 

DECLARE 
siteid BIGINT; 
datefrom timestamp without time zone; 
dateto timestamp without time zone; 


BEGIN 


-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 

    /* SQLWays Notice: SET TRANSACTION ISOLATION LEVEL READ COMMITTED must be called before procedure call */ 
-- SET TRANSACTION ISOLATION LEVEL READ COMMITTED 


    -- Insert statements for procedure here 
RETURN QUERY SELECT pl."Id", 
     pl."RtbActiveSiteId", 
     pl."AdPlaceId", 
     pl."AdPosition", 
     pl."Ctr", 
     pl."State", 
     pl."BidPrice", 
     pl."MinBidFloor", 
     pl."MinBidFloorCurrency", 
     pl."AverageCpm", 
     pl."AverageClickCost", 
     coalesce(SUM(ss."BidsCount"),0) AS BidsCount, 
     coalesce(SUM(ss."ShowsCount"),0) AS ShowsCount, 
     coalesce(SUM(ss."RealShowsCount"),0) AS RealShowsCount, 
     coalesce(SUM(ss."ClicksCount"),0) AS ClicksCount, 
     coalesce(SUM(ss."ClickLayerClicksCount"),0) as ClickLayerClicksCount, 
     coalesce(SUM(ss."ShowsCost"),0::money) AS ShowsCost, 
     coalesce(SUM(ss."ClicksCost"),0::money) AS ClicksCost, 
     coalesce(SUM(ss."BidsCost"),0::money) AS BidsCost, 
     coalesce(SUM(ss."SliderMovesCount"),0) AS SliderMovesCount 
    FROM "whis2011"."RtbActiveSitePlaces" pl 
    LEFT OUTER JOIN "whis2011"."RtbActiveSitePlaceStatistics" ss ON ss."RtbActiveSitePlaceId" = pl."Id" 
    WHERE ss."Date" >= datefrom AND ss."Date" < dateto AND pl."RtbActiveSiteId" = siteid 
    GROUP BY pl."Id", pl."RtbActiveSiteId", pl."AdPlaceId", pl."AdPosition", pl."Ctr", pl."State", pl."BidPrice", 
    pl."MinBidFloor", pl."MinBidFloorCurrency", pl."AverageCpm", pl."AverageClickCost"; 
END; 
$BODY$ 
LANGUAGE plpgsql 
COST 100 
CALLED ON NULL INPUT 
SECURITY INVOKER 
VOLATILE; 

} 
+1

如果你的問題是什麼圖形,通過各種手段包括屏幕截圖。如果是代碼,請不要向我們展示代碼圖片。在這裏發佈實際的代碼。 – Gerrat

+0

如果函數返回一個表,則需要使用'select * from getrtbactivesiteplaces(1475,'2016-02-01','2016-08-01')' –

+1

http://meta.stackoverflow.com/questions/ 285551/why-may-i-not-upload-images-code-on-so-when-asking-question/285557#285557 –

回答

1

你有兩個問題,你的代碼,我有兩個建議進行改進。

首先,您應該執行RETURNS TABLE ...而不是RETURNS SETOF varchar指定該函數返回的所有列。其次,你有三個函數參數,它們在功能塊中重新聲明的名字掩蓋了參數值。這就是爲什麼該函數不返回任何內容,因爲不使用參數值。

第三,我把所有的總和放在一個子查詢中,它更容易閱讀,也可能更高效。最後,由於函數體是一個單獨的SQL語句,因此您應該將其作爲一個SQL函數,它比PL/pgSQL語言函數更有效。

見下文。

CREATE OR REPLACE FUNCTION "whis2011"."getrtbactivesiteplaces" 
    (IN siteid int8, IN datefrom timestamp, IN dateto timestamp) 
RETURNS 
             
  
    SETOF "varchar" 
   TABLE (id int, RtbActiveSiteId int, ...) -- add all fields 
AS $BODY$ 

             
  
    DECLARE siteid BIGINT; datefrom timestamp without time zone; dateto timestamp without time zone; 
   -- don't redeclare parameters!!! 

             
  
    BEGIN 
   -- not needed for a SQL function 
    -- Insert statements for procedure here 
    
             
  
    RETURN QUERY 
   SELECT pl."Id", -- SQL function uses simple SELECT 
     pl."RtbActiveSiteId", 
     pl."AdPlaceId", 
     pl."AdPosition", 
     pl."Ctr", 
     pl."State", 
     pl."BidPrice", 
     pl."MinBidFloor", 
     pl."MinBidFloorCurrency", 
     pl."AverageCpm", 
     pl."AverageClickCost", 
     ss.* 
    FROM "whis2011"."RtbActiveSitePlaces" pl 
    LEFT JOIN (
     SELECT "RtbActiveSitePlaceId" AS "Id" 
       coalesce(SUM("BidsCount"),0) AS BidsCount, 
       coalesce(SUM("ShowsCount"),0) AS ShowsCount, 
       coalesce(SUM("RealShowsCount"),0) AS RealShowsCount, 
       coalesce(SUM("ClicksCount"),0) AS ClicksCount, 
       coalesce(SUM("ClickLayerClicksCount"),0) as ClickLayerClicksCount, 
       coalesce(SUM("ShowsCost"),0::money) AS ShowsCost, 
       coalesce(SUM("ClicksCost"),0::money) AS ClicksCost, 
       coalesce(SUM("BidsCost"),0::money) AS BidsCost, 
       coalesce(SUM("SliderMovesCount"),0) AS SliderMovesCount 
     FROM "whis2011"."RtbActiveSitePlaceStatistics" 
     WHERE "Date" >= datefrom AND "Date" < dateto 
     GROUP BY "RtbActiveSitePlaceId") ss USING ("Id") 
    WHERE pl."RtbActiveSiteId" = siteid; 

             
  
    END; 
   -- not needed for a SQL function 
$BODY$ LANGUAGE sql STRICT STABLE; -- don't call on NULL input and use STABLE

然後,您可以調用這個函數像這樣:

SELECT * FROM getrtbactivesiteplaces(1475, '2016-02-01', '2016-08-01'); 
+0

謝謝!它現在正在工作。 – Nikito

0

你的函數的返回類型爲setof varchar,但它應該是這樣的:

RETURNS TABLE (Id    integer, 
       RtbActiveSiteId integer, 
       ... etc ... 
      ) 
+0

它不起作用!實際上腳本的作品,但沒有回報(但應根據我的結果返回至少2行) – Nikito

相關問題