为什么目标表在选择插入过程中不被识别为表格?

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

Why is the target table not recognized as table in select insert into procedure?

问题

我们正在从表TBL_bkmvdata_131_B110构建表TBL_bkmvdata_131_B110_L0,使用select insert into,但目标表不被识别为表。

我们得到了以下错误消息:

错误: "tbl_bkmvdata_131_b110_l0" 不是已知变量
 15: INTO  TBL_bkmvdata_131_B110_L0
               ^
SQL 状态: 42601
字符: 645

只是为了确保其他一切都正常... 如果我们取消注释declare TBL_bkmvdata_131_B110_L0 varchar;

我们会得到:

CREATE PROCEDURE

查询在 43 毫秒内成功返回。
英文:

We are building a table TBL_bkmvdata_131_B110_L0 from table TBL_bkmvdata_131_B110 using the select insert into but the target table is not recognized as table.

create or replace procedure gla_first_step()
language plpgsql
as $$
-- declare TBL_bkmvdata_131_B110_L0 varchar;
begin

select *,
CASE WHEN (B110TrialBalanceCodeText1406 LIKE N'%DATA%' and B110TrialBalanceCodeText1406 not LIKE N'%DATA%'
										and B110TrialBalanceCodeText1406 not LIKE N'%DATA%') OR
		  (B110TrialBalanceCodeText1406 LIKE N'%DATA%' and B110TrialBalanceCodeText1406 not like  N'%DATA%') OR
		  B110TrialBalanceCodeText1406 LIKE N'%DATA%' OR
		  B110TrialBalanceCodeText1406 LIKE '%EXPENSE%' OR
		  B110TrialBalanceCodeText1406 LIKE '%INCOME%' 
		  THEN 'P' else 'B' end as accountType
INTO  TBL_bkmvdata_131_B110_L0
from  TBL_bkmvdata_131_B110;

end;
$$

We get:

ERROR:  "tbl_bkmvdata_131_b110_l0" is not a known variable
LINE 15: INTO  TBL_bkmvdata_131_B110_L0
               ^
SQL state: 42601
Character: 645

Just to make sure everything else works.. if we release the comment from
declare TBL_bkmvdata_131_B110_L0 varchar;

We get

CREATE PROCEDURE

Query returned successfully in 43 msec.

答案1

得分: 0

根据文档 43.5.3. 使用单行结果执行命令:

>SELECT select_expressions INTO [STRICT] target FROM ...;

这意味着您需要使用 CREATE TABLE ... AS SELECT 从旧表中构建一个新表。当您取消注释 -- declare TBL_bkmvdata_131_B110_L0 varchar; 时,使用了 plpgsql 形式的 SELECT INTO,并且将查询的结果分配给了 TBL_bkmvdata_131_B110_L0

英文:

Per docs 43.5.3. Executing a Command with a Single-Row Result:

>SELECT select_expressions INTO [STRICT] target FROM ...;

<...>

>Tip

>Note that this interpretation of SELECT with INTO is quite different from PostgreSQL's regular SELECT INTO command, wherein the INTO target is a newly created table. If you want to create a table from a SELECT result inside a PL/pgSQL function, use the syntax CREATE TABLE ... AS SELECT.

This means you will need to use CREATE TABLE ... AS SELECT to build a new table from an old in the function. When you uncommented -- declare TBL_bkmvdata_131_B110_L0 varchar; then the plpgsql form of SELECT INTO was used and the result of the query was assigned to TBL_bkmvdata_131_B110_L0.

huangapple
  • 本文由 发表于 2023年5月29日 23:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358411.html
匿名

发表评论

匿名网友

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

确定