2013-07-03 28 views
1

當你有2代表的表像這樣使用exec()函數插入到表

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 

回答

1

在這種情況下,你不需要使用EXEC

INSERT INTO BranchSubType 
    (BranchTypeID, 
    BranchSubTypeDescription, 
    BranchSubTypeId, 
    DateCreated, 
    UserCreated, 
    IsDeleted) 
SELECT External_BranchTypeID, 
     'Normal', 
     1, 
     getdate(), 
     'System', 
     0 
FROM #BranchType WHERE BranchTypeID =1 
+0

這似乎是工作。謝謝 –