Solved

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

Posted on 2008-10-23
6
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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