简体   繁体   English

将参数传递到psql命令时出错

[英]Errors while passing parameters into a psql command

I'm trying to connect to a Redshift cluster and run a SQL script with parameters, but am getting errors. 我正在尝试连接到Redshift集群并使用参数运行SQL脚本,但是却出现错误。

Here's the Bash command: 这是Bash命令:

psql -v schema="$REDSHIFT_SCHEMA" -h "$REDSHIFT_HOSTNAME" -d "$REDSHIFT_DATABASE" -U "$REDSHIFT_USERNAME" -p "$REDSHIFT_PORT" -f "nulltest.sql" -v inputStartDateId=20170318 -v inputEndDateId=20170404

And the contents of nulltest.sql is: 而且nulltest.sql的内容是:

select 
    COALESCE( $inputStartDateId, min(StartDateID)),
    COALESCE( $inputEndDateId, max(EndDateID))
from (
    select 
        row_number()over(order by FiscalWeekID desc) as rownum,
        FiscalWeekID,
        min(dateid)StartDateID
        ,max(dateid)EndDateID 
    from 
        dm.dim_Time
    where DateKey < getdate()
    group by FiscalWeekID
    ) a
where rownum <= 3

The error I'm getting is: 我得到的错误是:

psql:nulltest.sql:15: ERROR:  syntax error at or near "$"
LINE 2:  COALESCE( $inputStartDateId, max(StartDateID))

Am I calling the parameter incorrectly in the SQL script? 我在SQL脚本中错误地调用了参数吗? The SQL runs perfectly fine through Aginity when I pass values into the parameter at run time. 当我在运行时将值传递给参数时,SQL通过Aginity运行得非常好。 I'm not sure what's supposed to be different when calling the SQL through the psql command though 我不确定通过psql命令调用SQL时应该有什么不同

You're passing your parameters correctly on the command line, but your nulltest.sql file should use :inputStartDateId or :'inputStartDateId' instead of $inputStartDateId . 您在命令行上正确传递了参数,但您的nulltest.sql文件应使用:inputStartDateId:'inputStartDateId'而不是$inputStartDateId Previous answer: https://stackoverflow.com/a/30850125/6650678 . 先前的答案: https : //stackoverflow.com/a/30850125/6650678

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM