简体   繁体   中英

Create a user-defined function to return a table with customer information?

I am trying to create a user-defined function, but I keep getting this error message:

Msg 102, Level 15, State 1, Procedure Huckestein_ufnCustomerDemographics, Line 2
Incorrect syntax near 'RETURNS'.

I have been researching this for hours and still can't figure out what is going on... Any advice is greatly appreciated.

Here is my SQL Code:

CREATE FUNCTION Huckestein_ufnCustomerDemographics 
RETURNS TABLE 
AS
RETURN
Select  CustomerID, FirstName, LastName, Phone, SalesLT.Address.*
From SalesLT.Customer, SalesLT.Address

Please create that function as shown below:

CREATE FUNCTION Huckestein_ufnCustomerDemographics()
RETURNS TABLE 
AS
RETURN (
SELECT  C.CustomerID, C.FirstName, C.LastName, C.Phone, A.*
FROM SalesLT.Customer C INNER JOIN SalesLT.CustomerAddress AS CA  
        ON C.CustomerID = CA.CustomerID
     INNER JOIN SalesLT.[Address] A
        ON A.AddressID = CA.AddressID

);
GO

You can test it as shown below:

SELECT * FROM Huckestein_ufnCustomerDemographics()

Try this

CREATE FUNCTION Huckestein_ufnCustomerDemographics 
RETURNS TABLE 
AS
RETURN
(Select  CustomerID, FirstName, LastName, Phone, SalesLT.Address.*
From SalesLT.Customer, SalesLT.Address);

Note the brackets and semi colon. RETURNS TABLE creates an inline table valued function. It is meant to have a single select wrapped in a return statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM