插入字符串包含postgresql中的“$$”

e0bqpujr  于 2022-12-23  发布在  PostgreSQL
关注(0)|答案(2)|浏览(279)

我在尝试插入用户表时遇到问题。

DO $$
 DECLARE newId bigint;
 BEGIN 
              INSERT INTO users("email","bio","status","created_by","updated_by")
              VALUES ('tetsinsert@gmail.com','❄❄Haocute$$','accepted','1','1')
                RETURNING id INTO newId;

                UPDATE synces SET "version" = "version" + 1, updated_at = now(),
                new_id = newId WHERE "old_id" = 98525 and table_name = 'users'; 
                IF NOT FOUND THEN 
                INSERT INTO synces
                (created_at, updated_at, deleted_at, table_name, old_id, new_id, "version")
                VALUES(now(), now(), null, 'users', 98525, newId, 0);
                END IF;
            
 END $$

感谢您阅读我的帖子。

pjngdqdw

pjngdqdw1#

有两种可能性:
1.使用不同的自定义字符串分隔符:

do $my_custom_string_separator$
begin
  raise notice '❄❄Haocute$$';
end
$my_custom_string_separator$;
NOTICE:  ❄❄Haocute$$
DO

1.可以使用带有转义值的扩展字符串:

do $$
begin
  raise notice e'❄❄Haocute\$\$';
end
$$;
NOTICE:  ❄❄Haocute$$
DO
ua4mk5z4

ua4mk5z42#

我找到了解决办法

DO $do$
 DECLARE newId bigint;
 BEGIN 
              INSERT INTO users("email","bio","status","created_by","updated_by")
              VALUES ('tetsinsert@gmail.com','❄❄Haocute$$','accepted','1','1')
                RETURNING id INTO newId;

                UPDATE synces SET "version" = "version" + 1, updated_at = now(),
                new_id = newId WHERE "old_id" = 98525 and table_name = 'users'; 
                IF NOT FOUND THEN 
                INSERT INTO synces
                (created_at, updated_at, deleted_at, table_name, old_id, new_id, "version")
                VALUES(now(), now(), null, 'users', 98525, newId, 0);
                END IF;
            
 END $do$

相关问题