{"Entry":{"collection":"sql","key":"do","name":"DO","aliases":["do"],"metadata":{"aliases":["do"],"changed_in":[],"changes":[{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"9.0"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"11"}],"content_hash":"80eeade202486cadd0c4823c2466185ac8bced65aa1f650e4fd477cb59e396b9","derived_content_hash":"fcc193278f1abdacb25cff3ef81c4f77f229cc8fac799bd4001c1289e23aaca4","documentation_refresh":{"batch":"20261113","original_source_rev":"wiki-repair-20261023:39624b539264e3bf743e408e29569a41aa906e1cf1ad1072ffa416ce1f002654","versions":{"14":"14.25","15":"15.20","16":"16.16","17":"17.12","18":"18.7","19":"19rc2"}},"editorial":{},"first_version":"9.0","group":"routine","imported_at":"2026-09-30T17:43:36.967434+08:00","last_version":"20","name":"DO","object":"","position":2001,"present_in":["9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"execute an anonymous code block","purpose_zh":"","related":["create-language"],"repair_provenance":{"original_source_revision":"wiki-repair-20261023:0ea8d6fdb74ec7df630fd3af4bd3aa12fa7933d8b1e80b797fe1a4caaa0a25f2","receipt_sha256":"9c8c58f7d70e0565d465b50da843b31d59c47ce78b0703333188e05ed89aeef8","repair":"wiki-repair-20261023"},"slug":"do","source_rev":"wiki-repair-20261023:39624b539264e3bf743e408e29569a41aa906e1cf1ad1072ffa416ce1f002654","synopsis":"DO [ LANGUAGE lang_name ] code","verb":"DO"}},"Definition":{"Collection":"sql","Key":"do","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"do","SourceRevision":"wiki-repair-20261023:39624b539264e3bf743e408e29569a41aa906e1cf1ad1072ffa416ce1f002654","Facts":{"anchor":"SQL-DO","description_zh":"执行匿名代码块","description_zh_html":"\u003cpre\u003eDO [ LANGUAGE lang_name ] code\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\n\u003ccode class=\"command\"\u003eDO\u003c/code\u003e 执行匿名代码块，换言之，就是执行一个以过程语言编写的瞬时匿名函数。\n  \u003c/p\u003e\u003cp\u003e\n   该代码块会被当作一个不带参数、返回 \u003ccode class=\"type\"\u003evoid\u003c/code\u003e 的函数体。它只会被解析并执行一次。\n  \u003c/p\u003e\u003cp\u003e\n   可选的 \u003ccode class=\"literal\"\u003eLANGUAGE\u003c/code\u003e 子句可以写在代码块之前，也可以写在之后。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      要执行的过程语言代码。与 \u003ccode class=\"command\"\u003eCREATE FUNCTION\u003c/code\u003e 一样，它必须以字符串字面值形式指定。建议使用美元引用的字符串字面值。\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      编写该代码所用的过程语言名称。如果省略，默认值为 \u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e。\n     \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\n   要使用的过程语言必须已经通过 \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e 安装到当前数据库中。\u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e 默认已安装，而其他语言则不会。\n  \u003c/p\u003e\u003cp\u003e\n   用户必须拥有该过程语言的 \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e 权限；如果该语言是不受信任的，则必须是超级用户。这与用该语言创建函数时的权限要求相同。\n  \u003c/p\u003e\u003cp\u003e\n   如果在事务块中执行 \u003ccode class=\"command\"\u003eDO\u003c/code\u003e，则该过程语言代码不能执行事务控制语句。只有当 \u003ccode class=\"command\"\u003eDO\u003c/code\u003e 在其自身事务中执行时，才允许使用事务控制语句。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e\n   将模式 \u003ccode class=\"literal\"\u003epublic\u003c/code\u003e 中所有视图上的全部权限授予角色 \u003ccode class=\"literal\"\u003ewebuser\u003c/code\u003e：\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nDO $$\nDECLARE r record;\nBEGIN\n    FOR r IN SELECT table_schema, table_name FROM information_schema.tables\n             WHERE table_type = 'VIEW' AND table_schema = 'public'\n    LOOP\n        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';\n    END LOOP;\nEND$$;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\n   SQL 标准中没有 \u003ccode class=\"command\"\u003eDO\u003c/code\u003e 语句。\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan class=\"simplelist\"\u003e\u003ca class=\"xref\" href=\"/zh/docs/18/sql-createlanguage.html\" title=\"CREATE LANGUAGE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","documentation_revision":{"inputs":[{"file":"sql-do.html","kind":"same-version-reviewed-manual","locale":"en","sha256":"9a7e6ac5693fc6aab66c1f57d14eaa139c20b5b326ee49ceaf0696c60e3f1b94","version":"18.7"},{"file":"sql-do.html","kind":"same-version-reviewed-manual","locale":"zh-Hans","sha256":"c00b3f08a94808c4a141643fde64463e026cf95fef018dabf9d7907ea3b11df9","version":"18.7"}],"release":"18.7","runtime_remeasured":false,"source_revision":"pgdoc:b9f0e7232f60f380d642ac9b99226606c254b552"},"file":"sql-do.html","lang":"en","name":"DO","purpose":"execute an anonymous code block","purpose_zh":"执行匿名代码块","related":["create-language"],"sections":[{"html":"\u003cp\u003e\n\u003ccode class=\"command\"\u003eDO\u003c/code\u003e executes an anonymous code block, or in other\n   words a transient anonymous function in a procedural language.\n  \u003c/p\u003e\u003cp\u003e\n   The code block is treated as though it were the body of a function\n   with no parameters, returning \u003ccode class=\"type\"\u003evoid\u003c/code\u003e.  It is parsed and\n   executed a single time.\n  \u003c/p\u003e\u003cp\u003e\n   The optional \u003ccode class=\"literal\"\u003eLANGUAGE\u003c/code\u003e clause can be written either\n   before or after the code block.\n  \u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The procedural language code to be executed.  This must be specified\n      as a string literal, just as in \u003ccode class=\"command\"\u003eCREATE FUNCTION\u003c/code\u003e.\n      Use of a dollar-quoted literal is recommended.\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The name of the procedural language the code is written in.\n      If omitted, the default is \u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e.\n     \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\n   The procedural language to be used must already have been installed\n   into the current database by means of \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e.\n   \u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e is installed by default, but other languages are not.\n  \u003c/p\u003e\u003cp\u003e\n   The user must have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege for the procedural\n   language, or must be a superuser if the language is untrusted.\n   This is the same privilege requirement as for creating a function\n   in the language.\n  \u003c/p\u003e\u003cp\u003e\n   If \u003ccode class=\"command\"\u003eDO\u003c/code\u003e is executed in a transaction block, then the\n   procedure code cannot execute transaction control statements.  Transaction\n   control statements are only allowed if \u003ccode class=\"command\"\u003eDO\u003c/code\u003e is executed in\n   its own transaction.\n  \u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003e\n   Grant all privileges on all views in schema \u003ccode class=\"literal\"\u003epublic\u003c/code\u003e to\n   role \u003ccode class=\"literal\"\u003ewebuser\u003c/code\u003e:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nDO $$\nDECLARE r record;\nBEGIN\n    FOR r IN SELECT table_schema, table_name FROM information_schema.tables\n             WHERE table_type = 'VIEW' AND table_schema = 'public'\n    LOOP\n        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';\n    END LOOP;\nEND$$;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003e\n   There is no \u003ccode class=\"command\"\u003eDO\u003c/code\u003e statement in the SQL standard.\n  \u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca class=\"xref\" href=\"/docs/18/sql-createlanguage.html\" title=\"CREATE LANGUAGE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"DO [ LANGUAGE \u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e","synopsis_text":"DO [ LANGUAGE lang_name ] code","translation_provenance":{"inputs":[{"kind":"current-pgdoc-translation","path":"/Users/vonng/pgsty/pgdoc/zh/18/html/sql-do.html","sha256":"106ea1d9f14bb5555bf3c230389bbffc9688544b3c315b36ccef82a560a5d453"}],"kind":"same-version-pgdoc","locale":"zh-Hans","source_revision":"b1b500221dbe5437cfca004d6b04d3f6e422765d","version":"18"},"zh_from":"same-version-manual"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"do","SourceDatabase":"center","Version":"18","Locale":"en","Title":"DO","Summary":"execute an anonymous code block","BodyHTML":"\u003cpre\u003eDO [ LANGUAGE lang_name ] code\u003c/pre\u003e\u003csection\u003e\u003ch2\u003eDescription\u003c/h2\u003e\u003cp\u003e\n\u003ccode\u003eDO\u003c/code\u003e executes an anonymous code block, or in other\n   words a transient anonymous function in a procedural language.\n  \u003c/p\u003e\u003cp\u003e\n   The code block is treated as though it were the body of a function\n   with no parameters, returning \u003ccode\u003evoid\u003c/code\u003e.  It is parsed and\n   executed a single time.\n  \u003c/p\u003e\u003cp\u003e\n   The optional \u003ccode\u003eLANGUAGE\u003c/code\u003e clause can be written either\n   before or after the code block.\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003eParameters\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The procedural language code to be executed.  This must be specified\n      as a string literal, just as in \u003ccode\u003eCREATE FUNCTION\u003c/code\u003e.\n      Use of a dollar-quoted literal is recommended.\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The name of the procedural language the code is written in.\n      If omitted, the default is \u003ccode\u003eplpgsql\u003c/code\u003e.\n     \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003eNotes\u003c/h2\u003e\u003cp\u003e\n   The procedural language to be used must already have been installed\n   into the current database by means of \u003ccode\u003eCREATE EXTENSION\u003c/code\u003e.\n   \u003ccode\u003eplpgsql\u003c/code\u003e is installed by default, but other languages are not.\n  \u003c/p\u003e\u003cp\u003e\n   The user must have \u003ccode\u003eUSAGE\u003c/code\u003e privilege for the procedural\n   language, or must be a superuser if the language is untrusted.\n   This is the same privilege requirement as for creating a function\n   in the language.\n  \u003c/p\u003e\u003cp\u003e\n   If \u003ccode\u003eDO\u003c/code\u003e is executed in a transaction block, then the\n   procedure code cannot execute transaction control statements.  Transaction\n   control statements are only allowed if \u003ccode\u003eDO\u003c/code\u003e is executed in\n   its own transaction.\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003eExamples\u003c/h2\u003e\u003cp\u003e\n   Grant all privileges on all views in schema \u003ccode\u003epublic\u003c/code\u003e to\n   role \u003ccode\u003ewebuser\u003c/code\u003e:\n\u003c/p\u003e\u003cpre\u003e\nDO $$\nDECLARE r record;\nBEGIN\n    FOR r IN SELECT table_schema, table_name FROM information_schema.tables\n             WHERE table_type = \u0026#39;VIEW\u0026#39; AND table_schema = \u0026#39;public\u0026#39;\n    LOOP\n        EXECUTE \u0026#39;GRANT ALL ON \u0026#39; || quote_ident(r.table_schema) || \u0026#39;.\u0026#39; || quote_ident(r.table_name) || \u0026#39; TO webuser\u0026#39;;\n    END LOOP;\nEND$$;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003eCompatibility\u003c/h2\u003e\u003cp\u003e\n   There is no \u003ccode\u003eDO\u003c/code\u003e statement in the SQL standard.\n  \u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003eSee Also\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/docs/18/sql-createlanguage.html\" title=\"CREATE LANGUAGE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"wiki-repair-20261023:39624b539264e3bf743e408e29569a41aa906e1cf1ad1072ffa416ce1f002654","ContentHash":"94a5866a394cf1fe883a9bce40f18b335d7e9b700849a6fc08a3d98abafff2ba","Payload":{"related":["create-language"],"sections":[{"html":"\u003cp\u003e\n\u003ccode class=\"command\"\u003eDO\u003c/code\u003e executes an anonymous code block, or in other\n   words a transient anonymous function in a procedural language.\n  \u003c/p\u003e\u003cp\u003e\n   The code block is treated as though it were the body of a function\n   with no parameters, returning \u003ccode class=\"type\"\u003evoid\u003c/code\u003e.  It is parsed and\n   executed a single time.\n  \u003c/p\u003e\u003cp\u003e\n   The optional \u003ccode class=\"literal\"\u003eLANGUAGE\u003c/code\u003e clause can be written either\n   before or after the code block.\n  \u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The procedural language code to be executed.  This must be specified\n      as a string literal, just as in \u003ccode class=\"command\"\u003eCREATE FUNCTION\u003c/code\u003e.\n      Use of a dollar-quoted literal is recommended.\n     \u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\n      The name of the procedural language the code is written in.\n      If omitted, the default is \u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e.\n     \u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\n   The procedural language to be used must already have been installed\n   into the current database by means of \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e.\n   \u003ccode class=\"literal\"\u003eplpgsql\u003c/code\u003e is installed by default, but other languages are not.\n  \u003c/p\u003e\u003cp\u003e\n   The user must have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege for the procedural\n   language, or must be a superuser if the language is untrusted.\n   This is the same privilege requirement as for creating a function\n   in the language.\n  \u003c/p\u003e\u003cp\u003e\n   If \u003ccode class=\"command\"\u003eDO\u003c/code\u003e is executed in a transaction block, then the\n   procedure code cannot execute transaction control statements.  Transaction\n   control statements are only allowed if \u003ccode class=\"command\"\u003eDO\u003c/code\u003e is executed in\n   its own transaction.\n  \u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003e\n   Grant all privileges on all views in schema \u003ccode class=\"literal\"\u003epublic\u003c/code\u003e to\n   role \u003ccode class=\"literal\"\u003ewebuser\u003c/code\u003e:\n\u003c/p\u003e\u003cpre class=\"programlisting\"\u003e\nDO $$\nDECLARE r record;\nBEGIN\n    FOR r IN SELECT table_schema, table_name FROM information_schema.tables\n             WHERE table_type = 'VIEW' AND table_schema = 'public'\n    LOOP\n        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';\n    END LOOP;\nEND$$;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003e\n   There is no \u003ccode class=\"command\"\u003eDO\u003c/code\u003e statement in the SQL standard.\n  \u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca class=\"xref\" href=\"/docs/18/sql-createlanguage.html\" title=\"CREATE LANGUAGE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE LANGUAGE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","synopsis_html":"DO [ LANGUAGE \u003cem class=\"replaceable\"\u003e\u003ccode\u003elang_name\u003c/code\u003e\u003c/em\u003e ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecode\u003c/code\u003e\u003c/em\u003e","synopsis_text":"DO [ LANGUAGE lang_name ] code"}},"RequestedLocale":"en","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
