诚然,很难理解您要完成的工作。如果您实际上是在尝试将行插入两个不同的表中,如 marc_s 所述,则必须使用两个插入语句。
但是,从您的示例来看,您可能不是尝试插入两个表,而是使用两个表插入到您转置数据的第三个表中。如果是这种情况,那么您可以在一条语句中完成:
Insert MysteryTable( Id, fieldvalue )
Select 1, val1
From Table1
Union All
Select 2, val2
From Table1
Union All
Select 3, val3
From Table2 --assuming these come from Table2. Isn't clear in the OP
Union All
Select 4, val4
From Table2 --assuming these come from Table2. Isn't clear in the OP
当然,如果 Table1 或 Table2 有很多行,那么您显然会在 MysteryTable 中获得许多具有相同 Id 值的行。
对 OP 的更改进行更新
鉴于您的说明,您可以完成您所寻求的,但需要两个与上述类似的查询。
Insert Table1( Id, fieldvalue )
Select 1, val1
From SourceTable
Union All
Select 2, val2
From SourceTable
Insert Table2( Id, fieldvalue )
Select 1, val3
From SourceTable
Union All
Select 2, val4
From SourceTable
生成您的 id 值的另一个变体是:
With NumberedItems As
(
Select val1 As val
From SourceTable
Union All
Select val2
From SourceTable
)
Insert Table1(id, fieldname)
Select Row_Number() Over( Order By val ) As Num
, val
From SourceTable
With NumberedItems As
(
Select val3 As val
From SourceTable
Union All
Select val4
From SourceTable
)
Insert Table2(id, fieldname)
Select Row_Number() Over( Order By val ) As Num
, val
From SourceTable
顺便说一句,在上面的例子中,我使用了Union All,但是如果你想规范化数据,你可能想要有不同的值。在这种情况下,您将使用Union 而不是Union All。