SQL存储过程中 错误156:在关键字‘set’附近有语法错误

SQL2000的存储过程如下,检查时老是出现错误156:在关键字‘set’附近有语法错误,请高手指导。
CREATE PROCEDURE [test]
as
Declare @n int
Declare @ct datetime
set @n=-30
while @n<0
begin
set @ct=DATEADD(DAY,@n, GETDATE())
INSERT INTO AI
SELECT AVG (f1) AS f1, AVG (f2) AS f2, AVG (f3) AS f3, @ct AS dt
FROM demo
WHERE (YEAR(dt)=YEAR(@ct) AND (Month(dt)=MONTH(@ct) AND (Day(dt)=DAY (@ct)
set @n=@n+1
end
go
CREATE PROCEDURE [test]
as
Declare @n int
Declare @ct datetime
set @n=-30
while @n<0
begin
set @ct=DATEADD(DAY,@n, GETDATE())
INSERT INTO AI
SELECT AVG (f1) AS f1, AVG (f2) AS f2, AVG (f3) AS f3, @ct AS dt
FROM demo
WHERE (YEAR(dt)=YEAR(@ct))AND (Month(dt)=MONTH(@ct)) AND (Day(dt)=DAY (@ct))
set @n=@n+1
end
go
最新回答
敲击岁月

2024-11-03 06:22:41

WHERE 部分括号混乱,不对称啊,弄不清楚就每个条件一行,一目了然。
凉风有信

2024-11-03 05:37:11

CREATE PROCEDURE [test]
as
Declare @n int
Declare @ct datetime
set @n=-30
while @n<0
begin
set @ct=DATEADD(DAY,@n, GETDATE())
INSERT INTO AI
SELECT AVG (f1) AS f1, AVG (f2) AS f2, AVG (f3) AS f3, @ct AS dt
FROM demo
WHERE YEAR(dt)=YEAR(@ct) AND Month(dt)=MONTH(@ct) AND Day(dt)=DAY (@ct)
set @n=@n+1
end
go