Im not sure whether im in right place to ask this question. But I wonder How a function that i created fall under the table valued function? why cant it be inside scalar function?
I have the below function recorded in a Database.
IF OBJECT_ID (N'dbo.ufnGetTVP', N'FN') IS NOT NULL
DROP FUNCTION ufnGetTVP;
GO
CREATE FUNCTION dbo.ufnGetTVP(@input varchar(50))
returns @mt table (a nvarchar(60))
AS
-- Returns the stock level for the product.
BEGIN
declare @output varchar(1)
declare @len int=(select len(@input))
while(@len>0)
begin
set @output =substring(@input,0,2)
set @input=substring(@input,charindex(@output,@input)+2,len(@input))
SET @len=len(@input)
insert into @mt
Select @OUTPUT
end
return
END;
GO
if a function will be saved as TVF how a scalar function can created?
Please share your thoughts.
As I wrote in my comment - the difference between a Scalar function and a Table valued function is only the return type of the function.
A scalar valued function is a function that returns a scalar (or single) value such as int, varchar, bit etc`, while a table valued function is a function that returns a table.
The basic syntax of creating a scalar functions is the same as the one for creating a table function:
CREATE FUNCTION <name>
(
<parameters list>
)
RETURNS <scalar type>
AS
BEGIN
-- function code goes here
END;