0

我有一个日历表,我想获取今天的计划项目并复制记录,保留大部分数据并对某些字段进行一些更改,效果很好。

问题在于调用存储过程。我需要在不同的表中创建另一条记录并返回范围标识 ID。

sp inserts 从 Update 中获取插入的数据并使用它来创建新记录,然后返回范围标识。

我尝试使用一个函数,但它不喜欢插入。我如何在这个语句中调用这个 sp?

UPDATE calendar_tbl
SET WO=0
OUTPUT 
    inserted.startDate, 
    dateadd(ss, 0, inserted.startDate), 
    inserted.text, 
    inserted.color, 
    inserted.tColor,
    dbo.funcEpoch(inserted.startDate),
    '',
    inserted.id,
    exec dbo.sp_create @AccountNumber,inserted.CompanyID ,inserted.LocationID, inserted.text,inserted.startDate, dateadd(ss, 0, inserted.startDate), inserted.UserID,'',
    inserted.UserID,
    inserted.LocationID,
    inserted.CompanyID,
    inserted.AccountNumber,
    0
    INTO 
        calendar_tbl(
            startDate, 
            endDate,
            text,
            color,
            tColor,
            evLength,
            rtype,
            PID,
            WOID,
            UserID,
            LocationID,
            CompanyID,
            AccountNumber,
            WOorSchedule)
WHERE   
    (SELECT CONVERT(VARCHAR, startDate, 101)) = (SELECT CONVERT(VARCHAR, getdate(), 101)) AND
    accountNumber = @AccountNumber
4

1 回答 1

1

您可以使用可组合的 DML 来嵌套 UPDATE、INSERT 并捕获 auditId。下面的简单示例:

USE tempdb
GO

SET NOCOUNT ON

-- Create a table
IF OBJECT_ID('dbo.sampleTable') IS NOT NULL DROP TABLE dbo.sampleTable
CREATE TABLE dbo.sampleTable ( rowId INT IDENTITY PRIMARY KEY, someData UNIQUEIDENTIFIER DEFAULT NEWID(), dateAdded DATETIME DEFAULT GETDATE(), addedBy VARCHAR(30) DEFAULT SUSER_NAME(), dateModified DATETIME, ts ROWVERSION )
GO
IF OBJECT_ID('dbo.auditTable') IS NOT NULL DROP TABLE dbo.auditTable
CREATE TABLE dbo.auditTable ( auditId INT IDENTITY(1000,1) PRIMARY KEY, rowId INT, someData UNIQUEIDENTIFIER, dateAdded DATETIME, addedBy VARCHAR(30), ts ROWVERSION )
GO

-- Populate table
INSERT INTO dbo.sampleTable DEFAULT VALUES
GO 100


-- Example composable DML query to do both UPDATE, INSERT and record new auditIds
INSERT INTO dbo.auditTable ( rowId, someData, dateAdded, addedBy )
OUTPUT inserted.auditId
SELECT rowId, someData, dateAdded, addedBy
FROM
    (
    UPDATE TOP(10) t1
    SET dateModified = GETDATE()
    OUTPUT inserted.rowId, inserted.someData, inserted.dateAdded, inserted.addedBy
    FROM dbo.sampleTable t1
    WHERE dateModified IS NULL
    ) x


SELECT *
FROM dbo.sampleTable
WHERE dateModified IS NOT NULL

SELECT *
FROM dbo.auditTable

这样的东西对你有用吗?

于 2013-07-30T21:40:39.887 回答