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


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

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

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