SSIS 2008. Very simple task. I want to retrieve a System Variable and use it in an SQL INSERT. I want to retrieve the value of System:MachineName
and use it in an insert statement.
Using the statement INSERT INTO MYLOG (COL1) SELECT @[System::MachineName]
gives the error Error: ..failed to parse. Must declare the scalar variable "@"
Using the statements SELECT @System::MachineName
or SELECT @@[System::MachineName]
gives the error 'Error Incorrect systax near '::'
I am not trying to pass a parameter to the query. I have searched for a day already but couldn't find how to do this one simple thing!
Here is one way you can do this. The following sample package was created using
SSIS 2008 R2
and uses SQL Server 2008 R2 as backend.dbo.PackageData
SQLServer
to connect to your database, say to an SQL Server database.Execute SQL Task
General
tab of the editor, set theConnection
property to your connection manager named SQLServer.SQLStatement
, enter the insert statementINSERT INTO dbo.PackageData (PackageName) VALUES (?)
Data Type
would beVARCHAR
. Set theParameter
Name to0
, which indicates the index value of the parameter. Click OK button.Hope that helps.
Along with @user756519's answer, Depending on your connection string, your variable names and SQLStatementSource Changes
Per my comment against @ZERO's answer (repeated here as an answer so it isn't overlooked by SSIS newcomers).
The OP's question is pretty much the use case for SSIS property expressions.
To pass SSIS variables into the query string one would concatenate it into an expression set for the SqlStatementSource property:
"INSERT INTO MYLOG (COL1) SELECT " + @[System::MachineName]
This is not to suggest the accepted answer isn't a good pattern, as in general, the parameterised approach is safer (against SQL injection) and faster (on re-use) than direct query string manipulation. But for a system variable (as opposed to a user-entered string) this solution should be safe from SQL injection, and this will be roughly as fast or faster than a parameterised query if re-used (as the machine name isn't changing).
I never use it before but maybe you can check out the use of expression in Execute SQL task for that.
Or just put the whole query into an expression of a variable with evaluateAsExpression set to true. Then use OLE DB to do you insert