Trying to loop through, and delete from, a list of tables using SSIS

China☆狼群 提交于 2020-01-15 08:57:27

问题


I am currently working on establishing an archive and purge process for our database. I inherited this task from an employee who left a few days ago - he'd been working on it (infrequently) for the past several months so I've had to spend quite awhile retracing his steps so to speak and trying to figure out how he had this all set up. We are using a scheduled task to perform our archiving process and I have that part mostly ready to deploy. However, we're using an SSIS package to handle the purge process, and I've never created or modified an SSIS package before so I'm running into some issues that I'm frankly not really even sure where to start with in terms of troubleshooting.

Essentially, we have a table that we use to store the list of tables which will be archived (and eventually purged), which also records the order that the tables need to be purged in to avoid leaving any orphaned records or FK conflicts. The purpose of the SSIS package then is to load the list of tables, ordered according to the purge order, then loop through each of those tables and delete from them based on a WHERE clause referring to the archive date of each record. I think one of the problems I'm running into is that table names can't be used as parameters in an SQL command, and despite trying a few ways to work around this I can't quite get it to work right.

Based on what I've read elsewhere I tried to create a variable containing the SQL statement I'm trying to execute, then selecting that variable as the source for an SQL statement, but I'm still not getting it right. I think I may be pulling the table names from the foreach loop containing the SQL command incorrectly but I'm not sure what I'm doing wrong.

I'll post images below of the two SQL commands I'm using if that helps. I can post the foreach loop as well, or different information pertaining to either of the commands. Again I apologize for the vagueness of my question, I'm just not really familiar enough with SSIS yet to actually know what information would be useful to provide. I unfortunately don't have enough rep to post the images directly so I'll just provide links.

Purge order SQL command

Delete SQL command


回答1:


Yeah you've set your second SQL Task up wrong. When you select "Variable" as the SQLSourceType, then the SourceVariable value should be the name of the SSIS variable that contains the text of your SQL Statement.

So instead of "DELETE FROM WHERE DATEADD(..."

Your SourceVariable value should be something like "User::SqlCmd", and you should have populated the SqlCmd variable with the complete SQL command to be executed.




回答2:


it seems that you did not bind the result of each loop of for each loop container to your TableName variable. please double click your for each loop container then go to variable mapping, then make sure to assign index 0 to variable TableName. thanks




回答3:


Thanks for the answers, I've solved the problem though I'm still not quite sure how. Exasperated, I decided on a lark to try deleting the SQL command responsible for performing the purge, then re-creating it the same way. Bafflingly this fixed the problem.

Like I said in the question, I don't know much at all about how SSIS packages are formed but I imagine there is a lot of metadata and other information stored behind the scenes. I figured since I was picking up a project someone else had been working on there was a chance that the changes I made had gummed up the works somehow. Apparently this was the case. At any rate thanks again for the help.



来源:https://stackoverflow.com/questions/47930161/trying-to-loop-through-and-delete-from-a-list-of-tables-using-ssis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!