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 に答える