如何将值插入MS SQL的子选择查询?

时间:2017-07-03 07:32:38

标签: sql sql-server

出于某种原因,我只需要一个查询来完成我的项目。

select empId, lastName,firstName,employee.sinNum,departmentId,position,baseSalary,gender,age,emailAddr,phoneNum from employee join person
on employee.sinNum = person.sinNum

上面会生成一个结果集,我想在这个结果集中插入值。

Insert into (empId, lastName,firstName,employee.sinNum,departmentId,position,baseSalary,gender,age,emailAddr,phoneNum from 
employee join person on employee.sinNum = person.sinNum) values 
('meng','xue',333,10,'clerk',3000,'male',30,'j@tt.com',2321)

但它不起作用。那么如何将"插入到(选择...)"? 感谢您的时间。 :)

已更新(以下是我创建的表格)

create table person (
sinNum int primary key not null,
gender varchar(6) not null check (gender in ('male','female')) default 
'female',age int not null check (age>=18 and age<=100),
emailAddr varchar (50) not null,
phoneNum int not null,  
)

create table employee (
empId int identity (1,1) unique,
lastName varchar (30) not null,
firstName varchar (30) not null,
sinNum int not null unique foreign key references person (sinNum),
departmentId int not null foreign key references department (departmentId), 
position varchar (20) not null check (position in 
('clerk','assistant','supervisor','manager','director','president')) default 
'clerk',
baseSalary float not null
)

一个人应该有员工信息和个人信息。这两个表通过sinNum

相互连接

当新员工加入公司时。我们都需要注册所有(她)的信息。这就是为什么我想加入两个表作为一个结果并使用一个语句来添加所有信息。

3 个答案:

答案 0 :(得分:1)

对于select into,您需要告诉它需要在哪个表中插入结果。最好还有列名,以便在表中添加一个额外的列不会使sql无效。

例如:

insert into mytable (empId, Name, position, sinNum, age, phoneNum)
select e.empId, Name, position, e.sinNum, age, phoneNum 
from employee e
join person p on e.sinNum = p.sinNum;

或只是值:

insert into mytable (empId, Name, position, sinNum, age, phoneNum)
values (1,'Colin','clerk',909,20,345678);

这要求目标表已存在。

如果要将其插入例如新的临时表中,则可以使用select into语法。哪个会创建临时表。

select e.empId, Name, position, e.sinNum, age, phoneNum 
into #mytemptable
from employee e
join person p on e.sinNum = p.sinNum;

-- the temp table already exists, so we can insert some extra values to it
insert into #mytemptable (empId, Name, position, sinNum, age, phoneNum) values
(1,'Colin','clerk',909,20,345678),
(2,'Hazel','boss',1001,50,876543);

临时表仅在会话处于活动状态时存在。

答案 1 :(得分:1)

HI Jeffery -

选择查询返回结果集而不是表。因此,如果要在结果集中再插入一行,则可以使用临时表或UNION。像这样 -

select empId, lastName,firstName,employee.sinNum,departmentId,position,baseSalary,gender,age,emailAddr,phoneNum 
from employee join personon ON employee.sinNum = person.sinNum 
UNION 
Select 'meng','xue',333,10,'clerk',3000,'male',30,'j@tt.com',2321 

谢谢:)

答案 2 :(得分:0)

Select Into创建一个物理表,如果您不想创建物理表,则使用@table变量,如下所示:

DECLARE @tmpEmployee TABLE
(  
  empId int, [Name] varchar(100), position varchar(100), sinNum int, age int , phoneNum int
)

INSERT INTO @tmpEmployee
select empId, Name, position, sinNum, age, phoneNum from employee join person
on employee.sinNum = person.sinNum

INSERT INTO @tmpEmployee (empId, Name, position, sinNum, age, phoneNum)
VALUES (01,'Colin','clerk',909,20,345678)

Select * from @tmpEmployee

更新

如果要合并的行始终是单行已知值,则可以使用以下方法结束单个队列:

SELECT empId, Name, position, sinNum, age, phoneNum from employee join person
on employee.sinNum = person.sinNum
UNION ALL
SELECT empid=01,Name='Colin',position='clerk',sinName=909,age=20,phoneNum=345678