3

I am working on an assignment and found some trouble. Here is the assignment:

This assignment will use the northwind database and the Employees table.

This will be a table user defined function.

  1. Create a user defined function named Employee_Name. Pass an Employee Id as a parameter.

  2. Return the First Name, Last Name, and Phone Number of the employee whose ID matches the id passed to the user defined function.

  3. Create a query that uses the function and returns information for the employee with an id of 5.

Here is my solution so far:

CREATE FUNCTION dbo.Employee_Name(@employId int)
RETURNS TABLE
AS 
RETURN(
    SELECT FirstName,LastName,HomePhone 
    FROM dbo.Employees 
    WHERE EmployeeID = @employId
    GROUP BY EmployeeID
)

SELECT * FROM dbo.Employee_Name(5);
GO

Here is the error I'm receiving:

Msg 156, Level 15, State 1, Procedure Employee_Name, Line 11
Incorrect syntax near the keyword 'SELECT'.
3
  • Just a guess, but you might have to create a temp table in your function, populate it, then return it. Commented Nov 24, 2013 at 21:37
  • 1
    Northwind is a sample for Microsoft SQL Server. Is the MySQL tag appropriate on this question? Commented Nov 24, 2013 at 22:07
  • @DanBracuk I thought you cannot use a temp table inside of UDF? Commented Nov 24, 2013 at 23:00

3 Answers 3

1

You have to seperate creating the function and using it. Run the CREATE statement before you start working on #3.
Technically, you can also just insert a GO before the final SELECT in your sample code and it should work alright.

Sign up to request clarification or add additional context in comments.

Comments

1

You function is not created because the SELECT statement:

SELECT FirstName,LastName,HomePhone 
FROM dbo.Employees 
WHERE EmployeeID = @employId
GROUP BY EmployeeID

is incorrect (it inludes 3 fields in the SELECT and one, completely different in GROUP BY).

In fact you don't even need a GROUP BY, since EmployeeID is a unique value. Just create your function as

CREATE FUNCTION dbo.Employee_Name(@employId int)
RETURNS TABLE
AS 
RETURN(
    SELECT FirstName,LastName,HomePhone 
    FROM dbo.Employees 
    WHERE EmployeeID = @employId
)

then your SELECT * FROM dbo.Employee_Name(5) will work.

And yes as @KekuSemau pointed out, either run CREATE FUNCTION and your SELECT * ... as separate queries or insert GO between them - CREATE has to be in a separate batch.

1 Comment

Indeed, I didn't even see that GROUP BY at all!
-1

Try This:

CREATE FUNCTION dbo.Employees_Name(@employeeID int)
Returns Table
AS
Return(
   SELECT FirstName,LastName,PhoneNumber
   FROM dbo.Employees
   WHERE EmployeeID = @employID
   )

   GO
   SELECT * FROM dbo.Employees_Name(5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.