I want to copy a row from a table into the same table by changing few column values. Depending on the shape in that row I have to copy the row from shape table into the same table with new id.
I tried this:
CREATE PROCEDURE Sp_CopyAnnotation
@AnnotationID int,
@AssetVersionID int,
@NewAssetVersionID int
AS
DECLARE @NewAnnotationID int
DECLARE @Shape varchar(50)
BEGIN
insert into AnnotationMain(Text,x, y,Shape,Color,z1,JobID,AssetID,UserID,UserName,Department,AssetVersionID,HideAnnotation)
select Text
,x
,y
,Shape
,Color
,z1
,JobID
,AssetID
,AssetVersionID
,UserID,UserName
,Department
,@NewAssetVersionID
,1
from AnnotationMain where AnnotationID=@AnnotationID;
--set @NewAnnotationID=@@IDENTITY
select @NewAnnotationID=AnnotationID,@Shape=Shape from Proofing.AnnotationMain where AnnotationID=@@IDENTITY
update AnnotationMain
set Annoation_num=(select (max(Annoation_num)+1)
from AnnotationMain
where AssetVersionID=@AssetVersionID)
where AnnotationID=@NewAnnotationID
IF(@shape='ellipse')
BEGIN
Insert into CircleTable(AnnotationID,x1,x2,y1,y2)
select @NewAnnotationID
,x1
,x2
,y1
,y2
from CircleTable where AnnotationID=@AnnotationID
select result=1;
END
ELSE IF(@shape='rectangle')
BEGIN
Insert into RectangleTable(AnnoationID,x,y,w,h)
select @NewAnnotationID
,x
,y
,w
,h
from RectangleTable where AnnoationID=@AnnotationID
select result=1;
END
ELSE IF(@shape='pencil')
BEGIN
Insert into PencilTable(AnnoationID,x1,y1,cord)
select @NewAnnotationID
,x1
,y1
,cord
from PencilTable where AnnoationID=@AnnotationID
select result=1;
END
END
GO
But it gives me an error
Msg 121, Level 15, State 1, Procedure Sp_CopyAnnotation, Line 23 The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
And I'm not bale to make out where I'm going wrong
AssetVersionID missing in Insert column list
INSERT INTO AnnotationMain
(Text,
x,
y,
Shape,
Color,
z1,
JobID,
AssetID,
AssetVersionID, --Missing
UserID,
UserName,
Department,
AssetVersionID,
HideAnnotation)
SELECT Text,
x,
y,
Shape,
Color,
z1,
JobID,
AssetID,
AssetVersionID,
UserID,
UserName,
Department,
@NewAssetVersionID,
1
FROM AnnotationMain
WHERE AnnotationID = @AnnotationID;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With