Solved

Delete All Records From SQL server table in ASP

Posted on 2007-04-06
8
1,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 18868441
Arthur:

The command is:
TRUNCATE TABLE tblAnalytics

And I already metioned that.
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Viewers will learn how the fundamental information of how to create a table.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

632 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