Reply to Re: Truncate table if exists

Your name:

Reply:


Posted by Mike on 01/26/06 15:02

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация