测试数据中的参照完整性

时间:2017-01-31 11:39:57

标签: tsql sql-server-2012

我有以下插页:

    INSERT INTO TransactionActivityLog
    (
         [UserName]
        ,[ApplicationId]
        ,[ActivityType]
        ,[ActivityDate]
        ,[ActivityAllowed]
        ,[ResultCode]
        ,[ResultMessage]
        ,[CCTransactionId]
        ,[CCMasterTokenId]
        ,[PaymentTransactionStatus]
        ,[CCMasterTokenStatus]
        ,[AdditionalData]
        ,[TargetUserId]
        ,[BranchId]
    )
    VALUES
    (
        'UserName' + CONVERT(varchar(5), @TargetRowCount) -- UserName
        ,'00000000-0000-0000-0000-000000000000' -- ApplicationId
        ,FLOOR(RAND()*(4-0)+0) -- ActivityType
        ,DATEADD(DAY, RAND(CHECKSUM(NEWID()))*(1+DATEDIFF(DAY, @FromDate, @ToDate)), @FromDate) -- ActivityDate
        ,1 -- ActivityAllowed
        ,NULL -- ResultCode
        ,NULL -- ResultMessage
        ,SELECT TOP 1 Id FROM CCTransactions ORDER BY NEWID() -- CCTransactionId (Needs referencing to CCTransactions table!)
        ,NULL -- CCMasterTokenId (Needs referencing to CCMasterToken table!)
        ,-1 -- PaymentTransactionStatus
        ,FLOOR(RAND()*(6-0)+0) -- CCMasterTokenStatus
        ,'AdditionalData' + CONVERT(varchar(5), @TargetRowCount) -- AdditionalData
        ,'00000000-0000-0000-0000-000000000000' -- TargetUserId
        ,ABS(Checksum(NewID()) % 5) + 1  -- BranchId
    )

该行:

SELECT TOP 1 Id FROM CCTransactions ORDER BY NEWID()

在SSMS中产生错误(SQL Server 2012)。

我的意图是;我想从表 CCTransactions 中随机选择一行,然后选择其ID并在上面的INSERT语句中插入其值。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用如下

null,
(SELECT TOP 1 Id FROM CCTransactions ORDER BY NEWID())
null
...