473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3739
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 records a day so any improvement in speed would be...
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 like "Delete 10 rows from table where table.field...
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 like this: unid | oid ---------+---------
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 get a cursor to live dataset. Even if it is a...
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 button, I want to show a message asking to user...
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 index: checkbox. Please let me know if you have any...
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 select either just one or multiple rows in the...
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 bound it to sql table Throw Bindingsource To...
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, but it's not working properly, the output count is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.