我目前正在使用OLE DB Destination
向表中添加一些记录。每条记录都有一个自动生成的Id
字段。
我想在一些子记录中使用这个生成的Id
字段作为外键。
我以为我能够从一个OLE DB Destination
组件到另一个json_encode($response, JSON_FORCE_OBJECT);
json_encode($response);
组件的数据流,但这不受支持。
我原本以为这是一个常见的问题 - 其他人如何解决它?
答案 0 :(得分:3)
在DataFlow Task
之前添加从此表中返回Execute SQL Task
的{{1}}
MAX(ID)
使用SELECT MAX(ID) FROM MY_TABLE
ResultSet
@[User::MaxID]
)中
Single Row
标记为@[User::MaxID]
变量ReadOnly
类型的输出列(例如:DT_I4
) 在脚本编辑器中使用以下代码(我使用的是Visual Basic语言)
NewID
在Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute> _
<CLSCompliant(False)> _
Public Class ScriptMain
Inherits UserComponent
Private CurrentID as Integer = 0
Public Overrides Sub PreExecute()
MyBase.PreExecute()
CurrentID = Me.Variables.NewID
End Sub
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
CurrentID += 1
Row.NewID = CurrentID
End Sub
End Class
中选中OLEDB Destination
选项,并将Keep identity
列映射到目标标识列
然后,您可以在将数据导入OLEDB目标之前使用NewID
列,因为此变通方法中预测了标识值。 (如果需要并行执行其他操作,可以添加Multicast组件来复制数据流)
<强>参考强>
答案 1 :(得分:1)
我最终使用了此处描述的方法:
就我而言,它看起来有点像这样:
INSERT INTO dbo.Benefit
(PeriodId,
BenefitCode,
...)
VALUES (
?,
?,
...);
SELECT ? = SCOPE_IDENTITY()