简体   繁体   English

timestamp()是否不接受函数参数作为输入?

[英]timestamp() do not take function parameter as input?

I am using sql in postgresql 我在PostgreSQL中使用SQL

cast(col as timestamp($1))

It gives me error around parameter 它给我参数周围的错误

The sql works when I give the number directly: 当我直接给出数字时,sql起作用:

cast(col as timestamp(1))

So timestamp do not take function parameter? 那么时间戳不带函数参数吗?

The precision modifier of the timestamp type is part of the type name and cannot be parameterized in plain SQL. timestamp类型精度修饰符是类型名称的一部分,不能在普通SQL中进行参数化。 That's not a function, even though the syntax with parentheses looks the same. 这不是一个函数,即使带有括号的语法看起来一样。

You would need dynamic SQL for that. 为此,您将需要动态SQL。 In a PL/pgSQL function build the statement as string and run it with EXECUTE . 在PL / pgSQL函数中,将语句构建为字符串,然后使用EXECUTE运行它。 Something like: 就像是:

EXECUTE 'SELECT col::timestamp(' || $1 || ')'
INTO my_var;

$1 being type integer . $1integer类型。 Not a string type like text , or you have a possible SQL injection hole. 不是类似于text的字符串类型,否则您可能有SQL注入孔。

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

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