--测试数据
create table ta(b int, c int, id int)
create table tb(d int, e int, id int)
insert ta select 1, 22, 3
union all select 2, 2, 4
insert tb select 2, 3, 3
go
--创建存储过程
/*我觉得没有必要写成存储过程,一条语句就可以了。
或者你把它定义成视图也行*/
create proc proc_temp
as
select ta.*, d=isnull(d, 0), e=isnull(e,0)
from ta left join tb
on ta.id=tb.id
go
exec proc_temp
--清除
drop proc proc_temp
drop table ta
drop table tb
create table ta(b int, c int, id int)
create table tb(d int, e int, id int)
insert ta select 1, 22, 3
union all select 2, 2, 4
insert tb select 2, 3, 3
go
--创建存储过程
/*我觉得没有必要写成存储过程,一条语句就可以了。
或者你把它定义成视图也行*/
create proc proc_temp
as
select ta.*, d=isnull(d, 0), e=isnull(e,0)
from ta left join tb
on ta.id=tb.id
go
exec proc_temp
--清除
drop proc proc_temp
drop table ta
drop table tb
declare @a table(b int,c int,id int)
insert @a values(1,22,3)
insert @a values(2,2,4)
--select * from @adeclare @b table(d int,e int,id int)
insert @b values(2,3,3)--select * from @bselect a.*,isnull(b.d,0) as d,isnull(b.e,0) as e from @a a left join @b b on a.id=b.id
它的关键是使用了.left join 楼主,你看看帮助就会明白了.
select ta.*,tb.d,tb.e from ta left join tb on ta.id = tb.id
我还不会删除存储呢??谢谢指点.
drop proc proc_temp