我在 SQL 中创建了自己的类型,并且该函数使用自定义类型。该函数输出总计数 med 和平均值。 SSMS 中一切正常,但是当我运行 Java 程序时,我一直没有返回任何结果集。
这是我的函数和数据类型
CREATE TYPE [dbo].[StatsInputTableType] AS TABLE(
[Name] [VARCHAR](50) NOT NULL,
[Value] [DECIMAL](18, 2) NOT NULL
)
GO
CREATE FUNCTION [dbo].[CalcStats]( @TableName StatsInputTableType READONLY)
RETURNS @retStats TABLE (
Name VARCHAR(50),
ValueCount INT,
ValueTotal DECIMAL(18,2),
ValueAverage DECIMAL(18,2),
ValueMedian DECIMAL(18,2)
)
AS
BEGIN
DECLARE @ordered_rows TABLE (
Name VARCHAR(50),
Value DECIMAL(7,2),
RowAsc BIGINT
)
INSERT INTO @ordered_rows
SELECT Name, Value, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name, Value)
FROM @TableName
ORDER BY Name, Value OFFSET 0 ROWS;
INSERT INTO @retStats
SELECT ftn.Name,
COUNT(ftn.Name) AS ValueCount,
SUM(ftn.Value) AS ValueTotal,
CAST(AVG(ftn.Value) AS DECIMAL(5,2)) AS ValueAverage,
MAX(medinf.ValueMedian) AS ValueMedian
FROM @TableName ftn
INNER JOIN (SELECT tn.Name,
AVG(Value) AS ValueMedian
FROM @ordered_rows tn
INNER JOIN (SELECT rows.Name,
(MAX(rows.RowAsc) + MIN(rows.RowAsc))/2.0 AS averow
FROM @ordered_rows rows
GROUP BY rows.Name
) AS averow ON tn.Name = averow.Name
WHERE ABS(tn.RowAsc - averow) < 0.6
GROUP BY tn.Name
ORDER BY tn.Name OFFSET 0 ROWS
) medinf ON medinf.Name = ftn.Name
GROUP BY ftn.Name
ORDER BY ftn.Name OFFSET 0 ROWS
RETURN
END
这是如何在 SQL 中运行它并输出正确的值。
DECLARE @myTable StatsInputTableType
INSERT INTO @myTable
VALUES (
'Pancakes',5),(
'Pancakes',7),(
'Pancakes',15),(
'Pancakes',5),(
'Pancakes',8),(
'Pancakes',10),(
'Pancakes',5),(
'Pancakes',6),(
'Hotdogs',4),(
'Hotdogs',3),(
'Hotdogs',2),(
'Hotdogs',1),(
'Hotdogs',2),(
'Hotdogs',3),(
'Hotdogs',4)
SELECT * FROM dbo.CalcStats(@myTable)
ORDER BY Name
Name ValueCount ValueTotal ValueAverage ValueMedian
Hotdogs 7 19.00 2.71 3.00
Pancakes 8 61.00 7.63 6.50
现在是 Java 部分。我正在使用 Spring 框架和 jdbc 模板,并有一个对象映射器,但我什至没有到达那部分。
我尝试了以下方法:
private JdbcTemplate jdbcTemplate;
String sql = "DECLARE @myTable StatsInputTableType "
+"INSERT INTO @myTable "
+" VALUES ( "
+" 'Pancakes',5),( "
+" 'Pancakes',7),( "
+" 'Pancakes',15),( "
+" 'Pancakes',5),( "
+" 'Pancakes',8),( "
+" 'Pancakes',10),( "
+" 'Pancakes',5),( "
+" 'Pancakes',6),( "
+" 'Hotdogs',4),( "
+" 'Hotdogs',3),( "
+" 'Hotdogs',2),( "
+" 'Hotdogs',1),( "
+" 'Hotdogs',2),( "
+" 'Hotdogs',3),( "
+" 'Hotdogs',4)"
+"SELECT * FROM dbo.CalcStats(@myTable)"
+"ORDER BY Name;";
List<AvgMedTotalObj> returnValue = null;
try {
returnValue = jdbcTemplate.query(sql, new AvgMedTotalObjMapper());
System.out.println("test");
} catch (EmptyResultDataAccessException e) {
} catch (DataAccessException e) {
logger.error("JDBC statement failed. " + "Exception: ", e);
}
并不断收到此错误:该语句未返回结果集。
org.springframework.jdbc.UncategorizedSQLException:StatementCallback; SQL 的未分类 SQLException [DECLARE @myTable StatsInputTableType INSERT INTO @myTable VALUES ( 'Pancakes',5),( 'Pancakes',7),( 'Pancakes',15),( 'Pancakes',5),( 'Pancakes' ,8),( '煎饼',10),('煎饼',5),('煎饼',6),('热狗',4),('热狗',3),('热狗',2 ),( '热狗',1),('热狗',2),('热狗',3),('热狗',4)SELECT * FROM dbo.CalcStats(@myTable)按名称排序;]; SQL状态[空];错误代码[0];该语句没有返回结果集。嵌套异常是 com.microsoft.sqlserver.jdbc.SQLServerException: 该语句没有返回结果集。
所以不用说,我真的很困惑为什么 SSMS 给我一个结果集,而当我从 Java 调用它时却没有。
任何帮助将不胜感激。
您可以在两条语句之间缺少分号。