问题抽象如下:
CREATE PROCEDURE TESTT
(
@Age int
)
AS
;WITH TEST AS
(
SELECT * FROM INFO
)
IF @Age<18
BEGIN
SELECT * FROM TEST WHERE Age<18
END
ELSE IF @Age=18
BEGIN
SELECT * FROM TEST WHERE Age=18
END
ELSE
SELECT * FROM TEST WHERE Age>18
错误提示:
Incorrect syntax near the keyword 'IF'.
如果不能请提示如何更改,谢谢。
------解决方案--------------------
CREATE PROCEDURE TESTT
(
@Age int
)
AS
begin
;WITH TEST AS
(
SELECT * FROM INFO
)
select * from test where age<18 and @age<18
union all
select * from text where age=18 and @age=18
union all
select * from text where age>18 and @age>18
end
------解决方案--------------------
CREATE PROCEDURE TESTT
(
@Age int
)
AS
SELECT * into TEST FROM INFO
IF @Age<18
BEGIN
SELECT * FROM TEST WHERE Age<18
END
ELSE IF @Age=18
BEGIN
SELECT * FROM TEST WHERE Age=18
END
ELSE
SELECT * FROM TEST WHERE Age>18
drop table TEST
------解决方案--------------------
CREATE PROCEDURE TESTT
(
@Age int
)
AS
;WITH TEST AS
(
SELECT * FROM INFO
)
SELECT
*
FROM TEST
WHERE (Age<18 AND @Age<18)
OR (Age=18 AND @Age=18)
OR (Age>18 AND @Age>18)