如何在 Postgres 9.3 中 +=
how to += in Postgres 9.3
我在重新分配一个值时遇到了麻烦,我的老师告诉我使用类似
var = var +(查询的其余部分)
但它根本不起作用,这是我的代码:
create or replace function recorrertablas()
returns void as
$BODY$
declare cursorx cursor for select id, nombre as nombres from tablas;
declare cursory refcursor;
declare rec record;
declare rec2 record;
declare consulta varchar;
--declare consulta2 varchar;
begin
open cursorx;
loop
fetch cursorx into rec;
exit when not found;
consulta= 'create table ' || rec.nombres;
--consulta2= '';
open cursory for select * from atributos where idtabla = rec.id;
loop
fetch cursory into rec2;
exit when not found;
consulta = consulta + '(' || rec2.id || ', ' || rec2.idtabla || ', ' || rec2.nombre || ', ' ||rec2.tipodedatos ;
if rec2.claveprimaria = 1 then
consulta= consulta +', constraint pk_ ' || rec.nombres || '( ' || rec.id || '));';
else
consulta = consulta +');';
end if;
end loop;
close cursory;
end loop;
execute consulta;
close cursorx;
end;
$BODY$
language plpgsql volatile;
如果您需要连接事物,请使用 ||
运算符或 concat()
函数,如下所示。
您一直在使用此运算符,但出于某种原因将其与 +
混合使用。
SELECT 'string' || 'part2';
或
SELECT concat('string', 'part2');
针对您的具体情况:
consulta = consulta || '(' || rec2.id || ', ' || rec2.idtabla || ', ' || rec2.nombre || ', ' ||rec2.tipodedatos ;
if rec2.claveprimaria = 1 then
consulta= consulta || ', constraint pk_ ' || rec.nombres || '( ' || rec.id || '));';
else
consulta = consulta || ');';
当使用 ||
运算符时,如果任何值的计算结果为 NULL
,则整个输出将为 NULL
,但是当使用 concat()
函数时,它将省略 NULL
值并仍然连接其他参数。
我在重新分配一个值时遇到了麻烦,我的老师告诉我使用类似 var = var +(查询的其余部分) 但它根本不起作用,这是我的代码:
create or replace function recorrertablas()
returns void as
$BODY$
declare cursorx cursor for select id, nombre as nombres from tablas;
declare cursory refcursor;
declare rec record;
declare rec2 record;
declare consulta varchar;
--declare consulta2 varchar;
begin
open cursorx;
loop
fetch cursorx into rec;
exit when not found;
consulta= 'create table ' || rec.nombres;
--consulta2= '';
open cursory for select * from atributos where idtabla = rec.id;
loop
fetch cursory into rec2;
exit when not found;
consulta = consulta + '(' || rec2.id || ', ' || rec2.idtabla || ', ' || rec2.nombre || ', ' ||rec2.tipodedatos ;
if rec2.claveprimaria = 1 then
consulta= consulta +', constraint pk_ ' || rec.nombres || '( ' || rec.id || '));';
else
consulta = consulta +');';
end if;
end loop;
close cursory;
end loop;
execute consulta;
close cursorx;
end;
$BODY$
language plpgsql volatile;
如果您需要连接事物,请使用 ||
运算符或 concat()
函数,如下所示。
您一直在使用此运算符,但出于某种原因将其与 +
混合使用。
SELECT 'string' || 'part2';
或
SELECT concat('string', 'part2');
针对您的具体情况:
consulta = consulta || '(' || rec2.id || ', ' || rec2.idtabla || ', ' || rec2.nombre || ', ' ||rec2.tipodedatos ;
if rec2.claveprimaria = 1 then
consulta= consulta || ', constraint pk_ ' || rec.nombres || '( ' || rec.id || '));';
else
consulta = consulta || ');';
当使用 ||
运算符时,如果任何值的计算结果为 NULL
,则整个输出将为 NULL
,但是当使用 concat()
函数时,它将省略 NULL
值并仍然连接其他参数。