SQL Server Admin
T-SQL Articles

May 24, 2010

Dynamically Pass a Table Name to TRUNCATE TABLE




Here’s a simple query that shows you how to dynamically pass a Table Name to the TRUNCATE TABLE command

USE YOURDB
GO
DECLARE
@tbl varchar(30) = 'YourTableName'
DECLARE @dynSQL varchar(50)

SET @dynSQL = 'TRUNCATE TABLE ' + @tbl

EXEC (@dynSQL)

You can even create a Stored Procedure and use this code. The only difference will be that ‘YourTableName’ will get replaced with a parameter of the StoredProcedure containing the Table Name


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

0 Responses to "Dynamically Pass a Table Name to TRUNCATE TABLE"
 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions