Search notes:

SQL-Server / T-SQL: User defined functions

Basic structure of a user defined function

if object_id('dbo.tq84_repeat', 'fn') is not null
   drop function dbo.tq84_repeat;
go

create function dbo.tq84_repeat (
   @pattern varchar(max),
   @count   integer
)
returns varchar(max)
as
begin
   declare
     @ret varchar(max) = '';

     while @count > 0 begin
        set @ret   = @ret + @pattern;
        set @count = @count - 1
     end;

    return @ret;
end;
go

print(dbo.tq84_repeat('* ', 10));
Github repository about-MSSQL, path: /t-sql/user-defined-functions/basic.sql

Table valued functions

A function can return a single rowset.
Such functions are known as table valued functions.

DML statements

Apparently, a user defined function cannot modify data in a database (execute DML statements).
Thus, instead of putting the DML statement into a function, a procedure might be created that uses output parameters.

See also

(User defined) stored procedure
The scalar UDF inlining feaure (IQP Features)
The MS-Access method OpenFunction of the object DoCmd.

Index