EF Core Stored Procedure Error: Incorrect syntax near '@p1'
EF Core Stored Procedure Error: Incorrect syntax near '@p1'
执行 API 调用时,出现以下错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@p1'
所以,我有以下存储过程:
/****** Object: StoredProcedure [dbo].[sprocGetDataForSummaryTable] Script Date: 30-Nov-21 00:23:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sprocGetDataForSummaryTable] @StartDate datetime2(7), @EndDate datetime2(7), @AccountName nvarchar(50) AS BEGIN
SET
NOCOUNT ON;
SELECT
a.Name,
DATEPART(YEAR, t.TransactionDate) AS Year,
DATEPART(MONTH, t.TransactionDate) AS Month,
SUM(t.Value) AS TotalValue,
c.Name AS CategoryName,
s.Name AS SubcategoryName
FROM
[PersonalMoneyTrackerDb].[dbo].[Transactions] AS t
INNER JOIN Categories AS c ON t.CategoryId = c.id
INNER JOIN Subcategories AS s ON t.SubcategoryId = s.Id
INNER JOIN Accounts as a ON t.AccountId = a.Id
WHERE
t.Type = 'Expense'
AND t.TransactionDate < @EndDate
AND t.TransactionDate > @StartDate
AND a.Name LIKE '%' + @AccountName + '%'
GROUP BY
a.Name,
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name,
s.Name
ORDER BY
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name;
END
GO
以及以下 .NET 6 EF 核心调用:
public async Task<List<SummaryTableEntity>> GetSummaries(string startDate, string endDate, string accountName)
{
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable {startDate} {endDate} {accountName}")
.ToListAsync();
}
通过使用 SQL Server Profiler,我注意到它被翻译成:
exec sp_executesql N'sprocGetDataForSummaryTable @p0 @p1 @p2
',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 nvarchar(4000)',@p0=N'01/01/2020',@p1=N'01/01/2022',@p2=N''
我完全不知道这有什么问题,请帮忙。
您似乎缺少用于分隔参数的逗号:
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable {startDate}, {endDate}, {accountName}")
.ToListAsync();
执行 API 调用时,出现以下错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@p1'
所以,我有以下存储过程:
/****** Object: StoredProcedure [dbo].[sprocGetDataForSummaryTable] Script Date: 30-Nov-21 00:23:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sprocGetDataForSummaryTable] @StartDate datetime2(7), @EndDate datetime2(7), @AccountName nvarchar(50) AS BEGIN
SET
NOCOUNT ON;
SELECT
a.Name,
DATEPART(YEAR, t.TransactionDate) AS Year,
DATEPART(MONTH, t.TransactionDate) AS Month,
SUM(t.Value) AS TotalValue,
c.Name AS CategoryName,
s.Name AS SubcategoryName
FROM
[PersonalMoneyTrackerDb].[dbo].[Transactions] AS t
INNER JOIN Categories AS c ON t.CategoryId = c.id
INNER JOIN Subcategories AS s ON t.SubcategoryId = s.Id
INNER JOIN Accounts as a ON t.AccountId = a.Id
WHERE
t.Type = 'Expense'
AND t.TransactionDate < @EndDate
AND t.TransactionDate > @StartDate
AND a.Name LIKE '%' + @AccountName + '%'
GROUP BY
a.Name,
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name,
s.Name
ORDER BY
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name;
END
GO
以及以下 .NET 6 EF 核心调用:
public async Task<List<SummaryTableEntity>> GetSummaries(string startDate, string endDate, string accountName)
{
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable {startDate} {endDate} {accountName}")
.ToListAsync();
}
通过使用 SQL Server Profiler,我注意到它被翻译成:
exec sp_executesql N'sprocGetDataForSummaryTable @p0 @p1 @p2
',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 nvarchar(4000)',@p0=N'01/01/2020',@p1=N'01/01/2022',@p2=N''
我完全不知道这有什么问题,请帮忙。
您似乎缺少用于分隔参数的逗号:
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable {startDate}, {endDate}, {accountName}")
.ToListAsync();