Solved

Delete All Records From SQL server table in ASP

Posted on 2007-04-06
8
1,159 Views
Last Modified: 2008-02-01
Is there a way i can delete all the records in a table without doing a loop, ie:

RS.Open
Do While Not RS.EOF
   RS.Delete
RS.MoveNext
Loop
RS.Close.

I am using ASP and SQL server 2000.  In SQL query view i would just typeL

DELETE FROM tblAnalytics

and this would clear out the whole table, can this be done in ASP?

Thanks,
Carl
0
Comment
Question by:net-workx
8 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 18868341
>>and this would clear out the whole table, can this be done in ASP?<<
Sure.

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
        .ConnectionSting = "Your connection string goes here"
        .open
        .Execute "DELETE tblAnalytics"
        .Close
End With
Set  cn = Nothing
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18868343
Alternatively if there are no foreign keys on the table you can do:
TRUNCATE TABLE tblAnalytics

Instead of:
DELETE tblAnalytics
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18868432
also, and this is faster,

Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
        .ConnectionSting = "Your connection string goes here"
        .open
        .Execute "TRUNCATE tblAnalytics"
        .Close
End With
Set  cn = Nothing

TRUNCATE does NOT create TransLog entries, where DELETE does.

AW
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18868441
Arthur:

The command is:
TRUNCATE TABLE tblAnalytics

And I already metioned that.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18868444
Sorry, I missed your comment.

AW
0
 
LVL 6

Expert Comment

by:chiragkhabaria
ID: 18868996
Just for the knowledge, Truncate do create the log  the Minimal log entries

http://www.sql-server-performance.com/faq/sqlviewfaq.aspx?topicid=1&faqid=4
0
 

Author Comment

by:net-workx
ID: 18869209
ok because i have a set connection string of:

<%
DSN = "Driver={SQL Server};Server=ServerName;Database=DB;Uid=UN;Pwd=PW;Network=DBMSS0CN"
Set Connection = Server.CreateObject("ADODB.Connection")
Connection.Open(DSN)
%>


i changed the script to:

<%
Connection.CommandTimeout = 30000
Server.ScriptTimeOut = 30000

With Connection
      .Execute "DELETE tblAnalytics"
End With

Connection.Close
Set Connection = Nothing

Response.Redirect "/admin/page.asp?PageID=Resets&MessageID=7"
%>

I had to set the script timeout quite high at is was deleting +50,000 records (sometimes more - sometimes less)

As always if anyone knows a better way to do this then please let me know.

Thanks,
Carl
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18871003
>>As always if anyone knows a better way to do this then please let me know.<<
If you can use TRUNCATE TABLE you will find it a lot faster.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now