gpt4 book ai didi

c# - “输入末尾的语法错误”- 尝试从数据库启动存储过程时

转载 作者:行者123 更新时间:2023-11-29 12:50:03 25 4
gpt4 key购买 nike

我正在用 .net 核心编写并尝试从数据库 (Postgresql) 启动存储过程。

我收到一条错误消息,提示“输入末尾存在语法错误”。连接字符串正确(调试时发现)。问题在于 .FromSql() 方法语法。

代码如下:

List<ActivePackageForOpenBillingPeriod> activeUserPackagesForOpenBillingPeriod =null;

using(var conn = new NpgsqlConnection("Host=localhost;Port=xxx;Database=postgres;Username=xxx;Password=xxx;TrustServerCertificate=xxx;ApplicationName=xxx;"))
{

var s = "create function \"GetActiveUserPackagesForOpenBillingPeriod({0})\"";
activeUserPackagesForOpenBillingPeriod = context.ActivePackageForOpenBillingPeriods.FromSql(s,DateTime.Now).ToList();
}

这是我要调用的存储过程

    create function "GetActiveUserPackagesForOpenBillingPeriod"(date 
timestamp without time zone) returns TABLE("Amount" numeric, "Package"
character varying, "User" text,"Account" int, "AcceptanceActID" int,
"HasChangedPackage" boolean)
language plpgsql
as
$$
DECLARE
BEGIN
RETURN QUERY
SELECT
a."ID" AS "AccountId",
u."ID" AS "UserId",
up."PackageID",
up."TotalAmount",
CASE
WHEN count(DISTINCT tl."PackageID") IN (0,1) THEN false
ELSE true
END AS "HasChangedPackage"

FROM public."Accounts" as a
LEFT JOIN billing."TransactionHeaders" AS th ON th."AccountID" = a."ID"
INNER JOIN security."Users" AS u ON u."AccountID"= a."ID"
INNER JOIN billing."UserPackages" AS up ON up."UserID"=u."ID" AND COALESCE(th."Date", date) BETWEEN up."StartDate" AND COALESCE(up."EndDate", date)
LEFT JOIN billing."TransactionLines" AS tl ON th."Id" = tl."TransactionHeaderID"

WHERE th."AcceptanceActID" IS NULL AND a."StatusID"!=4 AND a."StatusID"!=3 AND up."StatusID"=1


GROUP BY a."ID", u."ID", up."PackageID", up."TotalAmount";

END;
$$;

最佳答案

您正在运行的查询是错误的。它混合了函数创建语句和获取结果的调用。

只有函数名应该被双引号,而不是参数

将查询更改为

var s = "SELECT * FROM \"GetActiveUserPackagesForOpenBillingPeriod\"({0})";

关于c# - “输入末尾的语法错误”- 尝试从数据库启动存储过程时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56462479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com