在 PostgreSQL 中声明局部变量?

发布于 2024-09-08 04:31:06 字数 562 浏览 9 评论 0原文

这里有一个几乎相同但没有真正回答的问题。

我正在将应用程序从 MS SQL Server 迁移到 PostgreSQL。在代码中的许多地方我使用局部变量,因此我想进行需要较少工作的更改,所以您能告诉我哪种是翻译以下代码的最佳方法吗?

-- MS SQL Syntax: declare 2 variables, assign value and return the sum of the two 
declare @One integer = 1
declare @Two integer = 2
select @One + @Two as SUM

返回:

SUM
-----------
3

(1 row(s) affected)

如果 Postgresql 8.4 甚至 9.0 包含可以简化翻译的重要功能,我将使用它。

There is an almost identical, but not really answered question here.

I am migrating an application from MS SQL Server to PostgreSQL. In many places in code I use local variables so I would like to go for the change that requires less work, so could you please tell me which is the best way to translate the following code?

-- MS SQL Syntax: declare 2 variables, assign value and return the sum of the two 
declare @One integer = 1
declare @Two integer = 2
select @One + @Two as SUM

this returns:

SUM
-----------
3

(1 row(s) affected)

I will use Postgresql 8.4 or even 9.0 if it contains significant fetaures that will simplify the translation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2024-09-15 04:31:06

Postgresql 历史上不支持命令级别的过程代码 - 仅在函数内。但是,在 Postgresql 9 中,添加了对执行内联代码块 有效地支持类似的东西,尽管语法可能有点奇怪,并且与使用 SQL Server 可以做的事情相比有很多限制。值得注意的是,内联代码块无法返回结果集,因此不能用于上面概述的内容。

一般来说,如果您想编写一些过程代码并让它返回结果,您需要将其放在函数中。例如:

CREATE OR REPLACE FUNCTION somefuncname() RETURNS int LANGUAGE plpgsql AS $
DECLARE
  one int;
  two int;
BEGIN
  one := 1;
  two := 2;
  RETURN one + two;
END
$;
SELECT somefuncname();

据我所知,PostgreSQL 有线协议不允许诸如命令返回多个结果集之类的事情。因此,您不能简单地将 T-SQL 批处理或存储过程映射到 PostgreSQL 函数。

Postgresql historically doesn't support procedural code at the command level - only within functions. However, in Postgresql 9, support has been added to execute an inline code block that effectively supports something like this, although the syntax is perhaps a bit odd, and there are many restrictions compared to what you can do with SQL Server. Notably, the inline code block can't return a result set, so can't be used for what you outline above.

In general, if you want to write some procedural code and have it return a result, you need to put it inside a function. For example:

CREATE OR REPLACE FUNCTION somefuncname() RETURNS int LANGUAGE plpgsql AS $
DECLARE
  one int;
  two int;
BEGIN
  one := 1;
  two := 2;
  RETURN one + two;
END
$;
SELECT somefuncname();

The PostgreSQL wire protocol doesn't, as far as I know, allow for things like a command returning multiple result sets. So you can't simply map T-SQL batches or stored procedures to PostgreSQL functions.

红焚 2024-09-15 04:31:06

您可以在 DECLARE 子句中使用 :== 声明局部变量,如下所示:

CREATE FUNCTION my_func()
RETURNS INT
AS $
DECLARE
  value1 INT := 1; -- Here
  value2 INT = 2; -- Here
  value3 INT DEFAULT 3; -- Here
  value4 CONSTANT INT := 4; -- Here
  value5 INT; -- Here
BEGIN
  RETURN value1 + value2 + value3;
END;
$ LANGUAGE plpgsql;

*Memos:

  • :==DEFAULT 是相同的。
  • 尝试更改常量局部变量 value4 会出错。
  • 未初始化的局部变量value5NULL
  • 您可以在 PL/pgSQL 函数和过程以及 DO 语句中使用 DECLARE 子句声明局部变量。

然后,调用 my_func() 返回 6 ,如下所示:

postgres=# SELECT my_func();
 my_func
---------
       6
(1 row)

You can declare local variables with :=, = in DECLARE clause as shown below:

CREATE FUNCTION my_func()
RETURNS INT
AS $
DECLARE
  value1 INT := 1; -- Here
  value2 INT = 2; -- Here
  value3 INT DEFAULT 3; -- Here
  value4 CONSTANT INT := 4; -- Here
  value5 INT; -- Here
BEGIN
  RETURN value1 + value2 + value3;
END;
$ LANGUAGE plpgsql;

*Memos:

  • :=, = and DEFAULT are the same.
  • Trying to change the constant local variable value4 gets error.
  • The uninitialized local variable value5 is NULL.
  • You can declare local variables with DECLARE clause in a PL/pgSQL function and procedure and DO statement.

Then, calling my_func() returns 6 as shown below:

postgres=# SELECT my_func();
 my_func
---------
       6
(1 row)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文