postgres #my表语法错误

时间:2018-01-24 04:40:20

标签: postgresql

以下代码显示 postgres sql 中#my table seq附近的语法错误。

IF (p_BusinessID = '')
    THEN
        SELECT RANK() OVER(ORDER BY SSO) SNO,Business, SSO, DisplayName, UPPER(ExServerName) ExServerName, POP, IMAP, EWS, DisplayMonth, DisplayYear 
        FROM "POP_IMAP_EWS_Data"
        WHERE DisplayYear = COALESCE(p_YearID, DisplayYear) AND MonthID = COALESCE(p_MonthID, MonthID) AND SSO = COALESCE(p_SSO, SSO);
    ELSE
       CREATE SEQUENCE #myTable_seq;

       CREATE TABLE #myTable 
       (
                     ID                                      INT DEFAULT NEXTVAL ('#myTable_seq'),
                     Item                    VARCHAR(100), 
       );

       INSERT INTO #myTable(Item)
       SELECT RTRIM(LTRIM(Item)) FROM SplitString(;p_BusinessID, ',');

       SELECT RANK() OVER(ORDER BY SSO) SNO, A.BusinessID, Business, SSO, DisplayName, UPPER(ExServerName) ExServerName, POP, IMAP, EWS, DisplayMonth, DisplayYear 
       FROM "POP_IMAP_EWS_Data"A
       INNER JOIN #myTable B ON B.Item = A.BusinessID
       WHERE DisplayYear = COALESCE(p_YearID, DisplayYear) AND MonthID = COALESCE(p_MonthID, MonthID) AND SSO = COALESCE(p_SSO, SSO);

有人能说出原因吗?

1 个答案:

答案 0 :(得分:2)

使用#前缀表示临时表特定于SQL Server(可能还有Sybase)。 PostgreSQL希望你说create temporary table ...

create temporary table mytable (
    id serial primary key,
    item varchar(100)
);

另外,您通常只需将id列设为serial列,然后让PostgreSQL处理序列,而不是自己挂钩,所以我也改变了它;使用serial也可以设置序列的所有者。