|
发表于 2020-3-31 23:45:01
|
显示全部楼层
--使用自定义函数
create table a(出发地 varchar(10),接收地 varchar(10))
insert into a
select '北京','南京'
union all select '南京','上海'
union all select '上海','北京'
go
create table b(id int ,name varchar(10))
insert into b
select 1 , '上海'
union all select
2, '南京'
union all select
3 ,'北京'
go
create function getid(@name varchar(10))
returns int
as
begin
declare @id int
select @id=id from b where name=@name
return @id
end
go
select dbo.getid(出发地) as 出发地,dbo.getid(接收地) as 接收地 from a
drop function getid
drop table a
drop table b
--结果
出发地 接收地
3 2
2 1
1 3 |
|