Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 delete query help

Author  Topic 

cyberpd
Yak Posting Veteran

60 Posts

Posted - 2007-08-22 : 01:30:28
I want to delete all the rows from my table stockReport, within a stored procedure.

When the SP runs I just need to check if the table exists in the database or not.
if the table exists then there will be Delete from stockReport, otherwise nothing will happen.

plz help me with this query.

thanks and regards

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2007-08-22 : 01:46:41
hi you can use
if exists(select * from sysobjects where xtype = 'U' and name = 'stockReport')
begin
delete from stockReport
end
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-22 : 01:50:00
If you want to delete all rows you can truncate it also

If exists(select * from sysobjects where xtype='u' and name='table_name')
truncate table table_name

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

cyberpd
Yak Posting Veteran

60 Posts

Posted - 2007-08-22 : 01:58:56
wow, that was fast..

thanks, that was exactly what i wanted...
i am stupid as i was going to use the following..

IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'tmpSTOCKREPORT')
BEGIN
DELETE FROM tmpSTOCKREPORT
END
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-22 : 02:51:37
As I said, if you want to delete all rows use truncate

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -