我想用存储过程把从多个表取出的数据存到一个临时表中 再显示出来
比如说employee表中有id,name,age。department表中有did,dname
我想把employee表中的name,age和department表中的did,dname取出来放到一个表中再显示出来。
实际数据比这个麻烦,只是举个简单例子,希望大家帮帮忙

------解决方案--------------------
--这样?
CREATE TABLE #temp(col1 NVARCHAR(max), col2 NVARCHAR(max))
INSERT #temp(col1, col2)
SELECT name, age FROM employee
INSERT #temp(col1, col2)
SELECT dname, did FROM department
SELECT * FROM #temp
------解决方案--------------------
是不是这样的,你看看:
create table department
(did int,name varchar(30))
insert into department
select 1,'开发部'
union all
select 2,'销售部'
union all
select 3,'实施部'
--did是这个员工所属的部门id
create table employee
(id int,name varchar(20),age int,did int)
insert into employee
select 1,'张三',22,1
union all
select 2,'李四',23,2
union all
select 3,'王五',25,3
--在存储过程中只要这样,就可以,临时表的名称为#temp_table
select e.name,
e.age,
d.name as department_name,
d.did
into #temp_table --这是sql server中的一种写法,
--可以直接创建表,同时表数据插入到表中
from employee e
left join department d
on e.did = d.did
select *
from #temp_table