레코드를 삽입하고 기본 키를 반환하는 함수를 작성하여 해당 함수를 다른 쿼리의 매개 변수로 전달할 수 있습니다. 예를 들어,이 시도 :INSERT가 포함 된 값을 반환하는 SQL 함수
CREATE FUNCTION CreateEntity
(
@entityTypeId int,
@siteId int
)
RETURNS bigint
AS
BEGIN
DECLARE @entityId bigint;
INSERT INTO [ObjectSystem].[Entities] ([EntityTypeId], [SiteId], [Deleted])
VALUES (@entityTypeId, @siteId, 0);
SELECT @entityId = SCOPE_IDENTITY();
RETURN @entityId;
END
GO
을하지만 MSSMS는 INSERT 쿼리를 강조하고 말한다 "함수 내에서 사이드 초래 운영자 '삽입'의 잘못된 사용을."
그래서 같이 이러한 기능을 사용할 수 있도록하고 싶습니다 : 모든 경우
INSERT INTO OtherTable (EntityId, Name, Description) VALUES
(CreateEntity(), 'Test', 'This is a test item.'),
(CreateEntity(), 'Test', 'This is a test item.'),
(CreateEntity(), 'Test', 'This is a test item.'),
(CreateEntity(), 'Test', 'This is a test item.'),
(CreateEntity(), 'Test', 'This is a test item.'),
(CreateEntity(), 'Test', 'This is a test item.');
가 어떻게 원하는 효과를 얻을 수 있습니까? 가능하지 않다면 어떻게 할 수 있습니까?