SQLTeam.com | Weblogs | Forums

Replicate\Clone selected data

tsql

#1

Hi,

I want to replicate data .

This is the example data

I want to replicate Pname1 with a different P_ID
The data should be replicated in all the tables.
lets say we replicate Pname1 and the new P_id is 16. this is how the tables should look now.

Once it is replicated. I will change Pname and Fnames for the new id 16.

I want to know how can achieve this.

Thanks in advance.


#2

Something like this...

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
	P_ID INT,
	COL_1 VARCHAR(10),
	COL_2 VARCHAR(10)
	);
INSERT #TestData (P_ID, COL_1, COL_2) VALUES 
	(15, 'Bob', 'Smith'),
	(15, 'Cathy', 'Davis'),
	(15, 'Dave', 'Jones');

SELECT * FROM #TestData td;

--===========================================
INSERT #TestData (P_ID, COL_1, COL_2) 
SELECT 
	td.P_ID + 1, 
	td.COL_1, 
	td.COL_2
FROM
	#TestData td
WHERE 
	td.P_ID = 15;

SELECT * FROM #TestData td;