如何创建接受[[customerID]]并返回CustName的函数请查看详细信息

时间:2016-04-26 17:04:46

标签: sql sql-server-2008 tsql

我被要求创建一个函数来接受CustomerID并返回CustomerID的CustomerID,我是新的学生/开发人员请问如果问题不明确请告诉我,以便我可以添加更多有关它的详细信息,但那是什么我被问到了。

1 个答案:

答案 0 :(得分:1)

SQL中的

functions有三种类型。注册其余的CLR函数......

create table test
(
id int,
name varchar(4)
)

insert into test
select 1,'abc'
union all
select 2,'cde'

1. 标量函数取一个值并返回一个值

现在对于上表,您可以创建如下的标量函数

create function dbo.test
(
@id int
)
returns varchar(4)
as
begin
declare @name varchar(4)
select @name=name from test where id =@id
 return @name
End

您可以调用它:

select  dbo.test(1)

2. 内联表值函数:采用与标量函数相同的单个输入并返回表

create function dbo.test
(
@id int
)
as 
returns TABLE
(
select * from test where id=@id)

您可以调用它:

从dbo.test(1)中选择*

3. 多表值函数

create function dbo.test
(
@id int
)
returns 
@test table
(
id int,
name varchar(4)
)
as
begin

insert into @test
select * from test where id =@id

return

end

你调用它就像: 从dbo.test(1)

中选择*

拿任何一本Itzik Ben Gan书籍开始学习SQL应该学习的方式