6

是否可以在 SQL Server 中执行类似的操作:

INSERT INTO MyTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7) VALUES

SELECT Col1 FROM Func1(),

SELECT Col2 FROM Func2(),

SELECT Col3,Col4,Col5 FROM Func3(),

SELECT Col6 FROM Func4(),

SELECT Col7 FROM Func5()

我有大量返回单值结果的函数和一个返回 3 列的函数。我想将所有这些数据插入到表格的一行中?

我可以看到返回多列的函数可能是一个问题?

4

3 回答 3

13

如果所有函数只返回一行...

INSERT INTO
  MyTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7)
SELECT
  f1.col1, f2.col2, f3.col3, f3.col4, f3.col5, f4.col6, f5.col7
FROM
  (SELECT Col1 FROM Func1())           AS f1
CROSS JOIN
  (SELECT Col2 FROM Func2())           AS f2
CROSS JOIN
  (SELECT Col3,Col4,Col5 FROM Func3()) AS f3
CROSS JOIN
  (SELECT Col6 FROM Func4())           AS f4
CROSS JOIN
  (SELECT Col7 FROM Func5())           AS f5

如果函数返回多于一行,则需要以正常方式加入它们;使用谓词来确定哪一行与哪一行连接。

于 2012-06-01T12:40:04.057 回答
0
 INSERT INTO MyTable (Col1,Col2,Col3,Col4,Col5,Col6,Col7) VALUES

 SELECT Col1 FROM Func1(),

 SELECT Col2 FROM Func2(),

 SELECT Col3 FROM (SELECT Col3,Col4,Col5 FROM Func3()),

 SELECT Col4 FROM (SELECT Col3,Col4,Col5 FROM Func3()),

 SELECT Col5 FROM (SELECT Col3,Col4,Col5 FROM Func3())

 SELECT Col6 FROM Func4(),

 SELECT Col7 FROM Func5()
于 2012-06-01T12:42:39.200 回答
0

您必须删除Values and all ","每个 select 语句周围的括号。

于 2015-07-14T15:58:51.857 回答