Solved

MS-SQL 6.5, Command line sql

Posted on 2001-06-09
9
915 Views
Last Modified: 2007-12-19
I have inherited a MS-SQL 6.5 system and brand to to it.  One table has over 7million records, of which I need to purge out.  It seems I can only sql-delete about 100,000 records at one time - safely, otherwise I get record locking errors.  I would like to batch this process using the isql command-line tool.  
How could I enter the isql command prompt using batch files?  When I do "isql -U username", it prompts for the password.  Is there anyway to specify the password within the batch?  My plan is to do some kind of loop in a batch file to delete 100,000 records at a time.  It takes about an hour for each.

One other related question.  I am deleteing because of device space limits.  When I delete a couple of millions records, will I see the database usage go down immediately, or do I have to compact, pack or something like that?

Thanks.
0
Comment
Question by:j_k
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Accepted Solution

by:
amitpagarwal earned 50 total points
ID: 6172737
i guess u create a store proc

create proc PurgeTable as

begin

declare @rowcount int
select @rowcount = count(1) from mytable

while(@rowcount > 0)
begin
set rowcount 100000
delete mytable
select @rowcount = @rowcount - 100000
end

end

hope this helps.

u can even call this procedure from the isql command line itself.

Thanks.
0
 
LVL 5

Expert Comment

by:amitpagarwal
ID: 6172739
i guess even select * into is a non logged operation

select * into newtable from oldtable
truncate oldtable
0
 
LVL 3

Expert Comment

by:hink
ID: 6172870
Even if select into is nonlogged, it requires the same space in the log space (due to rollback when truncate command fails). The only way is to delete it using several delete commands step by step. To avoid locking errors only (when space requiring doesn't mind), delete with(tablock).
0
 
LVL 3

Expert Comment

by:hink
ID: 6172886
If You want to delete all rows, You can drop the table and recreate it. It takes not time.
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6172890
You might try to specify the locking options:

DELETE MyTable (UPDLOCK,TABLOCK)
WHERE ...

Cheer
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6173023
You could

truncate table tbl

or

set rowcount 10000
  while exists (select * from tbl)
       delete tbl

set rowcount 0

You could even put a dely between the deletes if you have to do this while other users are doing things

set rowcount 10000
  while exists (select * from tbl)
  begin
       delete tbl
       waitfor delay, '00:00:05'
  end
set rowcount 0
0
 

Author Comment

by:j_k
ID: 6183774
I'll try to use something like this when I get in procedures.  For now, I used straight SQL and isql command-line to delete the records.

Another question to the forum - How do I reclaim the database space after removing 3million records?
0
 

Author Comment

by:j_k
ID: 6183775
I'll try to use something like this when I get in procedures.  For now, I used straight SQL and isql command-line to delete the records.

Another question to the forum - How do I reclaim the database space after removing 3million records?
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6186186
try shrink database.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

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

18 Experts available now in Live!

Get 1:1 Help Now