Solved

Delete All Records From SQL server table in ASP

Posted on 2007-04-06
8
1,195 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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