Passing SQL Server Name of SSIS Job to package variable - sql-server

I am trying to figure out a way that I can pass the SQL Sever Name an SSIS Job is running on, to a variable within a package.
Basically, which ever Server this Job is running on, will be passed to the ServerName property in an Ole DB Connection in the package so the data is loaded into that server.
I have been looking at documentation on package configuration, and I feel like it would be in the environment variable section. However, I do not think any of the listed "environment variables" are the server name. I have Googled this issue, and searched on StackOverflow for problems, but I cannot seem to find this problem.

There are probably a few different approaches, but I think you can accomplish this with a simple Execute SQL Task.
Create a new Execute SQL Task
Under General -> SQL Statement, enter your query Select ##SERVERNAME as ServerName in the "SQLStatement" field
Under General -> Result Set, choose "Single row"
Under Parameter Mapping, Enter your variable (create one if you don't have one) User::ServerName in Variable Name, "Input" as direction, 0 as Parameter Name, and -1 as Parameter Size
Under Result Set, enter "ServerName" for your Result Name and type the variable in Variable Name
Click OK
Give that a go and see if it accomplishes what you want.

I was able to accomplish this by using a script task, and C# code to grab the Machine Name, or IP address of the machine running it.
Dts.Variables["User::ServerName"].Value = System.Net.Dns.GetHostName();

Related

ODI getting agent name and server

I wanted to know if there is a method to get the Agent name and the server where it's running. I tried reading all the OdiGetInfo() material, but couldn't find anything helpful.
I need these informations since I have to do an ODI package that constantly check if an Agent and the server are up & running.
I know it's possible to do that using a shell script, executed from the ODI package, but I wanted to know if there is another way to do that.
Thank you.
Method to get Agent Name: odiRef.getSession("AGENT_NAME")
Method to get Server details: odiRef.getInfo("DEST_JAVA_URL")
Note: DEST_JAVA_URL parameter returns the entire JDBC URL mentioned in Physical Data server.

Using a scalar as a condition in an OLE DB data flow

I am having a problem with a data flow task in an ssis package i am trying to build. The objective of the package is to update tables situated in our local server using a connection to a distant server containing the source of the data, through a vpn connection.
There are no problems for tables which are re-downloaded entirely.
But some of the tables must be updated for real. What I mean is they're not re-downloaded. For each of those tables, I have to check the maximum value of the date column in our local server (int YYYMMDD type) and ask the package to download only the data added after that date.
I thought about using a scalar (#MAXDATE for ex) but the issue is, I have to declare this scalar in a session with our local server, and I cannot use it as a condition in an OLE DB Source task, because the latter implies a new session, this time with the distant server.
I can only view the database on the distant server and import it. So no way to create a table on it.
I hope it is clear enough. Would you have any tips to solve this problem?
Thank you in advance.
Ozgur
You can do this easily by using an execute SQL Task, a Data Flow task and one variable. I would probably add some error checking just in case no value is found on the local system, but that depends very much on what might go wrong.
Assuming VS2008
Declare a package level variable of type datetime. Give it an appropriate default value.
Create an Execute SQL Task with a query that returns the appropriate date value. On the first page of the properties window, make sure the Result Set is set to "Single Row." On the Result Set page, map the date column to the package variable.
Create a Data Flow task. In the OLE DB Data Source, write your query to include a question mark for the incoming date value. "and MaxDate>?". Now when you click on the Paramaters button, you should get a pop-up that allows you to map "Parameter0" to your package level variable.

finding the correct connection string for a local SQL instance

I'm trying to build a connection string for a test environment that will connect to the local SQL Server instance on different machines. The purpose of this is so that a developer can checkout the code from TFS, build it, and run the testcases, connecting to his local DB. The problem is that different developer's machines may have different SQL Server setups. In particular, some may be running the full server, others may be running SQL Server Express.
I'm trying to right a utility routine that will take template connection string (e.g., Data Source=(local); Initial Catalog= myDB; Integrated Security=SSPI;) and modify the Data Source to work with the local server.
I've tried using SmoApplication.EnumAvailableServers() (returns an empty table, regardless of whether I user true or false parameters), and SqlDataSourceEnumerator.GetDataSources() (returns 2888 servers from the network, but none on the local machine), SQLCMD -L (returns nothing).
Any suggestions?
In the alternative, is there an easy way to tell whether a particular connection string will connect to a server (without waiting for it to timeout if it doesn't). If I could find the answer to that, I could try the likely suspects until I got one to work.
you might try to get the connection string as following:
Create a new blank file and name it test.udl.
Double click on it, and a "Data Link Properties" dialog should appear.
On "Providers" tab, select "Microsoft OLE DB Provider for SQL Server" or "SQL Native Client"
On "Connections" tab, try various settings and use the "Test Connection" button to test them. Click "Ok" when it works.
Open the test.udl file in Notepad and copy the line that starts with "Provider=" into your Web.config "ConnectionString" value, BUT delete the little part that says "Provider=SQLNCLI.1;"
If you want each developer to work with their own local SQL server, then the ADO connection string should have the Data Source set to localhost
... ; Data Source=localhost; ...
Additionally, to get a list of current servers, go to the command line and run
osql -L
You can look in the registry to find all local SQL Server instances. This key contains the list: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL.
Each named instance will have a value in this key. For named instances the name of the value is the same as the name of the instance. For the default instance the value will be named MSSQLSERVER.
This will do the trick:
Data Source=.\SQLEXPRESS

What is the point of "Initial Catalog" in a SQL Server connection string?

Every SQL Server connection string I ever see looks something like this:
Data Source=MyLocalSqlServerInstance;Initial Catalog=My Nifty Database;
Integrated Security=SSPI;
Do I need the Initial Catalog setting? (Apparently not, since the app I'm working on appears to work without it.)
Well, then, what's it for?
If the user name that is in the connection string has access to more then one database you have to specify the database you want the connection string to connect to. If your user has only one database available then you are correct that it doesn't matter. But it is good practice to put this in your connection string.
This is the initial database of the data source when you connect.
Edited for clarity:
If you have multiple databases in your SQL Server instance and you don't want to use the default database, you need some way to specify which one you are going to use.
Setting an Initial Catalog allows you to set the database that queries run on that connection will use by default. If you do not set this for a connection to a server in which multiple databases are present, in many cases you will be required to have a USE statement in every query in order to explicitly declare which database you are trying to run the query on. The Initial Catalog setting is a good way of explicitly declaring a default database.

How can I get the name of the database server from within a stored procedure?

I'm making complete connection strings inside my procedure and would like to inject the name of the database server in them. Is there any way that I can detect the name from inside or am I doomed to passing it in?
Use SERVERPROPERTY:
SERVERPROPERTY('MachineName'): name of the SQL Server host name, cluster aware
SERVERPROPERTY('ComputerNamePhysicalNetBIOS'): name of physical machine name. In a cluster, is the name of the current active node. On a standalone instalation, is identical with MachineName
SERVERPROPERTY('InstanceName'): name of the current SQL Server instance. NULL for default.
One thing I'd recomend against is the dreaded ##SERVERNAME. This property is notorious for getting out of sync with reality after a machine rename. I've seen way too many apps burned by this problem to place any trust on it. The correct rename procedure is in BOL, but few use it properly: How to: Rename a Computer that Hosts a Stand-Alone Instance of SQL Server.
So for MS SQL Server you could:
SELECT ##SERVERNAME
For MySQL, it's
SELECT variable_value as servername
FROM information_schema.global_variables
WHERE variable_name = 'hostname';
For Oracle, its:
SELECT global_name FROM global_name

Resources