是否可以在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列。我想将所有这些数据插入表格的一行?
我可以看到函数返回muliple列可能是个问题吗?
答案 0 :(得分:10)
如果所有函数只返回一行......
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
如果函数返回多行,则需要以正常方式连接它们;使用谓词确定哪一个左行连接到哪一行。
答案 1 :(得分: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()
答案 2 :(得分:0)
您必须删除每个选择语句周围的Values and all ","
和括号。