Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Use a variable to dynamically specify the table name


Use a variable to dynamically specify the table name

Author
Message
Paul Hernández
Paul Hernández
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 654
Hi folks,

This is my next challenge: I'm using an Execute SQL Task to run an update. The source and target tables are in the same server but in different databases. Therefore I'm using the fully qualify name for the target table and set the connection to the source table. Then I need to parameterized both, the source and target tables for deployment flexibility. I can use a package configuration and store the connection string to the source, but the connection to the target is in the SQL statement, that is [db_name].[owner].[tablename]. I know that the name of the tables and the columns cannot be a variable.
Some people recommend to use an expression to build the SQL statement, but this is not an option for me, because the script is about 100 lines long, and the lines are also very large. That´s make the whole package not easy to maintain and so on.

Could anyone recommend me another option?

Kind Regards

Paul Hernández
Mackers
Mackers
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 499
The only ways I can think of doing this are by generating & running dynamic SQL statements either through a stored proc (passing the table names as variables) or a task script (again passing the table names as variables)

Mack
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8348 Visits: 19491
Mackers (2/26/2013)
The only ways I can think of doing this are by generating & running dynamic SQL statements either through a stored proc (passing the table names as variables) or a task script (again passing the table names as variables)

Mack


Or write all of the update logic in a single stored proc which encapsulates the cross-database logic and just call that - nothing dynamic required.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Mackers
Mackers
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 499
Just out of interest, how would that work if you wanted to change the database name?

Mack
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8348 Visits: 19491
Mackers (2/26/2013)
Just out of interest, how would that work if you wanted to change the database name?

Mack


Which database name?

Obviously, the stored proc has to live in a known location and if both the source and target database are completely dynamic, it would have to go into a separate database completely, which is known. Then it could accept parameters - source & target database and table info - and generate and execute dynamic SQL to perform the update.

But it's difficult to judge whether that level of dynamism is required, without more info from the poster.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Mackers
Mackers
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 499
Good point Phil.

Maybe a script task would be a better option as it is database independent.

Mack
Paul Hernández
Paul Hernández
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 654
Hi all,

thanks Phil and Mack for your quick answer.

In my scenario both options (stored procedure and script taks) work fine.

I need to update an operational CRM (Dynamics - SQL DB) using data from an analytical CRM (SAS+SQL DB). Both databases are in the same server, so I just need to parameterized the target or the source. It is just to make the deployment more flexible but is not a strong requirement. As I said, both solutions will work.

Kind Regards,

Paul Hernández
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search