如何将此编码从 SQL 服务器更改为 PostgreSQL
How to change this coding from SQL Server to be in PostgreSQL
此查询运行使用 Microsoft SQL Server Management Studio 成功,但我无法在 PostgreSQL 中使用它。我想要一个类似的查询来使用 PostgreSQL 给出相同的结果
请注意:这将 运行 在包含 1 名初级和 2 名高级的 table 上。
如果我有更多的大三和大四学生,我也想使用这个查询
DECLARE @Budget Int = 50000, @Seniors int = 0, @Juniors int = 0, @SeniorSalary int = 20000, @juniorSalary int = 10000
WHILE @Budget >= @juniorSalary
BEGIN
IF @Budget >= @SeniorSalary
BEGIN
SET @Budget = @Budget - @SeniorSalary;
SET @Seniors = @Seniors + 1;
END
ELSE IF @Budget >= @juniorSalary
BEGIN
SET @Budget = @Budget - @juniorSalary;
SET @Juniors = @Juniors + 1;
END
END
SELECT @Seniors AS [Seniors], @Juniors as [Juniors], @Budget As [RemainingBudget]
你的第一个错误是假设 SQL 服务器中的某些东西 运行 它将 Postgres(或任何其他 DBMS)从那个服务器流出。此外,它不是查询,而是脚本,psql 脚本的结构与 T-sql 脚本完全不同。我建议您花大量时间学习一些 Postgres 教程,例如 command line tutorial and PostgreSQLTutorial and/or 其他人或 !gasp! 旧技术 - 一本书。
我很欣赏有时问题是概念之一,比如在 Postgres 中期望 T-Sql 脚本到 运行。因此,我提供以下内容作为一次性演示,为您指明正确的方向。它是您脚本的 Postgres 翻译,当然不是唯一的,也许不是最好的。
我把它的描述留给你研究。
create or replace
function developer_budget
( budget integer
, seniorsalary integer
, juniorsalary integer
)
returns table ( seniors integer
, juniors integer
, remaining integer
)
language plpgsql
as $$
declare
seniors integer = 0;
juniors integer = 0;
begin
while budget >= juniorsalary
loop
if budget >= seniorsalary then
budget = budget - seniorsalary;
seniors = seniors + 1;
elsif budget >= juniorsalary then
budget = budget - juniorsalary;
juniors = juniors + 1;
end if;
end loop;
return query select seniors,juniors, budget;
end;
$$;
select seniors "Seniors"
, juniors "Juniors"
, remaining "Remaining bubjet"
from developer_budget( Budget => 50000
, SeniorSalary => 20000
, JuniorSalary => 10000
);
此查询运行使用 Microsoft SQL Server Management Studio 成功,但我无法在 PostgreSQL 中使用它。我想要一个类似的查询来使用 PostgreSQL 给出相同的结果 请注意:这将 运行 在包含 1 名初级和 2 名高级的 table 上。 如果我有更多的大三和大四学生,我也想使用这个查询
DECLARE @Budget Int = 50000, @Seniors int = 0, @Juniors int = 0, @SeniorSalary int = 20000, @juniorSalary int = 10000
WHILE @Budget >= @juniorSalary
BEGIN
IF @Budget >= @SeniorSalary
BEGIN
SET @Budget = @Budget - @SeniorSalary;
SET @Seniors = @Seniors + 1;
END
ELSE IF @Budget >= @juniorSalary
BEGIN
SET @Budget = @Budget - @juniorSalary;
SET @Juniors = @Juniors + 1;
END
END
SELECT @Seniors AS [Seniors], @Juniors as [Juniors], @Budget As [RemainingBudget]
你的第一个错误是假设 SQL 服务器中的某些东西 运行 它将 Postgres(或任何其他 DBMS)从那个服务器流出。此外,它不是查询,而是脚本,psql 脚本的结构与 T-sql 脚本完全不同。我建议您花大量时间学习一些 Postgres 教程,例如 command line tutorial and PostgreSQLTutorial and/or 其他人或 !gasp! 旧技术 - 一本书。
我很欣赏有时问题是概念之一,比如在 Postgres 中期望 T-Sql 脚本到 运行。因此,我提供以下内容作为一次性演示,为您指明正确的方向。它是您脚本的 Postgres 翻译,当然不是唯一的,也许不是最好的。
我把它的描述留给你研究。
create or replace
function developer_budget
( budget integer
, seniorsalary integer
, juniorsalary integer
)
returns table ( seniors integer
, juniors integer
, remaining integer
)
language plpgsql
as $$
declare
seniors integer = 0;
juniors integer = 0;
begin
while budget >= juniorsalary
loop
if budget >= seniorsalary then
budget = budget - seniorsalary;
seniors = seniors + 1;
elsif budget >= juniorsalary then
budget = budget - juniorsalary;
juniors = juniors + 1;
end if;
end loop;
return query select seniors,juniors, budget;
end;
$$;
select seniors "Seniors"
, juniors "Juniors"
, remaining "Remaining bubjet"
from developer_budget( Budget => 50000
, SeniorSalary => 20000
, JuniorSalary => 10000
);