我目前正在编写一个存储过程,它创建一个现有对象的确切副本。 对象使用多个表,在这些表2使用许多一对多的关系:多对多关系中引用的重复记录
这里使用的模式:
---------------------------------------
tbl_AssociationA
---------------------------------------
ID | ObjectID | Description
---------------------------------------
1 | 12 | 'Some description'
2 | 12 | 'Some text here'
3 | 13 | 'Some words here'
...
---------------------------------------
---------------------------------------
tbl_AssociationB
---------------------------------------
ID | ObjectID | LabelText
---------------------------------------
1 | 12 | 'Foo'
2 | 12 | 'Foo foo'
3 | 13 | 'Some words...'
4 | 13 | 'Some other Foos'
5 | 14 | 'Some foos here'
6 | 12 | 'Some other words'
...
---------------------------------------
-------------------------
tbl_RelationAB
-------------------------
ID_A | ID_B
-------------------------
1 | 1
2 | 1
2 | 2
2 | 6
3 | 4
3 | 3
...
------------------------
现在,让我们说,我想创建的Object 12
副本(我们称之为Object X
)。我想复制tbl_AssociationA
和tbl_AssociationB
中的记录,因为我不想对Object X
进行进一步修改以修改Object 12
。
所以,创造tbl_AssociationA
和tbl_AssociationB
新的记录,我会用这样的:
INSERT INTO tbl_AssociationA
SELECT @NewId,Description FROM tbl_AssociationA WHERE [email protected];
INSERT INTO tbl_AssociationB
SELECT @NewId,LabelText FROM tbl_AssociationB WHERE [email protected];
我怎样才能插入tbl_RelationAB
使用这些新创建的记录。请注意,tbl_AssociationA
和tbl_AssociationB
中的列ID
是IDENTITY
。
使用scope_identity检索Iden http://msdn.microsoft.com/en-us/library/ms190315.aspx – Paparazzi 2013-03-04 17:00:53