Solved

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

Posted on 2008-10-23
6
698 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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