如何将系统变量值传递给执行 SQL 任务中的 SQL 语句? [英] How do I pass system variable value to the SQL statement in Execute SQL task?
问题描述
SSIS 2008.非常简单的任务.我想检索系统变量并在 SQL INSERT 中使用它.我想检索 System:MachineName
的值并在插入语句中使用它.
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.
使用语句 INSERT INTO MYLOG (COL1) SELECT @[System::MachineName]
给出错误 Error: ..failed to parse.必须声明标量变量@"
Using the statement INSERT INTO MYLOG (COL1) SELECT @[System::MachineName]
gives the error Error: ..failed to parse. Must declare the scalar variable "@"
使用语句 SELECT @System::MachineName
或 SELECT @@[System::MachineName]
给出错误 'Error Incorrect systax near'::'
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!
推荐答案
这是您可以执行此操作的一种方法.以下示例包是使用 SSIS 2008 R2
创建的,并使用 SQL Server 2008 R2 作为后端.
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.
- 在 SQLServer 数据库中创建一个名为
dbo.PackageData
的示例表
- 创建一个 SSIS 包.
- 在 SSIS 上,添加名为
SQLServer
的 OLE DB 连接管理器以连接到您的数据库,例如连接到 SQL Server 数据库. - 在控制流选项卡上,拖放一个
执行 SQL 任务
- 双击执行 SQL 任务以显示执行 SQL 任务编辑器.
- 在编辑器的
General
选项卡上,将Connection
属性设置为名为 SQLServer 的连接管理器. - 在属性
SQLStatement
中,输入插入语句INSERT INTO dbo.PackageData (PackageName) VALUES (?)
- Create an SSIS package.
- On the SSIS, add an OLE DB connection manager named
SQLServer
to connect to your database, say to an SQL Server database. - On the Control flow tab, drag and drop an
Execute SQL Task
- Double-click on the Execute SQL task to bring the Execute SQL Task Editor.
- On the
General
tab of the editor, set theConnection
property to your connection manager named SQLServer. - In the property
SQLStatement
, enter the insert statementINSERT INTO dbo.PackageData (PackageName) VALUES (?)
- 在参数映射"选项卡上,单击添加"按钮,选择要使用的包变量.相应地更改数据类型.此示例将 PackageName 插入表中,因此
Data Type
将是VARCHAR
.将Parameter
Name设置为0
,表示参数的索引值.单击确定"按钮.
- On the Parameter Mapping tab, click Add button, select the Package variable that you would like to use. Change the data type accordingly. This example is going to insert the PackageName into a table, so the
Data Type
would beVARCHAR
. Set theParameter
Name to0
, which indicates the index value of the parameter. Click OK button.
- 执行包.
- 您会注意到表中插入了一条新记录.我将包名称保留为 Package.这就是为什么表
希望有所帮助.
这篇关于如何将系统变量值传递给执行 SQL 任务中的 SQL 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!