Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inserting data from multiple rows into single colu

Author  Topic 

sharad.rk88
Starting Member

7 Posts

Posted - 2010-11-23 : 01:22:18
Hello all,
I have a strange requirement here.. Here it goes..
I am told to do a delete function.. for which i need to store the data into some other table first and then delete original rows... Seems easy right.

But the problem is i have to perform delete operation on some 10 tables each of which has loads of data.. some with even 100 columns.. And i have to store all the data from these tables into a single table before deleting.

Is there any suggestion on how to do it? How to insert data from multiple rows in single table?

Thanks in advance..

--
Sharad

sreekanth939
Starting Member

12 Posts

Posted - 2010-11-23 : 01:38:48

insert into Table_Name Select 1 ;


:)
Go to Top of Page

sharad.rk88
Starting Member

7 Posts

Posted - 2010-11-23 : 02:21:30
what?

i didn't get u... that doesn't work for me...

--
Sharad
Go to Top of Page

sreekanth939
Starting Member

12 Posts

Posted - 2010-11-23 : 02:35:37
select the rows u wanted to delete.
and try this method

select b.* into "New_Table_Name" from ( "ur select query with column names " ) as b


select b.* into New_table from
(
select 1 as b,1 as v,1 as d,1 as f
union all
select 1,1,1,1
) as b


if it doesnt help u ignore this replay ;)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-23 : 03:52:28
Use OUTPUT to delete and save in one go:
http://msdn.microsoft.com/en-us/library/ms177564.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-23 : 04:06:26
Options
you could have a single column to hold all the data from a table. Could be held as xml or similar.
You could have columns in the table srctbl, col01, col02, ... them map the columns from the table to col01, col02...

In both these cases you would need to convert to charcter format if possible.

With these you don't have to convert
Similar to the second but
srctrbl, charcol01, charcol02, ... intcol01, intcol02, ...
hold the columns from each table
srctbl tbla_colname1, tbla_colname2, tbla_colname3, ... tblb_colname1,...
The columns are populated for the table depending on the source tabale name - the others are all null.
This is probably the easiest one to deal with. It is actually partitioning by table so you might thing why not have a table for each source table (lets call then audit trail tables) and combine with a view if you need to present as a single datasource.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sharad.rk88
Starting Member

7 Posts

Posted - 2010-11-23 : 05:27:42
hello webfred,
Thanks for the interesting concept... I researched a bit on output clause. But i didn't find how to insert values into already created table using output... all the examples on the internet show declaring a new @table and inserting deleted records into it..
I would appreciate if u provide me an example..

Thanks

--
Sharad
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-23 : 05:45:34
Just replace the table variable name with the table name.
The output clause can take a table variable, temp table or table.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -