|
--Use custom functions
create table a (departure place varchar (10), receiving place varchar (10))
insert into a
select 'Beijing', 'Nanjing'
union all select 'Nanjing', 'Shanghai'
union all select 'Shanghai', 'Beijing'
go
create table b (id int, name varchar (10))
insert into b
select 1, 'Shanghai'
union all select
2, 'Nanjing'
union all select
3, 'Beijing'
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 (departure place) as departure place, dbo.getid (receive place) as receive place from a
drop function getid
drop table a
drop table b
--result
Place of departure
3 2
twenty one
1 3 |
|