SQL Server存储过程将多个变量输入临时表

SQL Server存储过程将多个变量输入临时表,sql,sql-server,tsql,stored-procedures,Sql,Sql Server,Tsql,Stored Procedures,我需要创建一个临时表并用临时值填充它。变量具有从python脚本分配的值。我的代码如下: ALTER PROCEDURE [dbo].[AddScrapedInfoBULK] ( -- Parameters for the SP, (each field in the all tables) -- ProjectInfo Fields @ProjectInfoID AS INT, @OrderNumber AS NVARCHAR(255), @P

我需要创建一个临时表并用临时值填充它。变量具有从python脚本分配的值。我的代码如下:

ALTER PROCEDURE [dbo].[AddScrapedInfoBULK] 
    (
    -- Parameters for the SP, (each field in the all tables)
    -- ProjectInfo Fields
    @ProjectInfoID AS INT,
    @OrderNumber AS NVARCHAR(255),
    @PeriodofPerformance AS NVARCHAR(255),
    @POPEndDate AS DATETIME,
    @PopStartDate AS DATETIME
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @temproj TABLE (ProjectInfoID INT,
                            OrderNumber NVARCHAR(255),
                            PeriodofPerformance NVARCHAR(255),
                            POPEndDate DATETIME,
                            PopStartDate DATETIME)

    INSERT INTO @temproj 
        SELECT (@ProjectInfoID,
                @OrderNumber,
                @PeriodofPerformance,
                @POPEndDate,
                @PopStartDate)
END
但这是行不通的。如何用变量填充临时表?

您只需使用insert into。。。。价值观,使之成为现实

INSERT INTO @temproj 
            (projectinfoid, 
             ordernumber, 
             periodofperformance, 
             popenddate, 
             popstartdate) 
VALUES      (@ProjectInfoID, 
             @OrderNumber, 
             @PeriodofPerformance, 
             @POPEndDate, 
             @PopStartDate) 
您只需使用insert into。。。。价值观,使之成为现实

INSERT INTO @temproj 
            (projectinfoid, 
             ordernumber, 
             periodofperformance, 
             popenddate, 
             popstartdate) 
VALUES      (@ProjectInfoID, 
             @OrderNumber, 
             @PeriodofPerformance, 
             @POPEndDate, 
             @PopStartDate) 

删除选择框周围的括号

DECLARE    @ProjectInfoID AS INT,
    @OrderNumber AS NVARCHAR(255),
    @PeriodofPerformance AS NVARCHAR(255),
    @POPEndDate AS DATETIME,
    @PopStartDate AS DATETIME

    SET NOCOUNT ON;

    DECLARE @temproj TABLE (ProjectInfoID INT,
                            OrderNumber NVARCHAR(255),
                            PeriodofPerformance NVARCHAR(255),
                            POPEndDate DATETIME,
                            PopStartDate DATETIME)

    INSERT INTO @temproj 
        SELECT @ProjectInfoID,
                @OrderNumber,
                @PeriodofPerformance,
                @POPEndDate,
                @PopStartDate

删除选择框周围的括号

DECLARE    @ProjectInfoID AS INT,
    @OrderNumber AS NVARCHAR(255),
    @PeriodofPerformance AS NVARCHAR(255),
    @POPEndDate AS DATETIME,
    @PopStartDate AS DATETIME

    SET NOCOUNT ON;

    DECLARE @temproj TABLE (ProjectInfoID INT,
                            OrderNumber NVARCHAR(255),
                            PeriodofPerformance NVARCHAR(255),
                            POPEndDate DATETIME,
                            PopStartDate DATETIME)

    INSERT INTO @temproj 
        SELECT @ProjectInfoID,
                @OrderNumber,
                @PeriodofPerformance,
                @POPEndDate,
                @PopStartDate

它甚至没有按编写的那样编译。我知道它没有。如果我想从表中选择值,通常需要进行一些修改,但我从来没有尝试只在临时表中插入变量。仅供参考,@temproj是一个表变量,而不是临时或表。当您离开过程/批处理时,表变量会立即消失,因为as和表具有不同的生存期。@UnhandledExcepseSean谢谢,我在帖子中说错了,因为在运行后删除表变量是所需的功能。不用担心,但是由于插入表变量后,您发布的代码没有做任何事情,我想你可能会认为你可以在存储过程执行之后引用这个表变量。这甚至不能像编写的那样编译。我知道它不能。如果我想从表中选择值,通常需要进行一些修改,但我从来没有尝试只在临时表中插入变量。仅供参考,@temproj是一个表变量,而不是临时或表。当您离开过程/批处理时,表变量会立即消失,因为as和表具有不同的生存期。@UnhandledExcepseSean谢谢,我在帖子中说错了,因为在运行后删除表变量是所需的功能。不用担心,但是由于插入表变量后,您发布的代码没有做任何事情,我想您可能认为可以在存储过程执行后引用该表变量。@DBA108642。这是更好的解决办法。不仅是第一次,而且在insert中列出列是一种最好的做法,值得鼓励。@DBA108642。这是更好的解决办法。这不仅是第一次,而且在insert中列出列是一种最好的做法,应该鼓励这样做。