【问题标题】:How to insert values into a sub select query for MS SQL?如何将值插入 MS SQL 的子选择查询?
【发布时间】: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 相互连接

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

【问题讨论】:

  • 您的第一个查询不会生成表,它会返回结果集。为什么要插入结果?我怀疑你想插入employeeperson,对吧?
  • 你可以试试这种格式吗? 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


【解决方案1】:

嗨,杰弗里 -

选择查询返回结果集而不是表。因此,如果您想在结果集中再插入一行,则可以使用临时表或 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 

谢谢:)

【讨论】:

  • 我试过你的方法,可以添加一个人,但无法添加另一个人。
  • 您可以根据新行使用多个 UNION,例如 - UNION Select 'meng','xue',333,10,'clerk',3000,'male',30,'j@ tt.com',2321 UNION Select 'meng','xue',333,10,'clerk',3000,'男',30,'j@tt.com',2321 等等..
  • 那是相当可观的计划。但是如果我想添加很多人,那么它需要很多“联合”对吗?
  • 绝对需要大量的 UNION。如果你有很多这样的记录,那么将这些记录插入一个表中,然后使用单个 UNION 从新表中使用单个选择查询。
【解决方案2】:

对于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);

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

【讨论】:

  • 当我关闭与数据库的连接时,临时表会被破坏吗?
  • @Jeffery 正确。这就是为什么它是一个临时表。它存在时保存在临时数据库中,并在您断开连接时自动删除。所以它对于测试很有用,或者填充它然后用它来处理那些数据。例如对真实表的更新。但是表变量也可以用于此目的。
【解决方案3】:

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

【讨论】:

  • OP 询问如何使用单个语句插入两个现有表
  • 他说,要插入新表(从第一个查询结果集创建)。他的 cmets “上面将生成一个结果表,我想将值插入到这个新表中”
  • 再次阅读问题和 cmets。 Yes, I want to insert into employee and person table. But they have something in common and I don't want to insert same values into duplicate column。我会说 OP 误解了 SQL 并试图 hack 插入语句
  • 是的,请单独声明。因为我用 GUI 创建了一个 java 程序,我想通过 JDBC 与数据库交互。我将让用户填写所有空白文本字段并生成一个查询,因此当我单击添加按钮时,它会将其发送到数据库。然后在Jtable上显示结果
  • @Jeffery,如果总是需要合并单行数据,请检查我更新的答案。
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 2021-08-12
  • 2016-01-28
  • 2012-07-30
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多