sql - TSQL table variable in WHERE statement causes error -


/****** script selecttopnrows command ssms  ******/ declare @user int; declare @aipnip varchar(20); declare @accesstobasecompanies table (basecompanyid int);  set @user = 1;  insert @accesstobasecompanies (basecompanyid)  ((select c.basecompanyfk [dbo].companies c c.companiesteamfk in (select u.companiesteamfk [dbo].[companiesteams_users] u userfk = @user)) union (select c.basecompanyfk [dbo].beneficiaries c c.departmentfk in (select u.departmentfk [dbo].[departments_users] u userfk = @user)))  set @aipnip = (select top 1 fc.[purenip] [dbo].[basecompanies] bc inner join [dbo].[companies] c on bc.id = c.basecompanyfk , c.companytype = 1 inner join [dbo].[payment_partners] fc on fc.id = bc.companypartnerfk)  select bc.[id]       ,bc.[name] 'basecompany'       ,sum(cd.[paidamountnavireo]) - sum(cd.[grosstotal])       ,sum(case when (ps.id = 1 or ps.id = 3)   [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    bc.[id] in @accesstobasecompanies  team bc.[id], bc.[name] 

@accesstobasecompanies not empty, why when try execute query error: incorrect syntax near '@accesstobasecompanies'.

the argument in value list or subquery, not table. try:

where bc.[id] in (select basecompanyid @accesstobasecompanies) 

Comments