[英]SQL - Insert new row into an existing table based on another table entry
I have a company table and a licence table. 我有公司表和许可证表。 I need to insert a new row in the license table for every company in the company table that isn't already in the license table.
我需要在许可证表中为公司表中尚未包含在许可证表中的每个公司插入一个新行。
License (ID,CompanyID,LicenseStart,LicenseEnd,CreatedDate,AddUser,UpdateUser,UpdateDate,TemplateId)
The ID in this table is incremented by 1 when a new row is added. License (ID,CompanyID,LicenseStart,LicenseEnd,CreatedDate,AddUser,UpdateUser,UpdateDate,TemplateId)
添加新行时,此表中的ID增加1。
Company (ID,Name,CreateDate,CState,LocationID,LegalName)
The default value that would be entered for each CompanyID that isn't already in the license table should look something like this. 将为许可证表中尚未包含的每个CompanyID输入的默认值应如下所示。
Insert (ID, @theCompanyID, GetDate(), DATEADD(day,14,GETDATE()), null,null,null,null null)
@theCompanyID
would be the CompanyID that isn't in the license table @theCompanyID
将是不在许可证表中的CompanyID
I am very new to this so any help would be appreciated. 我是新手,所以任何帮助将不胜感激。
license.id
is an identity column, so you do not need to insert it. license.id
是一个标识列,因此您无需插入它。
insert into license (CompanyID, LicenseStart, LicenseEnd)
select c.id, GetDate(), DATEADD(day, 14, GETDATE())
from company c
where not exists (select 1
from license l
where c.ID = l.CompanyID
);
You also don't need to insert explicit NULL
values for columns where you are not supplying values. 您也不需要为不提供值的列插入显式
NULL
值。 The default is to set these values to NULL
. 默认设置是将这些值设置为
NULL
。
If your start and end dates do not have a time component -- just the date -- then use this instead: 如果您的开始日期和结束日期没有时间组件 - 只有日期 - 那么请改用:
select c.id, cast(GetDate() as date), cast(DATEADD(day, 14, GETDATE()) as date)
from company c
where not exists (select 1
from license l
where c.ID = l.CompanyID
);
Do an INSERT
with SELECT
, use NOT EXISTS
to make sure no existing company is inserted again. 使用
SELECT
执行INSERT
,使用NOT EXISTS
确保不再插入现有公司。 Something like: 就像是:
insert into license (ID,CompanyID,LicenseStart,LicenseEnd,CreatedDate,AddUser,UpdateUser,UpdateDate,TemplateId)
select ID, theCompanyID, GetDate(), DATEADD(day,14,GETDATE()), null,null,null,null null
from company
where not exists (select 1 from company
where company.CompanyID = license.CompanyID)
INSERT INTO License (CompanyID, LicenseStart, LicenseEnd)
SELECT ID, GetDate(), DATEADD(day,14,GETDATE())
FROM Company
WHERE ID NOT IN
(
SELECT DISTINCT CompanyID
FROM License
)
You can do it with LEFT JOIN
: 您可以使用
LEFT JOIN
执行此操作:
INSERT INTO License
( CompanyID ,
LicenseStart ,
LicenseEnd ,
CreatedDate ,
AddUser ,
UpdateUser ,
UpdateDate ,
TemplateId
)
SELECT ID ,
GETDATE() ,
DATEADD(DAY, 14, GETDATE()) ,
NULL ,
NULL ,
NULL ,
NULL ,
NULL
FROM Company c
LEFT JOIN License l ON l.CompanyID = c.ID
WHERE l.ID IS NULL
An alternative way to do is to use the EXCEPT
operator which is faster than subquery
or join
owing to it being a set function. 另一种方法是使用
EXCEPT
运算符,它比subquery
或join
更快,因为它是一个set函数。
Insert License
select a.CompanyID, GetDate(), DATEADD(day,14,GETDATE()), null,null,null,null null
from
(select id from company
except
select CompanyID from License
)a
Since OP has said that the ID
in the license table is incremented by 1 then you can write your query like this: 由于OP已经说过许可证表中的
ID
增加1,所以你可以这样编写你的查询:
INSERT INTO License(CompanyID,LicenseStart,LicenseEnd)
SELECT c.ID, GetDate(), DATEADD(day,14,GETDATE()))
FROM Company c
WHERE NOT EXISTS
(
SELECT l.CompanyID
FROM License l
WHERE l.CompanyID = c.ID
)
You do not have to specify the column in the INSERT
statement if the column is automatically incremented. 如果列自动递增,则不必在
INSERT
语句中指定列。 Also I chose to use NOT EXISTS
instead of NOT IN
in the `WHERE clause for the reason that Martin Smith specified in this Stack Overflow question . 此外,我选择在`WHERE子句中使用
NOT EXISTS
而不是NOT IN
,原因是Martin Smith在此Stack Overflow问题中指定的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.