1

Let's say I have the following scenario

Select
      age
     ,address
     ,full_name
from
     [dbo].customers

I want to replace the [dbo] part with an ssis variable, so when we go to run this package and need to replace the schema, it's a piece of cake.

What I have tried so far was creating a SQL variable, named schema_var_name, and appending it to the front, but this is not working out for me.

Can you tell me what I am doing wrong?

Here is what I have:

"Select
      age
     ,address
     ,full_name
from
     '@[User::schema_var_name]' + '.[customers]' "

2 Answers 2

1

You will need to concatenate the @[User::schema_var_name] variable within the SQL text, with the other parts of the query enclosed with double-quotes ("). An example of an expression for your SQL command is below. This can then be used as the expression for a variable that is set for the source statement in an Execute SQL Task or source component within a Data Flow Task.

"Select age, address, full_name from " +  @[User::schema_var_name] + ".[customers]"
Sign up to request clarification or add additional context in comments.

Comments

1

You could also build a dynamic sql statement and execute it in an Execute SQL Task. The ? tells the task that you want to use a variable in that location. You then set up the variable under Parameter Mapping. Your query would then look like this:

DECLARE @schemaName NVARCHAR(100) = ?
DECLARE @sqlQuery NVARCHAR(MAX) = N'
SELECT
      age
     ,address
     ,full_name
FROM
     [' + @schemaName + '].customers'

EXEC(@sqlQuery)

EDIT:

Here's what the Parameter Mapping would look like: Parameter_mapping

The Parameter Name is most often the least intuitive of these fields. It's a 1-based id sequence for Input parameters. If you had another input variable to use, its Parameter Name would be 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.