Accessing 'Global' Variables In An ExecuteSQL Task - sql

I have an SSIS package that does the following:
Selects the connection strings from a table of servers. The connection string is either the name of the server along with the domain (i.e. Dalin.myhouse.com) or is the direct IP to a server.
The package iterates through each connection string and populates a defined 'global' variable. This variable was created with in the Variable designer. For each connection string, the package will retrieve information about the server.
The problem I'm having is when you add a server IP to the list, the 'Friendly Name' may not be known at that time so I would just have the server IP in both the connection string column and the friendly name column of the table. I want to, after the end of an iteration, update that the Friendly Name column of the server entry within that table so that it has the server name pulled from the server using SERVERPROPERTY('Servername')
To do this, I would like to use an ExecuteSQL Task with the following code:
UPDATE [myDB].[mySchema].[myServers]
SET [ServerName] = VarA
WHERE ConnectionString = VarB
The previous code is using a static connection to the server where the myServers table resides.
VarA represents the global value I want to set the ServerName to which would be set in a separate SQLTask by using SERVERPROPERTY('Servername'). It needs to be in a separate task because it would have to connect to a server by using the same server the current iteration is using.
VarB is set at the beginning of every iteration to the next Connection String in the list.
I've seen examples on how to use this for the Script Task and Script Components but I would like to simply use the ExecuteSQL Task to accomplish this.
Summary:
Connect to ServerA and fill two global variables.
Connect to ServerB and use the two global variables to update a specific row in a table.
Any ideas?

I can't see how this can be accomplished without the variables being set within a Script Task, since ExecuteSQL tasks have to be set to a database connection. Script Tasks work for this because their connection is within the context of the server that's executing them. That being said, you could use a Script Task prior to this ExecuteSQL task that sets the variables to the local server instance.

So you need the Execute SQL task to take parameters?
http://msdn.microsoft.com/en-us/library/ms187685.aspx
Maybe I've misunderstood...

Related

SSIS - Passing variable as column name in where clause of an ado.net source SQL command expression

I'm trying to build a template SSIS package that will pull from our source system and populate our load tables. The package works as follows, since we're taking from Oracle and moving to SQL Server.
Variable of "SourceDelta", String, value is blank
Execute SQL Task that is a select statement against a SQL table holding the delta column name, LastUpdateDT, the result set is mapped to the variable SourceDelta.
I then have a data flow task, where the source is an ado.net source. I have an expression on the data flow task which is:
select *
from VariableSourceLocation
where
SourceDelta >= ##
and SourceDelta < ##
Fine, you'd think, it'd populate the SourceDelta colum name into the expression. However, when opening the data flow task and opening the ado.net source, the sql command text reads as follows:
select *
from VariableSourceLocation (sets correctly showing the value)
where
BLANK>= ##
and BLANK< ##
It just passes a blank, as the variable has no value until the previous step. It's as though it is not inputting the variable prior to the package validation, so validation errors as it can't run the where clause of the query.
Banging my head against the wall with this, any help appreciated. Fully aware of injection issues surrounding this but it's purely for me to create SSIS data flow packages utilising a single sql table only I will have access to for variables. It'll speed up the onerous task a lot.

ODBC source doesn't receive dynamic variable value

I am designing an incremental load for my ETL solution in SSIS. For that, I have an Execute SQL task that gets the maximum load time from the data warehouse and then stores it in a package variable, which is already set to evaluate as expression.
Then I have set the ODBC source's sql command property to an expression that has my query and the variable. However, I have looked into the variable during debug and when I run the package it seems that this variable doesn't get used in the sql command, instead it remains null.
I have already tried setting the variable property 'EvaluateAsExpression' to True, I have tried storing the query in a different variable and then setting that as the sql command for my ODBC source.

SSIS Using For Loop Container Not Executing Script Task and Data Flow Tasks

I'm reaching out to the experts as I have hit a wall with a recent project. I have created an SSIS package (2008R2) that uses a script task to build a SQL statement, where a variable(#month1) is being used within the SQL statement, to specify a month look back in a membership table. I want to also use the #month1 variable as a "counter" for the loop container to specify how many times to execute the query. The SQL query is attached to a data flow task to append these records into a table on a SQL server database. The script task and data flow task work outside of the for loop container with the initial value given for the #month1 variable but I cannot figure out how to make the for loop container update the #month1 "counter" variable so that the for each loop can use it as a "counter" and the SQL statement can use it as a condition with in the created SQL statement. Any one have any ideas or examples on how to do this?
** Update **
The For Loop container is the issue. The script task and data flow task work outside of the For Loop container. It will use the initial variable setting for #month1 and create the dynamic sql script, execute script and transfer data from source database server to the destination source server. The issue is when I place these steps within the For Loop container, the container executes and turns green but does not invoke the steps within it. This is why I'm thinking the container is not reading the variable #month1, even though the variable is set at the package level. Any thoughts?
First of all, try to set the data flow Delay Validation property to True. If it still not working, instead of passing the variable as parameter in the OLEDB Source use expressions:
Create a variable of type string.
Change its EvaluateAsExpression property to True
Set the expression similar to:
"select * from table
Where column =" + (dt_str, 50)#[User::month1]
In the OLEDB Source select the Access mode as SQL Command from variable and select this variable.
Be aware that the month1 variable is not created twice wuth different scopes, click on the data flow task and check the variable panel if it shows additional variables.
I appreciate everyone's responses but it seems I tricked myself on this one. In looking for the most complicated issue I overlooked the most simple and obvious one. The reason my For Loop was not executing the steps inside of the container was because I had the initial value for #month1 set to 3 (intentionally) and wanted to loop until it was resolved to -49. In the EvalExpression setting, it will evaluate until the statement is FALSE...so the evaluation I had in there of #month1 <= -49 was already false. It needed to be #month1 > -49 so as soon as it fell to -49 the statement would be false. I do this to myself more than I should admit, can't see the forest for the trees!

Get instance name from data source name

I'm using SQLDriverConnect function to connect to database. In connection string I can specify ODBC pre-configured data source name (DSN), function resolves necessary attributes and all works fine. But after successful connection I need to get instance name to which I have connected or connection port (because there can be several instances of mssql running on server). How can I implement this?
Run the following query on your connection:
select ##SERVERNAME
This will return the server and instance name
The preferred form is apparently to use SERVERPROPERY:
SELECT SERVERPROPERTY('ServerName')
which will return the server and instance name, and, unlike ##SERVERNAME, correctly returns results if the server has been renamed.

Check for the server name in connection string if it exists or not

I have done a program where in which I'll pass the server name to the connection string at run time. I need to check if the server name is valid.
Help me out with this?
Try using the SqlDataSourceEnumerator.Instance.GetDataSources() method to get a list of available SQL servers. (see MSDN: http://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources.aspx)
You alternatively might want to connect to the server in a Try..Catch block and check if the connection status is 'open'.