Error on Report with 2 Views combined

Non-admin users are getting a SQL error when running a report that combines company and lead address info. I am getting an 'Invalid Column name' 'lead_secterr' error in the SQL log, but that field is in the view. Any thoughts?

Thanks in advanced. Anthony

SELECT comp_name,type,addr_address1,addr_address2,addr_address3,addr_city FROM vcompanyleadlist WITH (NOLOCK) WHERE ((lead_secterr is null OR (lead_AssignedUserId=38))) AND ( (addr_state LIKE N'%PA%' ESCAPE '|') AND Comp_PrimaryUserId = 38
)ORDER BY comp_name ASC,type ASC,addr_address1 ASC,addr_address2 ASC,addr_address3 ASC,addr_city ASC
Invalid column name 'lead_secterr'

CREATE VIEW vCompanyLeadList
AS
SELECT [vCompany].[Comp_CompanyId], [vCompany].[Comp_Name], [vAddressCompany].[Addr_Address1], [vAddressCompany].[Addr_Address2], [vAddressCompany].[Addr_Address3], [vAddressCompany].[Addr_City],
[vAddressCompany].[Addr_State], [vAddressCompany].[Addr_PostCode], Comp_SecTerr, Comp_PrimaryUserID, 'Company' AS Type, Comp_createdby, Comp_channelid
FROM [CRM].[dbo].[vAddressCompany] [vAddressCompany] INNER JOIN
[CRM].[dbo].[vCompany] [vCompany] ON [vAddressCompany].[AdLi_CompanyID] = [vCompany].[Comp_CompanyId]
UNION ALL
SELECT Lead_LeadID, Lead_CompanyName, Lead_CompanyAddress1, Lead_CompanyAddress2, Lead_CompanyAddress3, Lead_CompanyCity, Lead_CompanyState, Lead_CompanyPostCode,
lead_secterr, lead_assignedUserID, 'Lead' AS Type, Lead_createdby, lead_channelid
FROM dbo.vListLead

  • 0

    You have to include the secterr field by name.

    When you use UNION or UNION ALL, the first select statements columns will be used, example:

    I am selecting pers_firstname and pers_personid, but if I try and select them in the query:

    So, actually in your statement, there is no column named 'lead_secterr', the data will be there, but it will be named 'comp_secterr'.

    Hope that helps