1
INSERT INTO [DEV_BI].dbo.[DimAktivitet]([Beskrivning],[företag],[Projektnummer],[Aktivitet],
            loaddate)
SELECT NULL,
       a.DATAAREAID,
       a.PROJID,
       a.MID_ACTIVITYNUMBER,
       GETDATE()
FROM [?].dbo.[v_ProjCostTrans_ProjEmplTrans] a
LEFT OUTER JOIN [DEV_BI] .dbo.[DimAktivitet] b ON a.MID_ACTIVITYNUMBER = b.Aktivitet
AND a.DataAreaID = b.företag
AND a.ProjID = b.Projektnummer
WHERE b.Aktivitet_key IS NULL

我在执行 sql 任务和参数映射中有上面的 sql 代码,我映射了一个名为 user::connectionstring 的变量,数据类型为 nvarchar ,参数名称 = 0。我收到以下错误。

[执行 SQL 任务] 错误:执行查询“插入 [DEV_BI].dbo.[Dimaktivitet]([Beskrivni...”失败,出现以下错误:“无效的对象名称 '?.dbo.v_ProjCostTrans_ProjEmplTrans'。”。可能失败原因:查询有问题,“ResultSet”属性设置不正确,参数设置不正确,或者连接没有正确建立。

请有人帮我解决这个问题。

4

1 回答 1

2

It appears you are trying to change the database based on a variable. The Execute SQL Task can only use parameters as filters in the WHERE clause. This behavior is described in this TechNet article. For instance, you could do this:

insert into [DEV_BI].dbo.[DimAktivitet]([Beskrivning],[företag],[Projektnummer],[Aktivitet],loaddate)  
select null,a.DATAAREAID,a.PROJID,a.MID_ACTIVITYNUMBER,GETDATE() from 
[DEV_BI].dbo.[v_ProjCostTrans_ProjEmplTrans] a 
left outer join 
[DEV_BI] .dbo.[DimAktivitet] b
on a.MID_ACTIVITYNUMBER = b.Aktivitet AND a.DataAreaID = b.företag AND a.ProjID = b.Projektnummer
where b.Aktivitet_key is null
AND b.SomeFilterCriteria = ?;

If you really want to vary the database based on a variable, then you have three options:

  1. Vary the Connection Manager connection string to your database connection based on an expression as described in a blog post. This is the best solution if you are only changing the database and nothing else.

  2. Generate the entire SQL code as a variable and execute a variable as the SQL command instead of passing variables to the Execute SQL Command. This is described in this blog post under the section "Passing in the SQL Statement from a Variable".

  3. Create a stored procedure, pass the parameter to the stored procedure, and let it generate the SQL it needs on the fly.

于 2013-04-01T19:48:29.453 回答