插入存储过程加上静态值

时间:2018-05-24 15:21:43

标签: sql sql-server stored-procedures sql-insert

我试图将多个存储过程的结果插入到临时表中。但是,我还想在每个实例中添加另一个静态值,这些静态值不是这些结果的一部分,但是我不确定执行此操作的语法。

到目前为止我所处的位置:

CREATE TABLE #table (
STATIC              INT, --STATIC NUMBER
SP1                 INT, --STORED PROCEDURE COL 1
SP2                 INT, --STORED PROCEDURE COL 2
SP3                 INT, --STORED PROCEDURE COL 3
);

INSERT INTO #table values('1'), EXEC stored_procedure 'param_1', 'param_2';
INSERT INTO #table values('2'), EXEC stored_procedure 'param_3', 'param_4';

任何建议都会很棒!

2 个答案:

答案 0 :(得分:3)

你不能按照自己的方式做到这一点,但你可以INSERTUPDATEINSERTUPDATE

CREATE TABLE #table (
STATIC              INT, --STATIC NUMBER
SP1                 INT, --STORED PROCEDURE COL 1
SP2                 INT, --STORED PROCEDURE COL 2
SP3                 INT, --STORED PROCEDURE COL 3
);

INSERT INTO #table (SP1,SP2,SP3)
EXEC stored_procedure 'param_1', 'param_2';
UPDATE #table
SET STATIC = 1
WHERE STATIC IS NULL;

INSERT INTO #table (SP1,SP2,SP3)
EXEC stored_procedure 'param_3', 'param_4';
UPDATE #table
SET STATIC = 2
WHERE STATIC IS NULL;

假设存储过程按顺序返回3列。

答案 1 :(得分:0)

插入临时表,然后加入:

CREATE TABLE #sp (
SpKey               INT IDENTITY(1,1) NOT NULL, --use for join
SP1                 INT, --STORED PROCEDURE COL 1
SP2                 INT, --STORED PROCEDURE COL 2
SP3                 INT, --STORED PROCEDURE COL 3
);

CREATE TABLE #static (
StaticKey           INT IDENTITY(1,1) NOT NULL, --use for join
Value               INT
);

INSERT INTO #sp EXEC stored_procedure 'param_1', 'param_2'
INSERT INTO #static VALUES (1)

INSERT INTO #sp EXEC stored_procedure 'param_3', 'param_4'
INSERT INTO #static VALUES (2)

SELECT * INTO #table
FROM
(
SELECT *
FROM #static
INNER JOIN #sp ON #static.StaticKey = #sp.SpKey) sub