Tuesday 2 April 2013

SQL Function Example

 

----------- Scaler Function -------

create function udfSquer(@num1 int)
returns int
as
begin
declare @result int;
set @result = @num1 * 5
return @result

end

select dbo.udfSquer(5)

------------- Table Valued Fucntion ----

create function udfTable(@num1 int)
returns @result table ( Id int)
as
begin
declare @start int =1;
declare @end int =10;
while @start <= @end
begin
insert into @result values (@num1 * @start)
set @start = @start + 1;
end
return
end

select * from dbo.udfTable(15)

No comments:

Post a Comment