Re: Truncate table if exists
- From: "Mike" <michael.matthys@xxxxxxxxxxx>
- Date: 26 Jan 2006 05:02:22 -0800
Checking for the object_id is indeed the way to go.
I once needed a quite similar thing and wrote this into a stored
procedure. Something like this:
/***************************************************************************************
* Procedure: pr_TruncateTable
*
* Purpose:
* Truncates the table with the specified name. This is actually
nothing more
* than a truncate table which checks first if the table exists.
*
* Input: Table Name
*
* Examples:
* exec pr_TruncateTable 'table01'
* exec pr_TruncateTable 'dbo.table01'
***************************************************************************************/
create procedure pr_TruncateTable
(
@Table varchar(250)
)
as
begin
set nocount on
declare @SQL varchar(1500)
if exists ( select *
from [dbo].[sysobjects]
where [id] = object_id(@Table)
and objectproperty([id], N'IsUserTable') = 1 )
begin
set @SQL = 'truncate table ' + @Table
exec (@SQL)
end
set nocount off
end
go
.
- References:
- Truncate table if exists
- From: rdraider
- Re: Truncate table if exists
- From: dperiwal
- Truncate table if exists
- Prev by Date: Re: Problem Building Script with Int
- Next by Date: Re: Can i debug/watch on the trigger's INSERTED and DELETED records/values?
- Previous by thread: Re: Truncate table if exists
- Next by thread: select cast help needed
- Index(es):
Relevant Pages
|
|