DECLARE @max1 INT;
DECLARE @max2 INT;
DECLARE @result INT;
SELECT @max1= MAX(ISNULL(Table1.Field1,0)) FROM Table1
WHERE CAST(Table1.registerDate AS DATE) = CAST (GETDATE() AS DATE)
SELECT @max2= MAX(ISNULL(Table2.Field1,0)) FROM Table2
WHERE CAST(Table2.registerDate AS DATE) = CAST (GETDATE() AS DATE)
SET @result=CASE WHEN @max1>@max2 THEN @max1+1 ELSE @max2+1 END;
我想根据registerDate从2表中获取最大值并返回此最大值+ 1; 现在最大值为12,此函数返回504例如!! 谁能告诉我这是怎么回事?
更新:根据此代码,一天的第一个值应为1.但在许多情况下,它返回25000例如!
答案 0 :(得分:0)
这里没有错。下面的代码按预期返回13。
DECLARE @max1 INT = 12;
DECLARE @max2 INT = 11;
DECLARE @result INT;
-- SELECT @max1= MAX(Table1.Field1) FROM Table1
--WHERE CAST(Table1.registerDate AS DATE) = CAST (GETDATE() AS DATE)
-- SELECT @max2= MAX(Table2.Field1) FROM Table2
--WHERE CAST(Table2.registerDate AS DATE) = CAST (GETDATE() AS DATE)
SET @result=CASE WHEN @max1>@max2 THEN @max1+1 ELSE @max2+1 END;
SELECT @result
我打赌下面的一个查询返回的是503。
SELECT MAX(Table1.Field1) FROM Table1
WHERE CAST(Table1.registerDate AS DATE) = CAST (GETDATE() AS DATE)
SELECT MAX(Table2.Field1) FROM Table2
WHERE CAST(Table2.registerDate AS DATE) = CAST (GETDATE() AS DATE)