1

当你有 2 个这样的表时

CREATE TABLE #BranchType
(
    [External_BranchTypeID] [uniqueidentifier] DEFAULT newsequentialid()  NOT NULL,
    [BranchTypeID] [smallint] identity(1,1) ,
    [BranchTypeDescription] [nvarchar](20) NOT NULL,
    [DateCreated] [datetime] NOT NULL,
    [UserCreated] [nvarchar](20) NOT NULL,
    [DateModified] [datetime] NULL,
    [UserModified] [nvarchar](20) NULL,
    [IsDeleted] [bit] NOT NULL,
)

CREATE TABLE BranchSubType
(
    [External_BranchSubTypeID] [uniqueidentifier] DEFAULT newsequentialid()  NOT NULL,
    [BranchSubTypeID] [smallint] identity(1,1) ,
    [BranchTypeID] [uniqueidentifier] NOT NULL,
    [BranchSubTypeDescription] [nvarchar](30) NOT NULL,
    [FinancialSystemTypeId] [smallint] NOT NULL,
    [DateCreated] [datetime] NOT NULL,
    [UserCreated] [nvarchar](20) NOT NULL,
    [DateModified] [datetime] NULL,
    [UserModified] [nvarchar](20) NULL,
    [IsDeleted] [bit] NOT NULL,
)

如何在 SQL Server 中执行如下插入操作?我正在尝试返回 guid 值

DECLARE @SQLCmd VARCHAR(max)
set @SQLCmd = 'SELECT External_BranchTypeID FROM   #BranchType WHERE BranchTypeID =1'

INSERT INTO BranchSubType (BranchTypeID, BranchSubTypeDescription, BranchSubTypeId,  DateCreated, UserCreated,IsDeleted) 
VALUES ( exec(@SQLCmd), 'Normal',1, getdate(), 'System',0) --FROM   #BranchType A WHERE A.BranchTypeID = 1
4

1 回答 1

1

在这种情况下,您不需要使用 EXEC

INSERT INTO BranchSubType 
    (BranchTypeID, 
     BranchSubTypeDescription, 
     BranchSubTypeId,  
     DateCreated, 
     UserCreated,
     IsDeleted) 
SELECT External_BranchTypeID,
       'Normal',
        1, 
        getdate(), 
        'System',
        0  
 FROM   #BranchType WHERE BranchTypeID =1
于 2013-07-03T13:02:02.887 回答