linux 没有运算符与给定的名称和参数类型匹配,您可能需要添加显式

but5z9lq  于 2023-01-08  发布在  Linux
关注(0)|答案(1)|浏览(136)

我正在尝试使用bash命令行从postgresql DB获取值:尝试从表中运行select语句时出现问题。
例如,如果我执行这个select语句,它返回successful并给出值

psql -U postgres -d postgres -p 5432 -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase'"

然而,当我试图添加更多的where语句(硬编码或变量)到WHERE语句时,我得到了这个错误:

ERROR:  operator does not exist: character varying <> integer
LINE 1: ...questtype='CreatepostgresCase' and applicationerrorcode!=25 and a...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

脚本:

#!/bin/bash
errorCodeSuccess=0
errorCodeFailure=30
sampleDbUser=postgres
sampleDBPort=5432

appErrorCodeFailure=25

#hardcoded
psql -U postgres -d postgres -p 5432 -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase' and applicationerrorcode=25  and pxcreatedatetime>current_date"

#variables used 
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t -c "select count(*) from sampledata.sif_work where servicerequesttype='CreatepostgresCase' and applicationerrorcode!="${appErrorCodeFailure}" and applicationerrorcode!="${errorCodeSuccess}"  and pxcreatedatetime>current_date"

为什么即使我硬编码了这个值,它仍然抛出错误。为什么?

envsm3lx

envsm3lx1#

PostgreSQL将25理解为整型文字,但'25'将被解释为文本文字/字符串常量,对于character varying类型的列将是work
您可以在双引号"结束之前和结束之后添加单引号',但是您也根本不需要将双引号字符串-bash evaluates $ expressions in double quotes结束:

errorCodeSuccess=0
errorCodeFailure=30
sampleDbUser=postgres
sampleDBPort=5432

appErrorCodeFailure=25

#hardcoded
psql -U postgres -d postgres -p 5432 -t \
    -c "select count(*) 
        from   sampledata.sif_work 
        where  servicerequesttype='CreatepostgresCase' 
        and    applicationerrorcode='25'--single quotes indicate a text literal
        and    pxcreatedatetime>current_date"

#variables used 
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t \
    -c "select count(*)
        from   sampledata.sif_work
        where  servicerequesttype='CreatepostgresCase'
        and    applicationerrorcode!='${appErrorCodeFailure}'
        and    applicationerrorcode!='${errorCodeSuccess}'
        and    pxcreatedatetime>current_date; "

看一下servicerequesttype='CreatepostgresCase',您已经知道可以在双引号字符串中安全地使用单引号。
您还可以将单引号作为值的一部分:

#already doesn't work:
errorCodeSuccess=0
#same effect:
errorCodeSuccess='0'  
#this will be interpreted as a column named "0":
errorCodeSuccess='"0"' 
#"0" would be a valid name, but I doubt you have one or want one

#this will work:
errorCodeSuccess="'0'"
errorCodeFailure="'30'"
sampleDbUser=postgres
sampleDBPort=5432
psql -U "${sampleDbUser}" -d postgres -p "${sampleDBPort}" -t \
    -c "select count(*)
        from   sampledata.sif_work
        where  servicerequesttype='CreatepostgresCase'
        and    applicationerrorcode != ${appErrorCodeFailure}
        and    applicationerrorcode != ${errorCodeSuccess}
        and    pxcreatedatetime>current_date; "

请记住,以这种方式构造查询总是不安全的--无论是在安全性还是方便性方面。您可以从psql -v开始改进这一点。

相关问题