批量插入获取ID
我通过 XML 文件插入 SQL 表数据
<root>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
</root>
在表 1 中,Attributevalueid
是自动生成的主键。我当时想在另一个表 table2 中插入 Attributevalueid - 如何通过存储过程实现?
I am inserting SQL table data through XML file
<root>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
<Attributedata>
<AttributeLabelID>-1</AttributeLabelID>
<ProductID>1</ProductID>
<AttributeLabel>User Label</AttributeLabel>
<AttributeTypeID>1</AttributeTypeID>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<Value>
<valueID>-1 </valueID>
<Uservalue>sss </Uservalue>
<ProductAttributelID>1</ProductAttributelID>
</Value>
<CreatedBy >1</CreatedBy>
</Attributedata>
</root>
In table 1, Attributevalueid
is auto generated and primary key. I want to insert at that time Attributevalueid
in another table table2 - how it is possible through a stored procedure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能应该使用触发器。
请重新标记您的问题。
You should probably use a trigger.
Please, retag your questions.
使用OUTPUT 子句:
Use the OUTPUT clause:
我同意触发器响应,但您也可以将该字段从 Identity 更改为常规 int 并使用计数器表。您最终需要一些额外的逻辑,但不需要很多,您可以将所有业务逻辑保留在您的应用程序中。
我仍然会接受触发建议。我只是提供这个作为在其他情况下对我来说效果很好的替代方案。
I agree with the trigger response, but you could also change the field from Identity to a regular int and use a counter table. It would require some additional logic on your end but not a lot and you could keep all of your business logic within your app.
I would still go with the trigger suggestion. I'm only offering this as an alternative that has worked well for me in other situations.