PostgreSQL:如何在'select'语句中给出整数值?

时间:2014-06-03 06:40:34

标签: postgresql

这里我需要使用dblink检查select语句中的条件。以下示例包含如下所示的更多详细信息:

示例:

 create or replace function dblink_fun(ID bigint) returns void as
 $$
 Begin
 perform dblink_connect('hostaddr=127.0.0.0 port=5432 dbname=db user=postgres password=***');

 With x AS ( Select      "EmpID","EmpNo","Slno","Edate"
        From tabletest c 
        Where c."EmpID" = ID
    )
, y AS 
( Select Row_Number() over ( Partition by "Slno" order by  "DateOfJoinig" Desc) AS Rnk,
"SName","Slno","DateOfJoining","TimeOfJoining","Address",Ranked."empID"
  from dblink('select
             "Slno"',
    (   case when "FirstName" is null then '' 
        else "FirstName" end || '' ||
        Case When "MiddleName" is null then '' 
        else "LastName" end || '' ||
    ) AS name ,
    (   Case When "Address1" is null then '' 
        else "Address1" end || '' || 
        Case When "Address2" is null then ''
        else "Address2" end || '' ||
    ) AS address,
    "JoinigDate", "TimeofJoin", "EmpID" From "remote_table"
    inner join "x" on x."Slno" = "Slno"
    Where x."Edate" >= (
 Case When "DateOfJoinig" is null then '1900-01-01' else "DateOfJoinig" end' /*error near 1900 */
 ) Ranked
 )

 perform dblink_disconnect();
 end;
 $$
 Language plpgsql;

错误:语法错误位于或接近“1900”

1 个答案:

答案 0 :(得分:1)

您的整个select语句都包含在单引号中:

from dblink('select ... 

但您在SQL本身中有单引号值。

您必须通过加倍嵌入单引号来逃避

只需将您选择的查询中的每个引号替换为两个单引号。

您的确切错误是由于碰巧1900落在嵌入式单引号之外的。