我有两张表,第一张的结构部分概括了第二张的结构:
table1 (id, i, j, k, a, b, c, x, y, z) -- requests
table2 (id, a, b, c, d) -- essential elements / bank subjects
我需要从给定 ID插入table1
记录。table2
最好的方法是什么?
我有两个想法:
1:
DECLARE @a type, @b type, @c type
SELECT @a = a, @b = b, @c = c, FROM table2 WHERE id = @id
INSERT INTO table1 (i, j, k, a, b, c, x, y, z)
VALUES (@i, @j, @k, @a, @b, @c, @x, @y, @z)
2:
CREATE TABLE #result (a type, b type, c type)
SELECT a, b, c INTO #result FROM table2 WHERE id = @id
INSERT INTO table1 (i, j, k, a, b, c, x, y, z)
VALUES (@i, @j, @k,
(SELECT a FROM #result),
(SELECT b FROM #result),
(SELECT c FROM #result),
@x, @y, @z)
存在什么另一种方法?哪一个是最佳实践?