472,353 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Deleting multiple rows from an SQL Table

Hi All.

I have a routine that checks a SQL Table for all records 3 months
prior to a predetermined date, then I insert them into an Archive DB
then delete those records from the original table.

When I do a "select" for the records, I load them into a dataset, use
an "insert" statement to insert the info into the second table (by a
for next loop and using executenonquery , then a "delete" statement to
remove them.

The way I delete them is "loop" through the rows of the dataset,
create an SQL statement then use ExecuteNonQuery (see code below).

Surely, this is NOT the way to do it as most times, the record count
is around 70000-10000, and it seems to take quiet a while to do (maybe
about 3 records a second).

Could someone advise the most appropriate and speedy way to delete the
items. Remember the records are in a dataset, and I have read
somewhere that you can update/Delete/Insert into SQL using the
Dataset, but not sure of the command.

Any help would be much appreciated.

Thanks,
Robert

<---------------------- Code ----------------------->
Dim iloop as integer
Dim dsorderItems as new dataset

For iloop = 0 To dsOrderItems.Tables("t1").Rows.Count - 1
sSql = "delete tbl_OrderItems where OrderNum = '" &
dsOrderItems.Tables("t1").Rows(iloop).Item("OrderN um") & "'"

updCommand = New SqlCommand(sSql, SQLConn)
updCommand.Connection.Open()
updCommand.ExecuteNonQuery()
updCommand.Connection.Close()
Next
Nov 20 '05 #1
5 3655
2
The bit where you say you read you could do it with a dataset.... I think
you are referring to the dataadapter... I use sqldataadapter for some
stuff... mainly to drive datagrids... anyway, the dsqldataadapterspurpose is
to handle getting records from the database into a dataset and handling any
updates, inserts and deletes. From what I can see to delete a bunch of
records it just executes multiple delete statements similar to your code...
it does however provide other functions and validations that I am not too
clear on also so it does offer advantages apperntly...

Using a dataset allows you to do the operation on a local copy of the data
and then update the server... which is different to performing the updates
directly on the database server itself.

If you are using SQL Server then fire up the SQL Profiler tool... it'll let
you see every SQL command/stored proc being executed on the server... very
handy for seeing what the sqldataadapter does behing the scenes....

Cheers
"Robert Brown" <rb****@edium.com> wrote in message
news:bd************************@posting.google.com ...
Hi All.

I have a routine that checks a SQL Table for all records 3 months
prior to a predetermined date, then I insert them into an Archive DB
then delete those records from the original table.

When I do a "select" for the records, I load them into a dataset, use
an "insert" statement to insert the info into the second table (by a
for next loop and using executenonquery , then a "delete" statement to
remove them.

The way I delete them is "loop" through the rows of the dataset,
create an SQL statement then use ExecuteNonQuery (see code below).

Surely, this is NOT the way to do it as most times, the record count
is around 70000-10000, and it seems to take quiet a while to do (maybe
about 3 records a second).

Could someone advise the most appropriate and speedy way to delete the
items. Remember the records are in a dataset, and I have read
somewhere that you can update/Delete/Insert into SQL using the
Dataset, but not sure of the command.

Any help would be much appreciated.

Thanks,
Robert

<---------------------- Code ----------------------->
Dim iloop as integer
Dim dsorderItems as new dataset

For iloop = 0 To dsOrderItems.Tables("t1").Rows.Count - 1
sSql = "delete tbl_OrderItems where OrderNum = '" &
dsOrderItems.Tables("t1").Rows(iloop).Item("OrderN um") & "'"

updCommand = New SqlCommand(sSql, SQLConn)
updCommand.Connection.Open()
updCommand.ExecuteNonQuery()
updCommand.Connection.Close()
Next

Nov 20 '05 #2
Cor
Hi Robert,

I think that I when I was using the dataadapter I think I would do it like
this.

(I think I would not do that for this, because I think that I would just use
the datareader and a commandnonquery with a SQL delete, but this is your
choose)

You can use a dataview, first set the sortorder and than a rowfilter for the
dates.
(Use the sort because that we have seen in this newsgroup goes faster)

Than you can itterate through the dataview just as
\\\\
Dim dv As New DataView(ds.Tables(0))
For i As Integer = dv.Count - 1 To 0 Step -1
dv(i).Delete()
Next
////
After this you can do a SQLdataadapter.update
You have to make for this an SQL deletecommand, or just use a cmdbuilder.

(You can of course also do that SQL non query delete in that dataviewloop.
Than I think it is better not to do the delete in the dataset but just
reread (fill) it again after that all deletes are done.)

I hope this helps?

Cor
Nov 20 '05 #3
Robert,

My dear friend, the answer is right in front of your eyes....

Why execute a query to delete every single record when you can execute a
query to delete ALL Records older than 3 months?, for example:
DELETE FROM Table1 WHERE YourFieldDate <='00/00/00'

To begin with, why go through all the records generating insert statements
when you could move them all in one shot with a single query?, for example:

select * into Table2 from Table1 WHERE YourFieldDate <= '00/00/00'

Good luck,

Juan.

"Robert Brown" <rb****@edium.com> wrote in message
news:bd************************@posting.google.com ...
Hi All.

I have a routine that checks a SQL Table for all records 3 months
prior to a predetermined date, then I insert them into an Archive DB
then delete those records from the original table.

When I do a "select" for the records, I load them into a dataset, use
an "insert" statement to insert the info into the second table (by a
for next loop and using executenonquery , then a "delete" statement to
remove them.

The way I delete them is "loop" through the rows of the dataset,
create an SQL statement then use ExecuteNonQuery (see code below).

Surely, this is NOT the way to do it as most times, the record count
is around 70000-10000, and it seems to take quiet a while to do (maybe
about 3 records a second).

Could someone advise the most appropriate and speedy way to delete the
items. Remember the records are in a dataset, and I have read
somewhere that you can update/Delete/Insert into SQL using the
Dataset, but not sure of the command.

Any help would be much appreciated.

Thanks,
Robert

<---------------------- Code ----------------------->
Dim iloop as integer
Dim dsorderItems as new dataset

For iloop = 0 To dsOrderItems.Tables("t1").Rows.Count - 1
sSql = "delete tbl_OrderItems where OrderNum = '" &
dsOrderItems.Tables("t1").Rows(iloop).Item("OrderN um") & "'"

updCommand = New SqlCommand(sSql, SQLConn)
updCommand.Connection.Open()
updCommand.ExecuteNonQuery()
updCommand.Connection.Close()
Next

Nov 20 '05 #4
Cor
Hi Juan,

That Robert wrote he first want to archive them.

But I agree with you that I would read them with a same where clause as
delete them, insert them in the archive and than delete them as you said.

But he said he wanted to use a dataset for that.

(I have given with this also the next answer)

:-)

Cor
Nov 20 '05 #5
Thanks to all that replied..

Juan and Cor have given me some real insight into how to perform the
operations. I was only putting it into a dataset because I thought
that might be the fastest, but I knew there was a better way.

I have tried out Juan's code and it certainly kills the timing of my
code.

Thank you all for the fast replies once again..

Robert


"Juan Romero" <ju*********@bowne.com> wrote in message news:<ej**************@TK2MSFTNGP10.phx.gbl>...
Robert,

My dear friend, the answer is right in front of your eyes....

Why execute a query to delete every single record when you can execute a
query to delete ALL Records older than 3 months?, for example:
DELETE FROM Table1 WHERE YourFieldDate <='00/00/00'

To begin with, why go through all the records generating insert statements
when you could move them all in one shot with a single query?, for example:

select * into Table2 from Table1 WHERE YourFieldDate <= '00/00/00'

Good luck,

Juan.

"Robert Brown" <rb****@edium.com> wrote in message
news:bd************************@posting.google.com ...
Hi All.

I have a routine that checks a SQL Table for all records 3 months
prior to a predetermined date, then I insert them into an Archive DB
then delete those records from the original table.

When I do a "select" for the records, I load them into a dataset, use
an "insert" statement to insert the info into the second table (by a
for next loop and using executenonquery , then a "delete" statement to
remove them.

The way I delete them is "loop" through the rows of the dataset,
create an SQL statement then use ExecuteNonQuery (see code below).

Surely, this is NOT the way to do it as most times, the record count
is around 70000-10000, and it seems to take quiet a while to do (maybe
about 3 records a second).

Could someone advise the most appropriate and speedy way to delete the
items. Remember the records are in a dataset, and I have read
somewhere that you can update/Delete/Insert into SQL using the
Dataset, but not sure of the command.

Any help would be much appreciated.

Thanks,
Robert

<---------------------- Code ----------------------->
Dim iloop as integer
Dim dsorderItems as new dataset

For iloop = 0 To dsOrderItems.Tables("t1").Rows.Count - 1
sSql = "delete tbl_OrderItems where OrderNum = '" &
dsOrderItems.Tables("t1").Rows(iloop).Item("OrderN um") & "'"

updCommand = New SqlCommand(sSql, SQLConn)
updCommand.Connection.Open()
updCommand.ExecuteNonQuery()
updCommand.Connection.Close()
Next

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rishi | last post by:
Looking for tips on how to improve performance on deleting records. In our database we do dataloads daily that require us to purge millions of...
7
by: patrick.mcdonald | last post by:
Hello, Example: I have 100 rows that all have the field vaule of 'XYZ' I would like to delete only 10 of those rows. Is there such a query...
2
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something...
4
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you...
3
by: manjub | last post by:
Hi, I am trying to add ability to delete multiple rows from a DataGridView in my application. When user selects multiple rows and presses delete...
10
by: beam1985 | last post by:
So my problem lays in the $_POST if statement at the bottom operation, I keep getting Notice: Undefined index: delete and also Notice: Undefined...
2
by: Brad Pears | last post by:
I am working on a new vb.net 2005 project using SQL server 2000 as the backend db. I have a listview where control I want a user to be able to...
6
by: =?Utf-8?B?TU1TSkVE?= | last post by:
How to let user delete multi rows from the BindingSource while the SelectionMode Property set to RowHeaderSelect I have in my program datagridview...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now,...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.