日期:2014-05-17 浏览次数:20487 次
--这样?
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