【发布时间】:2017-07-03 16:59:11
【问题描述】:
出于某种原因,我只需要一个查询即可完成我的项目。
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)
但它不起作用。那么如何结合“insert into (select...)”呢? 感谢您的时间。 :)
更新(这是我创建的表格)
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 相互连接
当新员工加入公司时。我们需要所有人注册他(她)的所有信息。这就是为什么我想加入两个表作为一个结果并使用一个语句来添加所有信息。
【问题讨论】:
-
您的第一个查询不会生成表,它会返回结果集。为什么要插入结果?我怀疑你想插入
employee和person,对吧? -
你可以试试这种格式吗? INSERT INTO "TABLENAME" ("COLUMNNAME1","COLUMNNAME2") SELECT T1."COLUMNNAME1",T2."COLUMNNAME2" FROM T1 LEFT JOIN T2 ON
-
如果您想将两个结果集合并为一个结果集,请尝试
Union ALL作为下一个代码:-select empId, Name, position, sinNum, age, phoneNum from employee join person on employee.sinNum = person.sinNum Union all Select 01,'Colin','clerk',909,20,345678 -
是的,我想插入员工和人员表。但它们有一些共同点,我不想将相同的值插入重复的列中
-
您必须分两步完成,您不能使用 1 个 INSERT 语句将数据插入到 2 个表中
标签: sql sql-server