21 Maret 2012

Incorrect syntax near 'RETURNS' or A RETURN statement with a return value cannot be used in this context

Applies To:
Microsoft SQL Server 2008 R2

Error Messages:
Msg 102, Level 15, State 1, Procedure A_Show_Product, Line 2

Incorrect syntax near 'RETURNS'.

Msg 178, Level 15, State 1, Procedure A_Show_Product, Line 3

A RETURN statement with a return value cannot be used in this context.






Code-1:

-----start-----
USE AdventureWorks

CREATE FUNCTION A_Show_Product         
RETURNS TABLE         
AS RETURN
(            
    SELECT * FROM Production.Product WHERE PRODUCTID='1' 

-----end-----


Error Messages:
Msg 102, Level 15, State 1, Procedure A_Show_Product, Line 2
Incorrect syntax near 'RETURNS'.
Msg 178, Level 15, State 1, Procedure A_Show_Product, Line 3
A RETURN statement with a return value cannot be used in this context.



Solution:
Adding the symbol "()" after syntax CREATE FUNCTION.
from "CREATE FUNCTION A_Show_Product" become "CREATE FUNCTION A_Show_Product ()"




Code After Fix:

-----start-----
USE AdventureWorks

CREATE FUNCTION A_Show_Product ()        
RETURNS TABLE         
AS RETURN
(            
    SELECT * FROM Production.Product WHERE PRODUCTID='1' 

-----end-----


Messages:
Command(s) completed successfully.



How to use this function:
----start----
SELECT * FROM A_Show_Product()
-----end-----


How to remove this function:
----start----
DROP FUNCTION A_Show_Product
-----end-----