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
Solved

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

Posted on 2008-10-23
6
702 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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 …
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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