Solved

Delete All Records From SQL server table in ASP

Posted on 2007-04-06
8
1,188 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

827 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