create table #test (a int identity(1,1), b varchar(20), c varchar(20))
insert into #test (b,c) values ('bvju','hjab')
insert into #test (b,c) values ('bst','sdfkg')
......
insert into #test (b,c) values ('hdsj','kfsd')
如何将从上述插入语句填充的标识值(#test. a
)插入到#example
表(另一个表)中
create table #sample (d int identity(1,1), e int, f varchar(20))
insert into #sample(e,f) values (identity value from #test table, 'jkhjk')
insert into #sample(e,f) values (identity value from #test table, 'hfhfd')
......
insert into #sample(e,f) values (identity value from #test table, 'khyy')
任何人都可以解释一下我如何为更大的记录集(数千条记录)实现它?
我们可以使用while
循环和scope_identity
?如果是,请解释我们如何做到这一点?
如果我从选择查询插入到#test中,会是什么情况?
插入到#test(b,c)中从…(数千条记录)中选择
我如何捕获标识值并将该值用于另一个(#sample) insert into #sample(e,f) select(来自#test的标识值),...从....(数千条记录)—
insert into #test (b,c) values ('bvju','hjab')
insert into #sample(e,f) values (@SCOPE_IDENTITY(), 'jkhjk')
@SCOPE_IDENTITY()返回最后使用的标识值
我刚刚用输出子句编写了一个“基于集合”的示例。
在这里。
IF OBJECT_ID('tempdb..#DestinationPersonParentTable') IS NOT NULL
begin
drop table #DestinationPersonParentTable
end
IF OBJECT_ID('tempdb..#DestinationEmailAddressPersonChildTable') IS NOT NULL
begin
drop table #DestinationEmailAddressPersonChildTable
end
CREATE TABLE #DestinationPersonParentTable
(
PersonParentSurrogateIdentityKey int not null identity (1001, 1),
SSNNaturalKey int,
HireDate datetime
)
declare @PersonOutputResultsAuditTable table
(
SSNNaturalKey int,
PersonParentSurrogateIdentityKeyAudit int
)
CREATE TABLE #DestinationEmailAddressPersonChildTable
(
DestinationChildSurrogateIdentityKey int not null identity (3001, 1),
PersonParentSurrogateIdentityKeyFK int,
EmailAddressValueNaturalKey varchar(64),
EmailAddressType int
)
-- Declare XML variable
DECLARE @data XML;
-- Element-centered XML
SET @data = N'
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person>
<SSN>222222222</SSN>
<HireDate>2002-02-02</HireDate>
</Person>
<Person>
<SSN>333333333</SSN>
<HireDate>2003-03-03</HireDate>
</Person>
<EmailAddress>
<SSNLink>222222222</SSNLink>
<EmailAddressValue>g@g.com</EmailAddressValue>
<EmailAddressType>1</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>222222222</SSNLink>
<EmailAddressValue>h@h.com</EmailAddressValue>
<EmailAddressType>2</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>333333333</SSNLink>
<EmailAddressValue>a@a.com</EmailAddressValue>
<EmailAddressType>1</EmailAddressType>
</EmailAddress>
<EmailAddress>
<SSNLink>333333333</SSNLink>
<EmailAddressValue>b@b.com</EmailAddressValue>
<EmailAddressType>2</EmailAddressType>
</EmailAddress>
</root>
';
INSERT INTO #DestinationPersonParentTable ( SSNNaturalKey , HireDate )
output inserted.SSNNaturalKey , inserted.PersonParentSurrogateIdentityKey into @PersonOutputResultsAuditTable ( SSNNaturalKey , PersonParentSurrogateIdentityKeyAudit)
SELECT T.parentEntity.value('(SSN)[1]', 'INT') AS SSN,
T.parentEntity.value('(HireDate)[1]', 'datetime') AS HireDate
FROM @data.nodes('root/Person') AS T(parentEntity)
/* add a where not exists check on the natural key */
where not exists (
select null from #DestinationPersonParentTable innerRealTable where innerRealTable.SSNNaturalKey = T.parentEntity.value('(SSN)[1]', 'INT') )
;
/* Optional. You could do a UPDATE here based on matching the #DestinationPersonParentTableSSNNaturalKey = T.parentEntity.value('(SSN)[1]', 'INT')
You could Combine INSERT and UPDATE using the MERGE function on 2008 or later.
*/
select 'PersonOutputResultsAuditTable_Results' as Label, * from @PersonOutputResultsAuditTable
INSERT INTO #DestinationEmailAddressPersonChildTable ( PersonParentSurrogateIdentityKeyFK , EmailAddressValueNaturalKey , EmailAddressType )
SELECT par.PersonParentSurrogateIdentityKeyAudit ,
T.childEntity.value('(EmailAddressValue)[1]', 'varchar(64)') AS EmailAddressValue,
T.childEntity.value('(EmailAddressType)[1]', 'INT') AS EmailAddressType
FROM @data.nodes('root/EmailAddress') AS T(childEntity)
/* The next join is the "trick". Join on the natural key (SSN)....**BUT** insert the PersonParentSurrogateIdentityKey into the table */
join @PersonOutputResultsAuditTable par on par.SSNNaturalKey = T.childEntity.value('(SSNLink)[1]', 'INT')
where not exists (
select null from #DestinationEmailAddressPersonChildTable innerRealTable where innerRealTable.PersonParentSurrogateIdentityKeyFK = par.PersonParentSurrogateIdentityKeyAudit AND innerRealTable.EmailAddressValueNaturalKey = T.childEntity.value('(EmailAddressValue)[1]', 'varchar(64)'))
;
print '/#DestinationPersonParentTable/'
select * from #DestinationPersonParentTable
print '/#DestinationEmailAddressPersonChildTable/'
select * from #DestinationEmailAddressPersonChildTable
select SSNNaturalKey , HireDate , '---' as Sep1 , EmailAddressValueNaturalKey , EmailAddressType , '---' as Sep2, par.PersonParentSurrogateIdentityKey as ParentPK , child.PersonParentSurrogateIdentityKeyFK as childFK from #DestinationPersonParentTable par join #DestinationEmailAddressPersonChildTable child
on par.PersonParentSurrogateIdentityKey = child.PersonParentSurrogateIdentityKeyFK
IF OBJECT_ID('tempdb..#DestinationPersonParentTable') IS NOT NULL
begin
drop table #DestinationPersonParentTable
end
IF OBJECT_ID('tempdb..#DestinationEmailAddressPersonChildTable') IS NOT NULL
begin
drop table #DestinationEmailAddressPersonChildTable
end
可以使用输出
子句。从文档(强调我的):
OUTPUT子句从受INSERT、UPDATE、DELETE或MERGE语句影响的每一行返回信息或基于这些行的表达式。这些结果可以返回给处理应用程序,以用于确认消息、归档和其他此类应用程序要求。结果也可以插入到表或表变量中。此外,您可以在嵌套的INSERT、UPDATE、DELETE或MERGE语句中捕获OUTPUT子句的结果,并将这些结果插入到目标表或视图中。
像这样:
create table #tempids (a int) -- a temp table for holding our identity values
insert into #test
(b,c)
output inserted.a into #tempids -- put the inserted identity value into #tempids
values
('bvju','hjab')
你接着问...
如果插入是来自select,该怎么办?
它的工作方式相同...
insert into #test
(b,c)
output inserted.a into #tempids -- put the inserted identity value into #tempids
select -- except you use a select here
Column1 ,Column2 from SomeSource
无论插入从值、派生表、执行语句、dml 表源还是默认值,其工作方式都相同。如果插入 1000 条记录,您将在#tempids
中获得 1000 个 id。
问题内容: 如何将上面插入语句中填充的标识值()插入表(另一个表)中 任何人都可以解释一下如何对更大的记录集(成千上万条记录)实施此操作吗? 我们可以使用loop和吗?如果是这样,请说明我们该怎么做? 如果我从选择查询中插入#test,会发生什么情况? 插入#test(b,c)中,从…中选择…(成千上万条记录) 我将如何捕获身份值并将该值用于另一个(#sample)插入#sample(e,f)插入
我想移动项目从一个表到另一个使用MySQL。 这是我目前使用的: 这是可行的,但是我想在一列中插入多个值,而不是在不同的列中插入每个值。 例如: 在表items_rooms中,我希望将items_phoenix中的值x和y放在items_rooms中的一列中。 如果x=5,y=2,我可以像这样将其保存到items\u房间中:一列:5.2,而不是每个值的不同列吗? 抱歉,如果这是混淆!
我试图在从一个表创建一个值后将其插入另一个表我有两个数据库“mem”和“location” 我想从mem表中添加主键id,并将其插入到位置表中的user_id列中。 我有一个sql查询在我的注册表单页面,自动递增'id'在mem表中,但似乎没有添加相同的值到位置表中的user_id, 有人能帮忙吗!
问题内容: 这是否可能在另一个数据结构中包含一个数据结构?到目前为止,我只能在列表中插入字符串或数字。 所需的数据结构将是具有一个列表,其中每个组件也是一个列表。 问题答案: 简单答案:不,Redis列表成员只能是字符串。 复杂的答案:字符串可以做很多事情。您可以使用多种不同的格式(JSON,XML,CSV,特定于语言的序列化等)将列表序列化为字符串。Redis可以容纳很大的字符串值,因此您可以只
问题内容: 我有这段代码可以从“ jobseeker”表中选择所有字段,并可以通过将userType设置为“ admin”来更新“ user”表,其中userID =$ userID(此userID是我用户中的一个用户数据库)。然后,该语句应将这些值从“ jobseeker”表插入到“ admin”表中,然后从“ jobseeker表”中删除该用户。sql表很好,我的语句正在将userType更改
本文向大家介绍如何在PostgreSQL中将一个表中的值插入到另一个表中?,包括了如何在PostgreSQL中将一个表中的值插入到另一个表中?的使用技巧和注意事项,需要的朋友参考一下 假设您有两个表:marks和student_info。下面分别给出两个示例 name roll_no perc_marks 阿尼凯特 12 24 西迪 45 65 亚什 26 42 伊莎 56 87 name rol