Function SQL

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

MS SQL

scaler value functions


CREATE FUNCTION dbo.CalculateTotalDaysForProject(@DevelopmentDays int, @Testin
gDays int)
RETURNS int
AS
BEGIN
DECLARE @totalcost int;
SELECT @ totalcost = @DevelopmentDays + @ TestingDays;
RETURN @totalcost;
END

Table value functions

CREATE FUNCTION GeneratedSales.SalesByPerson(@sales_ID int)


RETURNS @SalesData TABLE
(
[CustomerID] int,
[SalesAmount] int,
[SalesDate] date
)
AS
BEGIN
INSERT INTO @SalesData
SELECT Sales.CustomerID, Sales.SalesAmount, Sales.SalesDate from Sales
Where Sales.Sales_ID = @sales_ID
RETURN
END
Scalar udf

2. CREATE FUNCTION whichContinent


(@Country nvarchar(15))
RETURNS varchar(30)
AS
BEGIN
declare @Return varchar(30)
select @return = case @Country
when 'Argentina' then 'South America'
when 'Belgium' then 'Europe'
when 'Brazil' then 'South America'
when 'Canada' then 'North America'
when 'Denmark' then 'Europe'
when 'Finland' then 'Europe'
when 'France' then 'Europe'
else 'Unknown'
end
return @return
end

print dbo.WhichContinent('USA')
select dbo.WhichContinent(Customers.Country), customers.*
from customers
create table test
(Country varchar(15),
Continent as (dbo.WhichContinent(Country)))
insert into test (country)
values ('USA')
select * from test
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Country Continent
--------------- ------------------------------
USA North America

You might also like