SQL

How to take contents of select and use in a delete

Commands

select * from Location where Mnemonic IN ('ELORR', 'ELORD')


delete from FooRole where Id IN (select ur.Id from FooRole ur, Role r where userId = 5 AND ur.RoleId = r.Id)

UPDATE Path
SET [TypeId] = 1
WHERE Id IN (select Id from Path where TypeId = 2)

Declare @UserId bigInt 
SELECT @UserId = Id from UserTable WHERE Name = 'Peter'

IF EXISTS(SELECT 1 FROM UserTable WHERE Name = 'Peter')

BEGIN

INSERT INTO [dbo].[UserRole]
           ([UserId]
          ...
     VALUES
           (@UserId
           )
END

How to take Id of newly created entity and use in next query

DECLARE @newContractPathId INT

INSERT INTO [dbo].[ContractPath]

SET @newContractPathId = @@IDENTITY
Advertisements
%d bloggers like this: