Friday, February 10, 2012

Clearing all records in a table

Hi

I have two questions:

1. Is there a way to clear all records in a table from within a store procedure? (Instead of having to go into the table and manually selecting all rows and deleting them that way.)

2. I want to sort a list of records so that the only one left are the ones with just single words in. So I have used


WHERE SearchText LIKE '% %'

to remove the cells with spaces in, but I need to be able to remove the ones with punctuation too. Is there an easy way to do this?

Thanking you in advance.You can clear all records by calling

TRUNCATE TABLE tablename

or

DELETE FROM TABLE

You can do your search for punctuation the same way, adding OR searchText LIKE '%-%', etc.|||Just to be clear, is TRUNCATE TABLE a logged operation? I didn't think it was but thought I'd better check/point it out.|||Truncate table is a minimally logged operation. In a transaction, you can undo it. This transaction:


create table nisse (id int NOT NULL)
go
insert nisse select id from sysobjects
go
select count(*) from nisse
go
begin transaction
truncate table nisse
select count(*) from nisse
rollback transaction
go
select count(*) from nisse

Produces this output:

(101 row(s) affected)

----
101

(1 row(s) affected)

----
0

(1 row(s) affected)

----
101

(1 row(s) affected)|||Erm what is a "minimally logged" operation? Sorry if I'm hijacking this thread to be a teach-in for truncate table.|||The individual rows are not logged, but the data pages in use by the table are logged, and those pages are not reused until the transaction is committed.

No comments:

Post a Comment