可以在Redshift中运行一个使用变量而不使用EXECUTE语句的查询吗?

huangapple go评论53阅读模式
英文:

Is it possible to run a query in Redshift that uses a variable without EXECUTE statement?

问题

I'm just curious. I stumbled upon this code and wanted to get rid of EXECUTE. It worked for the DROP statement, but I couldn't figure out how to do it for the CREATE because it's using data saved to a variable.

BEGIN
SELECT
	INTO row '''' + LISTAGG(DISTINCT key, ''', ''') + '''' as keys
FROM
	some_table;

EXECUTE 'DROP TABLE IF EXISTS some_table_pivot;';

EXECUTE 'CREATE TABLE DROP TABLE IF EXISTS some_table_pivot;
 AS SELECT * FROM (SELECT account_id, key, value FROM DROP TABLE IF EXISTS some_table) PIVOT (MAX(lower(value)) FOR "key" in (' || row.keys || '));';

Does anyone know if it's possible?

英文:

I'm just curious. I stumbled upon this code and wanted to get rid of EXECUTE. It worked for the DROP statement, but i couldn't figure out how do it for the CREATE because it's using data saved to a variable.

BEGIN
SELECT
	INTO row '''' + LISTAGG(DISTINCT key, ''', ''') + '''' as keys
FROM
	some_table;

EXECUTE 'DROP TABLE IF EXISTS some_table_pivot;';

EXECUTE 'CREATE TABLE DROP TABLE IF EXISTS some_table_pivot;
 AS SELECT * FROM (SELECT account_id, key, value FROM DROP TABLE IF EXISTS some_table) PIVOT (MAX(lower(value)) FOR "key" in (' || row.keys || '));';

does anyone know if it's possible?

答案1

得分: 1

这似乎是存储过程内部的代码,对吗?在存储过程内部运行动态SQL的唯一方法是使用EXECUTE。

您可以使用一些外部支持代码将创建的SQL发送到Redshift。这可以是Lambda、shell脚本或任何可以通过JDBC/ODBC或API连接的工具。所以几乎可以使用任何工具。

英文:

This looks to be the code inside a stored procedure, no? An EXECUTE inside a stored procedure is the only way to run dynamic SQL internally to Redshift.

You could use some external support code to issue created SQL to Redshift. This could be a Lambda, shell scripts, or any tool that can connect via JDBC/ODBC or API. So just about anything.

huangapple
  • 本文由 发表于 2023年2月24日 02:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定