Function should have input parameters?

3.8k views Asked by At

Is it mandatory to pass input parameters for all the user defined functions?

We know, Stored procedure has both input and output parameters. Function has only input parameters.

We can write a stored procedure without using these parameters too.. Is it possible to write the user defined function without input parameter?

3

There are 3 answers

0
dotnetstep On BEST ANSWER

Yes you can definitely write User defined function without parameter.

One more thing I want to clarify that function may have input parameter and it has return value. Return value would be scalar or table depend on type of function you are creation.

0
Solomon Rutzky On

Why ask if it is possible when you can just type a few lines and see that it is possible ;-)

CREATE FUNCTION dbo.NoParamsUDF()
RETURNS NVARCHAR(50)
AS
BEGIN
  RETURN N'It worked!';
END;
GO

CREATE FUNCTION dbo.NoParamsTVF()
RETURNS TABLE
AS RETURN
  SELECT dbo.NoParamsUDF() AS [DidItWork?];
GO

SELECT * FROM dbo.NoParamsTVF();

Returns:

DidItWork?
-------------
It worked!

0
AudioBubble On

CREATE FUNCTION dbo.NoParamsUDF() RETURNS VARCHAR(50) AS BEGIN RETURN N'It worked!'; END; GO

CREATE FUNCTION dbo.NoParamsTVF() RETURNS TABLE AS RETURN SELECT dbo.NoParamsUDF() AS [DidItWork?]; GO

SELECT * FROM dbo.NoParamsTVF();