I am writing a user defined function and I get the Error 156: Incorrect
syntax near the keyword IF. My function looks like this
CREATE FUNCTION dbo.func1(@.var1 varchar(64))
RETURNS @.MaintCost TABLE (@.result1 varchar(64), @.result2 varchar(64),
@.result3 varchar(64))
AS
IF @.var1 = 'I'
BEGIN
INSERT @.MaintCost
SELECT Col1, Col2, Col3
FROM tbl1
WHERE Col3 = 'I'
RETURN
END
ELSE
BEGIN
INSERT @.MaintCost
SELECT Col1, Col2, Col3
FROM tbl1
WHERE Col3 <> 'I'
RETURN
Thanks for the Help
ENDCREATE FUNCTION dbo.func1(@.var1 varchar(64))
RETURNS @.MaintCost TABLE (@.result1 varchar(64), @.result2 varchar(64),
@.result3 varchar(64))
AS
BEGIN
...
END
AMB
"Keith" wrote:
> I am writing a user defined function and I get the Error 156: Incorrect
> syntax near the keyword IF. My function looks like this
> CREATE FUNCTION dbo.func1(@.var1 varchar(64))
> RETURNS @.MaintCost TABLE (@.result1 varchar(64), @.result2 varchar(64),
> @.result3 varchar(64))
> AS
> IF @.var1 = 'I'
> BEGIN
> INSERT @.MaintCost
> SELECT Col1, Col2, Col3
> FROM tbl1
> WHERE Col3 = 'I'
> RETURN
> END
> ELSE
> BEGIN
> INSERT @.MaintCost
> SELECT Col1, Col2, Col3
> FROM tbl1
> WHERE Col3 <> 'I'
> RETURN
> Thanks for the Help
> END|||Try this:
CREATE FUNCTION dbo.func1(@.var1 varchar(64))
RETURNS @.MaintCost TABLE
(
result1 varchar(64),
result2 varchar(64),
result3 varchar(64)
)
AS
BEGIN
IF @.var1 = 'I'
BEGIN
INSERT @.MaintCost
SELECT Col1, Col2, Col3
FROM tbl1
WHERE Col3 = 'I'
END
ELSE
BEGIN
INSERT @.MaintCost
SELECT Col1, Col2, Col3
FROM tbl1
WHERE Col3 <> 'I'
END
RETURN
END
No comments:
Post a Comment