I've tried every way to do this, but I can't. What I want is just to pass a number that would be an Id of a client and that I execute the function bringing me from the DB according to that Id (in the where)
The objective is:
"Create a function that reports the number of checks a given customer has had."
Here are some tables:
Chequeo Autos Cliente MarcaA
------- ------- ---------- --------
IdChequeo IdAuto IdCliente IdMarca
IdParte Matricula Nombre Nombre
IdMecanic IdAgen Cedula Modelo
Fecha NoPuertas Telefono Pais
IdAuto Color Direccion IdAuto
IdCliente
CREATE FUNCTION CHEQUEO(@IdClient int)
return INT
AS
BEGIN
SELECT Cliente.IdCliente,
Cliente.Nombre,
COUNT(Autos.IdCliente) AS Chequeos_Realizados
FROM Cliente
INNER JOIN Autos ON Cliente.IdCliente = Autos.IdCliente
INNER JOIN Chequeo ON Autos.IdAuto = Chequeo.IdAuto
INNER JOIN MarcaA ON Autos.IdAuto = MarcaA.IdAuto
WHERE Cliente.IdCliente = @IdClient
GROUP BY Cliente.Nombre,Cliente.IdCliente,Autos.IdCliente
END
I don't know what else to do, what is missing. I made the select statement with pure struggle. How do I pass the parameter?
The SELECT statement by itself works and does what I want, as long as I remove all that "function" stuff from it.
First, ask yourself some things that will help you: It's not return, it's returns. And it is not of type int since your query does not return a single integer value, but a lot of data. And in the end you failed to return the result.
Another thing that is also important to highlight is that if it returns multiple values (in the form of a table) the function is no longer scalar, but a table or inline table function, when changing the type of the function its structure changes. I leave you your example as an inline table function (of course, assuming what you say that the query works correctly) :