0
/****** Script for SelectTopNRows command from SSMS  ******/
DECLARE @user int;
DECLARE @aipNip varchar(20);
DECLARE @accessToBaseCompanies TABLE (baseCompanyId INT);

SET @user = 1;

INSERT INTO @accessToBaseCompanies (baseCompanyId) 
((SELECT c.BaseCompanyFk FROM [dbo].Companies c WHERE c.CompaniesTeamFk IN (SELECT u.CompaniesTeamFk FROM [dbo].[CompaniesTeams_Users] u WHERE UserFk = @user))
UNION ALL
(SELECT c.BaseCompanyFk FROM [dbo].Beneficiaries c WHERE c.DepartmentFk IN (SELECT u.DepartmentFk FROM [dbo].[Departments_Users] u WHERE UserFk = @user)))

SET @aipNip = (SELECT TOP 1 fc.[PureNip]
FROM [dbo].[BaseCompanies] bc
INNER JOIN [dbo].[Companies] c ON bc.Id = c.BaseCompanyFk AND c.CompanyType = 1
INNER JOIN [dbo].[Payment_Partners] fc ON fc.id = bc.CompanyPartnerFk)

SELECT bc.[Id]
      ,bc.[Name] as 'BaseCompany'
      ,SUM(cd.[PaidAmountNavireo]) - SUM(cd.[GrossTotal])
      ,SUM(CASE WHEN (ps.Id = 1 OR ps.Id = 3)
  FROM [dbo].[BaseCompanies] bc
  INNER JOIN [dbo].[Payment_CostDocuments] cd ON bc.Id = cd.BaseCompanyFk
  INNER JOIN [dbo].[PaymentStatuses] ps ON ps.Id = cd.PaymentStatusFk
  INNER JOIN [dbo].[Payment_Partners] fc ON fc.Id = cd.PartnerFk

  WHERE bc.[Id] IN @accessToBaseCompanies

Team BY
bc.[Id],
bc.[Name]

@accessToBaseCompanies が空ではないため、クエリを実行しようとするとエラーが発生するのはなぜですか: '@accessToBaseCompanies' 付近の構文が正しくありません。

4

3 に答える 3

12

への引数INは、テーブルではなく値リストまたはサブクエリです。試す:

WHERE bc.[Id] IN (select baseCompanyId from @accessToBaseCompanies)
于 2013-07-15T11:17:27.713 に答える
4

問題は次の行です。

WHERE bc.[Id] IN @accessToBaseCompanies

そのはず:

WHERE bc.[Id] IN (select baseCompanyId from @accessToBaseCompanies)
于 2013-07-15T11:18:15.563 に答える