I found some errors in your code that you can check below
REMARKS
In your description you placed the call to the function with the name RealEstatudf_eAgentSales and it should really be udf_RealEstateAgentSales according to your example.
You must assign the size for the parameters of type NVARCHAR. They were without size and I assigned a 25 for example.
The COUNT aggregate function, you must include it in your query and you can avoid having to create a variable.
The order in the WHERE section should be changed. First the field and then the parameter. However, it is not a mistake as you currently have it, but it is a bad practice.
I leave you the code I used, the enhanced function and some examples
Code:
--Purchases
IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Purchases]') AND TYPE IN (N'U'))
BEGIN
DROP TABLE Purchases
END
CREATE TABLE Purchases
(
PurchaseID INT IDENTITY
, DealMadeByEmployeeID INT
, PurchaseValue MONEY
)
INSERT INTO Purchases ( DealMadeByEmployeeID, PurchaseValue )
VALUES ( 1, 250.15 )
, ( 2, 15.50 )
, ( 1, 100 )
, ( 1, 300.15 )
, ( 2, 500.15 )
SELECT *
FROM Purchases
--Employees
IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Employees]') AND TYPE IN (N'U'))
BEGIN
DROP TABLE Employees
END
CREATE TABLE Employees
(
EmployeeID INT IDENTITY
, FirstName VARCHAR(50)
, SecondName VARCHAR(50)
, LastName VARCHAR(50)
)
INSERT INTO Employees ( FirstName, SecondName, LastName )
VALUES ( 'J', 'E', 'P' )
, ( 'Mitko', '', 'Z' )
SELECT *
FROM Employees
GO
Now the function should be like this:
--FUNCTION
DECLARE @strSQL nvarchar(1000)
IF EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].[udf_RealEstateAgentSales]'))
BEGIN
SET @strSQL = 'DROP FUNCTION [dbo].[udf_RealEstateAgentSales]'
EXEC sp_executesql @strSQL
END
GO
CREATE FUNCTION udf_RealEstateAgentSales
(@FirstName NVARCHAR(25) -- Missed length
, @SecondName NVARCHAR(25) -- Missed length
, @LastName NVARCHAR(25) -- Missed length
)
RETURNS @salesAmoutAndAgent TABLE (
FirstName NVARCHAR(25)
, SecondName NVARCHAR(25)
, LastName NVARCHAR(25)
, sales INT
)
AS
BEGIN
INSERT @salesAmoutAndAgent
SELECT @FirstName
, @SecondName
, @LastName
, COUNT(*) AS Sales
FROM Purchases AS P
INNER JOIN Employees AS E
ON P.DealMadeByEmployeeID = E.EmployeeID
WHERE FirstName = @FirstName
AND SecondName = @SecondName
AND LastName = @LastName
RETURN
END
GO
a. Employee somename
SELECT * FROM dbo.udf_RealEstateAgentSales('somename','somename','somename')
->
FirstName SecondName LastName sales
------------------------- ------------------------- ------------------------- -----------
somename somename somename 0
(1 row(s) affected)
b. Employee J E P
SELECT * FROM dbo.udf_RealEstateAgentSales('J','E','P');
->
FirstName SecondName LastName sales
------------------------- ------------------------- ------------------------- -----------
J E P 3
(1 row(s) affected)
c. Employee Mitko Z
SELECT * FROM dbo.udf_RealEstateAgentSales('Mitko','','Z');
->
FirstName SecondName LastName sales
------------------------- ------------------------- ------------------------- -----------
Mitko Z 2
(1 row(s) affected)
I hope you find it useful
varcharvariables and parameters that you use. If you use onlyNVARCHAR- then you get a string of EXACTLY 1 character length which is usually not what you want..... provide a reasonable length for your parameters!