Solved

Pass a parameter to an "Execute SQL task" that simply truncates a table

Posted on 2008-10-23
6
692 Views
Last Modified: 2012-08-14
I have a "Execute SQL task" in IS 2005 and it is placed in a foreach-container. For each iteration I want it to truncate a different table. The tables to truncate is specified in a variable at runtime.

In SQLtask_general.jpg you can see the settings of the general-tab and in SQLtask-parameterMapping.jpg you can see the settings of my parameter mapping.

When i execute the task I get the following error:
[Execute SQL Task] Error: Executing the query "TRUNCATE TABLE @tablename" failed with the following error: "Incorrect syntax near '@tablename'.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Does anybody know if/how it is possible to pass a parameter to my SQL-task?
SQLtask-general.jpg
SQLtask-parameterMapping.jpg
0
Comment
Question by:Butjii
  • 4
6 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22789019
Yeah, you generally can't use a table name as a variable in SQL.

You could do dynamic SQL ...


Declare @sql varchar(1000)

set @sql = 'Truncate Table ' + @tablename

exec (@sql)

Open in new window

0
 

Author Closing Comment

by:Butjii
ID: 31509103
Thank you!
0
 

Author Comment

by:Butjii
ID: 22810759
I've read that dynamic SQL should be avoided due to bad performance. Does anybody have a view on this matter?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Butjii
ID: 22810760
I've read that dynamic SQL should be avoided due to bad performance. Does anybody have a view on this matter?
0
 

Author Comment

by:Butjii
ID: 22810761
I've read that dynamic SQL should be avoided due to bad performance. Does anybody have a view on this matter?
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24035250
Well, yes, to the extent possible, dynamic SQL should be avoided.  That being said, though, you have to weigh the cost of dynamic SQL against the cost of not truncating the tables.  
I suppose your other choice ould be to have a set of TRUNCATE commands that are called depending on whether or not the table name is in the passed parameter but that would be rather cumbersome.
Why not just put the TRUNCATE command into whatever stored procedure is doing the rest of the work?  You could even pass a flag that you default, for instance, to 'N' meaning (Don't truncate the table) [@TruncateTable CHAR(01) = 'N'] and either truncate or not based on the flag.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now