Question Locked

transfer data from one table to three other tables in sql server 2005

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Iam looking for immediate help.
I have one table from that table i have to transfer data to three other tables . if some duplicates are there in the original table they must go to other table called duplicates.If no duplicates the data must be transfered to three other tables.

i have to write a stored procedure.
iam new to writting stored procedures.

so guys please help me.i appreciate your help.

4 replies

by tony hopkinson

a stored procedure is to be used

Nothing to return, nothing to pass in.

CREATE PROCEDURE MyProc
AS
some sql in here
GO

I can help with the sql if you want, but I'll have to buy a clue as to why sticking it between AS and GO is a problem.

by achanta.navya

Hi tony
Thanx for your immediate reply.can you help me with sql.I have to split the data in one table to three tables and if any duplicates they should go to another table called duplicates.

by tony hopkinson

Some tables so you can figure this out.

Table Source
ID Int (Identity)
ContentType Int
Description VarChar(63)

Table Duplicates
ID Int (Identity)
SourceID Int
ContentType Int
Description VarChar(63)
DateReceived DateTime

Table DataType1 (and 2 and 3)
ID Int (Identity)
Description VarChar(63)

Ok
Duplicates

Putting them in Duplicates, NB the structure I'm using will give you a duplicate redord every time the duplicate appears in source,

Insert Into Duplicates(SourceID,ContentType,Description,DateReceived)
Select (ID,ContentType, Description,GetDate())
From
(
Select Source.* From Source,DataType1
Where Source.ContentType = 1
and Source.Description = DataType1.Description
Union
Select Source.* From Source,DataType2
Where Source.ContentType = 2
and Source.Description = DataType2.Description
Select Source.* From Source,DataType1
Where Source.ContentType = 3
and Source.Description = DataType3.Description
) dummyname

And do all the dupiclates

Followed by

Insert Into DataType1(Description)
Select Source.Description
From Source
LEFT OUTER JOIN DataType1
On Source.Description = DataType1.Description
Where Source.ContentType = 1
and DataType1.Description is null

Repeat for tables 2 and 3

The above is about the 'best' version I can come up with in straight sql.

Aside from the union and the sneaky select from (some query) dummyname

NB dummyname can be any valid table name, preferably (not a real one !) The parser insists on it.

The idea is you use a normal join to detect duplicates
And the outer with the where ... is null clause to select all those that aren't in the target table.
It's much better way than doing this
Select * from Table 1 Where Table1.Key not In(select Table2.key From Table2)

NB I used Description as the unique ID to make a point, an integer of some type would of course be more efficient.

If there is going to be fair bit of data building up you'll want to build unique indexes or keys on source and target, then the joins will go well fast.

PS don't forget to housekeep duplicates !
Another nice addition if you are using a stored proc would be to send an email to someone that the job had run (say if it was on the scheduler and may be a a wee snippet of how many duplicates were added.

HtHs

PPS, I didn't run this lot, so spilling mestakes are a probability :p

This discussion is closed to new replies.