1

When you have 2 tables table like this

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,
)

How can you do an insert like the one below in SQL Server? I am trying to return the guid value

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 Answer 1

1

In this case you don't need to use EXEC

INSERT INTO BranchSubType 
    (BranchTypeID, 
     BranchSubTypeDescription, 
     BranchSubTypeId,  
     DateCreated, 
     UserCreated,
     IsDeleted) 
SELECT External_BranchTypeID,
       'Normal',
        1, 
        getdate(), 
        'System',
        0  
 FROM   #BranchType WHERE BranchTypeID =1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.