Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a function without a parameter in t-sql?

Tags:

tsql

I am being forced to put a @noparameter varchar(1)=null in this function? How do I create a function to return a table which has no parameters!


ALTER FUNCTION [dbo].[DropDownIndividuals](@noparameter varchar(1)=null)
RETURNS
    @IndividualsList TABLE(
          Case_Number VARCHAR(11)
         ,LastName    VARCHAR(100)
         ,FirstName   VARCHAR(100)
         ,Midlename   VARCHAR(100)
         ,FullName    VARCHAR(100) 
        )
AS  
BEGIN
       INSERT INTO @IndividualsList
       SELECT DISTINCT
       cast(Case_Number as varchar(10))as Case_Number
      ,[Lastname]
      ,[Firstname]
      ,[Middlename]
      ,rtrim([Lastname]+ ' '+ [Firstname]) as FullName
      FROM [MHMODSSQL1P].[ODS].[dbo].[Contact]
      WHERE [LastName] is not null and [FirstName] is not null
      UNION ALL SELECT null,null,null,null,null
      ORDER BY [LastName]

      RETURN 

END;
like image 795
hidden Avatar asked Feb 14 '11 17:02

hidden


People also ask

Can a function have no parameters SQL?

How to Create a Function Without a Parameter in SQL. Line one creates a function then named the function “YTDSALES()”. You can give your function any name. Remember to add parenthesis to the name of the function when without a parameter.

Can we have stored procedure without parameters?

The simplest kind of SQL Server stored procedure that you can call is one that contains no parameters and returns a single result set. The Microsoft JDBC Driver for SQL Server provides the SQLServerStatement class, which you can use to call this kind of stored procedure and process the data that it returns.

Are parameters mandatory to create a UDF?

Yes you can definitely write User defined function without parameter.

How do you call a function with out parameters in SQL?

1) You cannot call a PROCEDURE from SQL only a function. You have the above defined as a function but it doesn't return anything sensible. 2) you cannot call a function that has an OUT parameter -- only in parameters are allowed in SQL. Your solution is easy -- use a function that returns the number.


2 Answers

FUNCTION [dbo].[DropDownIndividuals]()

You can just use open and close brackets to define function without parameters, Assuming you are using SQL server.

like image 175
Sachin Shanbhag Avatar answered Sep 28 '22 03:09

Sachin Shanbhag


You should be a able to do it with something like this:

ALTER FUNCTION [dbo].[DropDownIndividuals]()

But since a table-valued function is essentially a parameterised view, you might as well just use a view rather than a TVF with no parameters:

CREATE VIEW [dbo].[DropDownIndividuals]
AS
SELECT -- etc
like image 39
LukeH Avatar answered Sep 28 '22 04:09

LukeH