SSIS Using Variables in Query Name - ssis

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]' "

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]"

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:
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.

Related

SSRS-Report -How to get table name from parameters

I am trying to get table name from parameters, then i will use that to my query like
declare #cmd nvarchar(100)
declare #tableName nvarchar(100)
select #cmd = ' select * from ' + #tableName
execute (#cmd)
when i try to run with query designer dataset, it popup error message like
the declare sql construct or statement is not supported
What am I missing that needs to be there or what is causing this issue?
i hope somebody can give a hand how to solve it
thanks
It's just the query designer that cannot support this for obvious reasons. Just click "Edit as text" in the top left corner of the query designer and then you can do whatever you like.
Just remember that your dataset must always return the same column names in the same order with the same datatypes.

SQL task select top N records where in is a variable from a parameter?

Ive got another task elsewhere in a package that I build a sql command from a script task, but I now have a simpler sql task that I want to parameterize the number of records I fetch, and drive this parameter via a package variable.
Now:
SELECT TOP 10 col1,col2,col3 from TABLE-A
What I need to use
eg. SELECT TOP ? col1,col2,col3 from TABLE-A
Where ? corresponds to a package variable of type int.
Can I do this with just the SQL task, and not have to derive the statement in something like a script task first?
[ update ]
Solution is what I was suspecting, I just added another script task before the sql task, and generate the statement there. Then change the sql task to use the variable that contains the statement.
Use the variable to build a dynamic sql string in a separate variable, and then execute the dynamic sql string variable.
Parameters can only be used in the WHERE clause.
As Tab mentioned, you will need to create a variable expression to construct the SQL statement. Then, in the Execute SQL Task, you will tell it to use a SQL String (see bullet point 4 in the section "Map query parameters to variables").
https://learn.microsoft.com/en-us/sql/integration-services/control-flow/execute-sql-task?view=sql-server-2017#parameters-in-the-execute-sql-task
declare #intVal as int = 5
select * from
(
select ROW_NUMBER() over(order by Id) as RowNo, * from <tableName>
)a
where a.RowNo <= #intVal
here #intVal will be parameter should be mapped in SQL task

MS SQL Linked Servers And Using In FROM Clause

I have a very basic test stored procedure, shown below. This proc is reading data in a different database, in a different server. To do this I am using a linked server. From what I have read, I need to change the FROM clause to this:
[linked server name].[database name].[schema name].[table name]
However, I would like to pass in the linked server name and database name as parameters and use them in my FROM clause. I am not concerned with injection attacks, etc. I will be passing this in from a config file.
create PROC [dbo].[SelectTEST]
#GU UNIQUEIDENTIFIER,
#LINKED_SERVER_NAME nvarchar(max),
#DATABASE_NAME nvarchar(max)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
SELECT [GU]
FROM '[' + #LINKED_SERVER_NAME +'].['+ #DATABASE_NAME + '].[Test Table] '
WHERE ([GU] = #GU OR #GU IS NULL)
COMMIT
This is a big mess of syntax errors. Is it possible to pass in these parameters and use in my stored procedure? I would have to make this change to a bunch of different procs, so sorta trying to find the a succinct solution...

SSIS Execute SQL task based on parameter

Can i do something like below, let me know
IF #parameter=1 BEGIN ...query... END IF #parameter=2
Need the correct syntax if it is possible.
It's OLE DB connection.
Not a Stored Proc. just a sql query
DECLARE #param AS INT = ?;
IF #param = 1
BEGIN
SELECT 1 AS Y;
END
ELSE IF #param = 2
BEGIN
SELECT 2 AS Y;
END
There are two question marks in your query and probably you were passing only one variable. I have seen code where developers pass the same value twice (or multiple) times. This is inefficient. A better way is to receive the passed parameters in SSIS variables. Advantages:
1. You need to pass one value only once.
2. More importantly, if you change the order in which the passed parameters are used in the sql, you do not need to change their order on the user-interface of Execute SQL Task Editor//Parameters. This is what Andy Leonard has suggested later in his response.
You can. Assuming you are referring to an Execute SQL Task, the parameters in an Execute SQL Task using an OLE DB connection utilize question marks (?) as parameter placeholders. You map the placeholders to SSIS variables on the Parameter Mapping page of the Execute SQL Task. In the SQLStatement property you would enter:
If (?=1)
begin
... {some T-SQL here} ...
end
If (?=2)
begin
... {some T-SQL here} ...
end
That's one way to accomplish what I think you are asking.
Another way is to create an Execute SQL Task to read the value of #parameter from the database into an SSIS variable. Then you can build two Execute SQL Tasks - one with each option for T-SQL as the SQLStatement property - and use expressions on precedent constraints to determine which Execute SQL Task to execute.
Hope this helps,
:{>
You cannot use Execute SQL Task to run Transact-SQL statements.
For setting conditional SQL Statement based on what you are trying to achieve.
In Execute SQL Task editor
In general tab, leave the SQLStatement blank.
In parameter mapping tab, add parameter and map User::Parameter variable to Parameter Name 0.
In Expression tab, set the SQLStatementSource to
(DT_NUMERIC, 18, 0) #[User::Parameter]==1 ? ...query 1... : ...query 2...

TSQL: variable for access to database (SQL Server 2008)

I have written a T-SQL script that migrates some data from one database to another.
At the moment I am doing that by use of dynamic sql.
For example see the following code:
Declare #sqlquery nvarchar(4000)
SET #sqlquery = N'SELECT * from ' + #LinkServerName + #SourceDatabaseName + '.dbo.Table'
EXEC #sqlquery
In this example #LinkServerName is a nvarchar variable that stores the name of the linked server for the SQL Server that contains the source database. #SourceDatabaseName is a nvarchar variable that stores the name of the source database.
I donĀ“t like that way. I would prefer the following code:
SELECT * from #SourceDatabase.dbo.Table
Is that possible?
Thank you in advance.
Second approach is incorrect, first one is the correct one. For more information check this other question here at stackoverflow how-to-use-variable-for-database-name-in-t-sql