Я решил сделать вставку или обновить, зависит от идентификатора, как показано ниже:
CREATE PROCEDURE createEditTip
@ID int = NULL,
@Title varchar(100),
@Description varchar(255),
@Status varchar(10),
@Expiration date = NULL,
@Published date,
@Link varchar(255),
@Category varchar(50),
@PhotoID int,
@Highlighted bit
AS
BEGIN
MERGE Tip AS target
USING (SELECT @ID, @Title, @Description, @Status, @Expiration, @Published, @Link, @Category, @PhotoID, @Highlighted)
AS source (ID, Title, Description, Status, Expiration, Published, Link, Category, PhotoID, Highlighted)
ON (target.ID = source.ID)
WHEN MATCHED THEN
UPDATE SET Title = source.Title,
Description = source.Description,
Status = source.Status,
Expiration = source.Expiration,
Published = source.Published,
Link = source.Link,
Category = source.Category,
Photo = source.PhotoID,
Highlighted = source.Highlighted
WHEN NOT MATCHED THEN
INSERT (Title, Description, Status, Expiration, Published, Link, Category, Photo, Highlighted)
VALUES (source.Title, source.Description, source.Status, source.Expiration, source.Published, source.Link, source.Category, source.PhotoID, source.Highlighted);
IF NOT EXISTS (select 1 from TipCategory where Name = @Category)
BEGIN
insert into TipCategory
values (@Category);
END
END
Я думаю, что это тихое хорошее решение.