I am trying to copy data from one table to another but with new ID, kind of duplicating entries for a new row. Data is copied correctly except but the FK which is not copied correctly. The procedure
CREATE PROCEDURE [dbo].[AddDuplicate]
@AIDNew as int,
@AID as int
AS
BEGIN
BEGIN
DECLARE @EIDNew int
IF NOT EXISTS (SELECT 1 FROM tblEvaris WHERE AID = @AIDNew)
INSERT INTO tblEvaris(Componenta,FactorRisc,FormaManifestare,Gravitate,ClsGravitate,Probabilitate,ClsProbabilitate,Risc,AID)
SELECT Componenta,FactorRisc,FormaManifestare,Gravitate,ClsGravitate,Probabilitate,ClsProbabilitate,Risc,@AIDNew
FROM tblEvaris WHERE AID = @AID
SET @EIDNew = SCOPE_IDENTITY();
INSERT INTO tblMasura (TipMasura,Masura, GravitateF, ClsGravitateF, ProbabilitateF, ClsProbabilitateF, RiscF, EID)
SELECT TipMasura,Masura,GravitateF,ClsGravitateF,ProbabilitateF,ClsProbabilitateF,RiscF,@EIDNew
FROM tblMasura JOIN tblEvaris ON tblMasura.EID = tblEvaris.EID WHERE tblEvaris.AID = @AID;
END
END
The first table is updated corectly, see the ID

but the second table only insert last ID as FK

and I don't understand why?

Marius VasilePosted Nov 25, 2023, 4:12 PM
I tried a different approach but the following procedure has a mistake and I don't see it
There was a small mistake in the last line, now the procedure works fine and it makes the changes I wanted in the first place
Marius VasilePosted Nov 25, 2023, 3:20 PM
I modified the procedure as follow
it will add data into tblMasura but only the last EID added to table
Prasad RaveendranPosted Nov 25, 2023, 3:06 PM
The reason you might not be getting the latest identity value from the
tblEvaristable usingSCOPE_IDENTITY()could be due to multiple factors:SCOPE_IDENTITY(): It retrieves the last identity value generated in the current session and scope. However, it only works for the current scope. If your procedure involves triggers or nested scopes that might interfere with the identity value, SCOPE_IDENTITY() might not return the expected value.
Concurrency Issues: If multiple insert operations are happening simultaneously, there might be a chance that another transaction inserts a record into the
tblEvaristable after your check withNOT EXISTSand before theSCOPE_IDENTITY()call.Other Operations: Any other operations, such as triggers or explicit changes to identity seed or increment values, could affect the identity value.
To ensure you get the latest identity value inserted into
tblEvaris, you could consider usingOUTPUT INSERTED.EIDafter theINSERT INTO tblEvarisstatement to retrieve the insertedEID. Here's an example:This approach captures the inserted
EIDinto a variable (@InsertedEIDs) and allows you to use it subsequently in your procedure. Remember to declare@InsertedEIDsas a table variable before using it.Additionally, please review the logic of your procedure and ensure there are no conflicts or concurrency issues that might affect the identity value retrieval.
Marius VasilePosted Nov 25, 2023, 3:00 PM
I tried first because it made more sense but it will only add data to first table and none to second. I have to translate this to sql server
Copy all data from tblMasura WHERE AID = OldAID but EID WHERE AID=NewAID
Prasad RaveendranPosted Nov 25, 2023, 2:52 PM
Apologize. Did you try with @AIDNew for the third solution instead of @AID? please try and let me know
Marius VasilePosted Nov 25, 2023, 2:41 PM
As far as this procedure
will add values to tblMasura but EID will be old ones, not new
Marius VasilePosted Nov 25, 2023, 2:35 PM
Thank you Prasad, first query is doing the same as mine. The second is throwing an error, I think because of
SET @EIDNew = (SELECT EID FROM tblEvaris WHERE AID = @AIDNew)
the result is not a single value
For the third solution I tried it but it doesn't insert values in second table
Prasad RaveendranPosted Nov 25, 2023, 1:23 PM
please use the below modified script and see how it behaves.
or
you can directly use the
EIDfrom thetblEvaristable without relying onSCOPE_IDENTITY(). Here's the modified procedure:OR
Yes, you can directly use
e.EIDfrom thetblEvaristable in theINSERT INTO...SELECTstatement fortblMasura. However, make sure thate.EIDfromtblEvariswill refer correctly to the newly inserted or existingEIDfor the given@AIDNew.Here's how you might use it:
This code snippet performs an
INSERT INTO...SELECToperation intotblMasura, usinge.EIDfromtblEvaris. It will select records fromtblMasuraand join them withtblEvarisbased on theEIDrelationship wheree.AIDmatches the provided@AID.Just ensure that
e.EIDcorresponds to the correctEID(either the newly inserted one or the existing one) for the given@AIDNewor@AID. Adjust the logic within the stored procedure accordingly to meet your exact requirements and to ensure data integrity.