我有这个SP
CREATE PROCEDURE ApplyExistingImageAcrossModel
@capidfrom int,
@capidto int,
@type int
AS
BEGIN
DECLARE @imageid uniqueidentifier
INSERT INTO ImageMetaData
(ImageId,CapId,[Type],Source,DateAdded,OriginalFileName,OriginalUrl,isDefault,Height,Width,ViewType,CapImageId,islive)
SELECT @imageid,@capidto,@type,source,GETDATE(),originalfilename,originalurl,isdefault,height,width,viewtype,capimageid,islive
FROM ImageMetaData
WHERE [type] = @type AND capid = @capidfrom
INSERT INTO ImageData
(ImageId,ImageBinary)
SELECT @imageid,ImageBinary
FROM ImageData A
LEFT JOIN ImageMetaData B
ON A.ImageId = B.ImageId
WHERE B.[type] = @type
AND B.capid = @capidfrom
END
问题是,select可以返回特定capid
的多个记录,我想将它们全部复制,但每条记录都需要一个唯一的@imageid
,我只生成一次,我需要在两个表中使用imageid
。
由于
答案 0 :(得分:0)
而是创建一个变量,声明表变量以存储唯一ID。然后,使用OUPUT
子句记录所有ID。像这样:
DECLARE @DataSource TABLE
(
@imageid uniqueidentifier
);
INSERT INTO ImageMetaData(ImageId,CapId,[Type],Source,DateAdded,OriginalFileName,OriginalUrl,isDefault,Height,Width,ViewType,CapImageId,islive)
OUTPUT inserted.ImageId
INTO @DataSource (@imageid)
SELECT NEWID(),@capidto,@type,source,GETDATE(),originalfilename,originalurl,isdefault,height,width,viewtype,capimageid,islive
FROM ImageMetaData
WHERE [type] = @type AND capid = @capidfrom
现在,在@DataSource
表格中,我们插入了imageID
。随心所欲地与他们合作。