场景一:
创建一个返回"Hello World!"字符串的函数。
PL/SQL:
create or replace function get_hello_msg
return varchar2 as
begin
return 'Hello World!';
end get_hello_msg;
/
执行:
select get_hello_msg msg from dual;
T-SQL:
create function dbo.fnGetHelloMsg()
returns varchar(50)
as
begin
return 'Hello World!'
end
go
执行:
select dbo.fnGetHelloMsg() msg
看以看出:
T-SQL中的函数必须要有括号,不然会报错。
场景二:
计算工资所得税(带有参数)
PL/SQL:
create or replace function get_tax(p_salary number)
return number as
begin
declare tax_salary number;
begin
tax_salary := p_salary - 2000;
if tax_salary<=0 then
return 0;
end if;
if tax_salary<=500 then
return tax_salary*5/100;
end if;
if tax_salary<=2000 then
return tax_salary*10/100 - 25;
end if;
if tax_salary<=5000 then
return tax_salary*15/100 - 125;
end if;
if tax_salary<=20000 then
return tax_salary*20/100 - 375;
end if;
if tax_salary<=40000 then
return tax_salary*25/100 - 1375;
end if;
if tax_salary<=60000 then
return tax_salary*30/100 - 3375;
end if;
end;
end get_tax;
执行:
select get_tax(6000) tax
from dual;