2016-07-15 37 views
-1

enter image description here有工作的SQL查詢C#錯誤 - 變量名已經被聲明

下面的查詢工作正常在SQL Management Studio中和的Altiris。當我們嘗試將代碼添加到用C#編寫的自定義報告工具時,我們會收到上述錯誤。

Declare @AcroPro  int 
Declare @AcroStan  int 

set @AcroPro = (select COUNT(distinct guid) from vAdobe_Acrobat_Pro where [Display Name] like '%Professional') 
set @AcroStan = (select COUNT(distinct guid) from vAdobe_Acrobat_Stan where [Display Name] like '%Standard') 

select distinct v2.name 'Software Product', t2.[Purchased Licenses], CASE 

     When t2._resourceguid = '8708D8B8-51A0-4386-B4A1-620CC63DEF0D' 
     Then @AcroPro 
     When t2._resourceguid = '37997FB9-9936-42CF-A34C-D68214C353CF' 
     Then @AcroStan 
     End [Active Licenses], CASE 

     When t2._resourceguid = '8708D8B8-51A0-4386-B4A1-620CC63DEF0D' 
     Then t2.[purchased licenses] - @AcroPro 
     When t2._resourceguid = '37997FB9-9936-42CF-A34C-D68214C353CF' 
     Then t2.[purchased licenses] [email protected] 
     End [Compliance] from vComputer v1 

inner join Inv_SoftwareProduct_InstallationInfo t1 on v1.Guid = t1._computerResourceGuid 
inner join Inv_SoftwareProduct_ComplianceInfo t2 on t1._ResourceGuid = t2._ResourceGuid 
inner join vSoftwareProduct v2 on t2._ResourceGuid = v2.guid 

where v2.name in ('Adobe Acrobat Professional', 'Adobe Acrobat Standard') 

and v1.IsManaged = '1' 
+1

顯然,錯誤與您的自定義報告工具有關。但是由於您沒有發佈任何與此相關的代碼,我們無法爲您做任何事情。 – sstan

+0

也許你可以嘗試在查詢的末尾和/或開頭添加「GO」。 – BJones

+0

使用事件探查器來捕獲您發送到服務器的實際SQL。它應該解釋你得到的錯誤。 –

回答

2

你的報告工具可能會發送相同的一組SQL命令在同一個查詢批處理兩次,在這種情況下,這些變量將被重新申報。

+0

謝謝你的迴應。我們對C#中的SQL調用進行了調整,現在它正在工作。 – user3009669