Solved

Delete All Records From SQL server table in ASP

Posted on 2007-04-06
8
1,170 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

16 Experts available now in Live!

Get 1:1 Help Now