根据从select

时间:2018-10-11 22:05:54

标签: sql sql-server tsql

我有一条选择语句,例如

SELECT
    [TaskStatusName],
    [TaskTypeName],
    [TaskId]
FROM
    [TaskStatusAudit]
WHERE
    [TaskId] IN
    (
        SELECT
            [T].[TaskId]
        FROM
            [Task] AS [T]
        WHERE
            [T].[DesignKey] = @LastDesignKeyOfProject
            AND
            [TaskStatusId] != @PendingStatus
            AND
            [TaskStatusId] != @CompletedStatus
    )

在此之下,我有一条插入语句:

INSERT INTO [TaskStatusAudit]
(
    [TaskStatusAuditId],
    [Date],
    [EmpGuid],
    [TaskStatusName],
    [TaskTypeName],
    [TaskId]
)
VALUES
(
    @TaskStatusName,
    @TaskTypeName,
    @TaskId
)

我的问题是,如何在我的第一次选择中执行插入的foreach值或行?问候

更新

作为下面的评论,我尝试这样做:

  DECLARE @TaskStatusName VARCHAR(255)
                , @TaskTypeName   VARCHAR(255)
                , @TaskId         UNIQUEIDENTIFIER;

     --Assigning pararameters
   SELECT
                 @TaskStatusName = 'COMPLETED'
                , @TaskTypeName = [TaskTypeName]
                , @TaskId = [T].[TaskId]
                 FROM [TaskStatusAudit] AS [TSA]
                     INNER JOIN [Task] AS [T] ON [T].[DesignKey] = @LastDesignKeyOfProject
                                            AND [T].[TaskStatusId] != @PendingStatus
                                            AND [T].[TaskStatusId] != @CompletedStatus
            -- using parameters to insert
            INSERT INTO [TaskStatusAudit]
            (
                 [TaskStatusAuditId]
                , [Date]
                , [EmpGuid]
                , [TaskStatusName]
                , [TaskTypeName]
                , [TaskId]
            )
            VALUES
                (
                 NEWID()
                , @Today
                , @CreatedBy
                , @TaskStatusName
                , @TaskTypeName
                , @TaskId
                )

我运行它,但是它只插入一行而不是多行。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

使用INSERT INTO SELECT FROM

此外,我还简化了您的SELECT,使其使用INNER JOIN而不是WHERE x IN ( SELECT y )

INSERT INTO [TaskStatusAudit] 
(
    [TaskStatusAuditId],
    [Date],
    [EmpGuid],
    [TaskStatusName],
    [TaskTypeName],
    [TaskId]
)
SELECT
    @auditId,
    GETUTCDATE(),
    @empGuid
    [TaskStatusName],
    [TaskTypeName],
    [TaskId]
FROM
    [TaskStatusAudit] AS A
    INNER JOIN [Task] ON
        [Task].DesignKey = @LastDesignKeyOfProject AND
        A.[TaskStatusId] != @PendingStatus AND
        A.[TaskStatusId] != @CompletedStatus

答案 1 :(得分:0)

参数存储单个值。它只会插入一个预期的。下面的代码应该可以工作;

INSERT INTO [TaskStatusAudit]
(
     [TaskStatusAuditId]
    ,[Date]
    ,[EmpGuid]
    ,[TaskStatusName]
    ,[TaskTypeName]
    ,[TaskId]
)            
    SELECT
        NEWID(), 
        GETDATE(),
        @CreatedBy,
        'COMPLETED',
        [TaskTypeName]
        [TaskId]
        FROM [TaskStatusAudit] AS [TSA]
            INNER JOIN [Task] AS [T] ON [T].[DesignKey] = @LastDesignKeyOfProject
                                AND [T].[TaskStatusId] != @PendingStatus
                                AND [T].[TaskStatusId] != @CompletedStatus

您可以从here

检查“ INSERT INTO SELECT”的语法