Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2012-02-29 : 14:36:02
|
I run the stored procedurSET @industryCriteria = N''IF @RegulatedOnly = 1 BEGIN SET @industryCriteria = @industryCriteria + N' AND usi.regStatusType = 1' ENDI get the following error that saysMsg 4104, Level 16, State 1, Line 1The multi-part identifier "usi.regstatusType" could not be bound. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-29 : 14:42:18
|
the error is not here but place where you're trying to execute it in EXECcheck if you've column regStatusType present in table referred by alias usi------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
X002548
Not Just a Number
15586 Posts |
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2012-02-29 : 14:51:49
|
Here's all my codeUSE [IURC]GO/****** Object: StoredProcedure [dbo].[Polaris_ReportSP_Rates_Addresses] Script Date: 02/29/2012 14:45:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO/****** Object: StoredProcedure [dbo].[Polaris_ReportSP_Rates_Addresses] Script Date: 06/21/2011 1:45:00 ******//************************************************************************************************** Name : Polaris_ReportSP_Rates_Addresses** Last Updated : 6/21/2011** Purpose : Selects the information for the Rates Addresses Report.** Changes : (6/13/11) Updated revised SP to work around missing addresses with subindustries (telecom)** : (6/21/11) Fixed problem bring in other industries (ie. Video) with NULL subindustries** Author : Kevin Dostalek*************************************************************************************************/ALTER PROCEDURE [dbo].[Polaris_ReportSP_Rates_Addresses] @IndustryID INT = 0, @Category INT = 0, @RegulatedOnly BIT = 0, @ArRequiredOnly BIT = 0, @FeeBilledOnly BIT = 0, @ReportClass INT = 0, @SortBy1 CHAR(1) = NULL, @SortBy2 CHAR(1) = NULL, @SortBy3 CHAR(1) = NULLASDECLARE @strSQL NVARCHAR(3000)DECLARE @orderBy NVARCHAR(2000)DECLARE @orderByCount INTDECLARE @industryCriteria NVARCHAR(500)SET NOCOUNT ONSET @industryCriteria = N''IF @RegulatedOnly = 1 BEGIN SET @industryCriteria = @industryCriteria + N' usi.regStatusType = 1' ENDIF @ArRequiredOnly = 1 BEGIN SET @industryCriteria = @industryCriteria + N' AND ui.requiresAnnualRpt = 1' ENDIF @ReportClass <> 0 BEGIN SET @industryCriteria = @industryCriteria + N' AND ar.ReportClass = ' + CAST(@ReportClass AS NVARCHAR(15)) ENDSET @strSQL = N'SELECT DISTINCT u.UtilityID, it.TypeID AS IndustryTypeID, 'IF @IndustryID = 0 SET @strSQL = @strSQL + N'''Water/Sewer'''ELSE SET @strSQL = @strSQL + N'it.Description'SET @strSQL = @strSQL + N' AS Industry,uot.Description AS Ownership, arct.Description AS ReportClassDescription, u.Name AS UtilityName,ua.AttnName, ua.AddrLine1, ua.AddrLine2, ua.City,usp.Name AS State, ua.PostalCode, ua.Phone, uc.Name AS CountryFROM UtilityIndustry ui INNER JOIN IndustryTypes it ON ui.IndustryType = it.TypeID INNER JOIN Utility u ON ui.UtilityID = u.UtilityID INNER JOIN UtilityOwnershipTypes uot ON u.OwnershipType = uot.TypeID INNER JOIN UtilityAddress ua ON ui.IndustryID = ua.UtilityIndustryID and ua.AddressType = 1 INNER JOIN URCStateProvince usp ON ua.StateID = usp.StateID INNER JOIN URCCountry uc ON ua.CountryID = uc.CountryIDinner join ( select u3.utilityid, sit3.description as [description], rst3.description as [status] from utility u3 inner join utilityindustry i3 on u3.utilityid=i3.utilityid inner join utilitysubindustry si3 on i3.industryid=si3.industryid inner join utilityregstatus rs3 on si3.subindustryid=rs3.subindustryid inner join regulatorystatustypes rst3 on rs3.statustype=rst3.typeid left outer join subindustrytypes sit3 on si3.subindustrytype=sit3.typeid where rs3.dateeffective=(select max(dateeffective) from utilityregstatus where subindustryid=rs3.subindustryid) ) as sit on u.utilityid=sit.utilityid LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID LEFT OUTER JOIN AnnualReport ar ON ui.IndustryID = ar.UtilityIndustryID AND ly.LastYearOnFile = ar.Year LEFT OUTER JOIN AnnualReportClassTypes arct ON ar.ReportClass = arct.TypeID WHERE '-- If industry = 0 then water/sewerIF @IndustryID = 0 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct ui.utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID LEFT OUTER JOIN AnnualReport ar ON ui.IndustryID = ar.UtilityIndustryID AND ly.LastYearOnFile = ar.Year WHERE industryType = 3' + @industryCriteria + ') AND ui.UtilityID in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ')' ENDELSE IF @IndustryID = 3 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 3' + @industryCriteria + ') AND ui.UtilityID not in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ')' ENDELSE IF @IndustryID = 4 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct ui.utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ') AND ui.UtilityID not in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 3' + @industryCriteria + ')' ENDELSE BEGIN SET @strSQL = @strSQL + 'ui.IndustryType = ' + CAST(@IndustryID AS NVARCHAR(15)) + @industryCriteria ENDIF @FeeBilledOnly = 1 BEGIN SET @strSQL = @strSQL + ' AND u.feeBill = ''YES''' ENDIf @Category = 1 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'', ''CLEC-R'')' ENDIf @Category = 2 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'')' ENDIf @Category = 3 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-R'')' ENDIf @Category = 4 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''AOS'')' ENDIf @Category = 5 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CAP'')' ENDIf @Category = 7 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''WIRELESS'')' ENDIf @Category = 9 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'', ''AOS'', ''CAP'', ''IXC'', ''WIRELESS'')' ENDquote: Originally posted by X002548 I have no problem. You need to post more of your codeDECLARE @industryCriteria varchar(8000), @RegulatedOnly intSET @RegulatedOnly = 1SET @industryCriteria = N''IF @RegulatedOnly = 1BEGINSET @industryCriteria = @industryCriteria + N' AND usi.regStatusType = 1'END Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
|
 |
|
X002548
Not Just a Number
15586 Posts |
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2012-02-29 : 15:14:39
|
Here's the complete code along with execUSE [IURC]GO/****** Object: StoredProcedure [dbo].[Polaris_ReportSP_Rates_Addresses] Script Date: 02/29/2012 14:45:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO/****** Object: StoredProcedure [dbo].[Polaris_ReportSP_Rates_Addresses] Script Date: 06/21/2011 1:45:00 ******//************************************************************************************************** Name : Polaris_ReportSP_Rates_Addresses** Last Updated : 6/21/2011** Purpose : Selects the information for the Rates Addresses Report.** Changes : (6/13/11) Updated revised SP to work around missing addresses with subindustries (telecom)** : (6/21/11) Fixed problem bring in other industries (ie. Video) with NULL subindustries** Author : Kevin Dostalek*************************************************************************************************/ALTER PROCEDURE [dbo].[Polaris_ReportSP_Rates_Addresses] @IndustryID INT = 0, @Category INT = 0, @RegulatedOnly BIT = 0, @ArRequiredOnly BIT = 0, @FeeBilledOnly BIT = 0, @ReportClass INT = 0, @SortBy1 CHAR(1) = NULL, @SortBy2 CHAR(1) = NULL, @SortBy3 CHAR(1) = NULLASDECLARE @strSQL NVARCHAR(3000)DECLARE @orderBy NVARCHAR(2000)DECLARE @orderByCount INTDECLARE @industryCriteria NVARCHAR(500)SET NOCOUNT ONSET @industryCriteria = N''IF @RegulatedOnly = 1 BEGIN SET @industryCriteria = @industryCriteria + N' AND sit.Status = ''Regulated''' ENDIF @ArRequiredOnly = 1 BEGIN SET @industryCriteria = @industryCriteria + N' AND ui.requiresAnnualRpt = 1' ENDIF @ReportClass <> 0 BEGIN SET @industryCriteria = @industryCriteria + N' AND ar.ReportClass = ' + CAST(@ReportClass AS NVARCHAR(15)) ENDSET @strSQL = N'SELECT DISTINCT u.UtilityID, it.TypeID AS IndustryTypeID, 'IF @IndustryID = 0 SET @strSQL = @strSQL + N'''Water/Sewer'''ELSE SET @strSQL = @strSQL + N'it.Description'SET @strSQL = @strSQL + N' AS Industry,uot.Description AS Ownership, arct.Description AS ReportClassDescription, u.Name AS UtilityName,ua.AttnName, ua.AddrLine1, ua.AddrLine2, ua.City,usp.Name AS State, ua.PostalCode, ua.Phone, uc.Name AS CountryFROM UtilityIndustry ui INNER JOIN IndustryTypes it ON ui.IndustryType = it.TypeID INNER JOIN Utility u ON ui.UtilityID = u.UtilityID INNER JOIN UtilityOwnershipTypes uot ON u.OwnershipType = uot.TypeID INNER JOIN UtilityAddress ua ON ui.IndustryID = ua.UtilityIndustryID and ua.AddressType = 1 INNER JOIN URCStateProvince usp ON ua.StateID = usp.StateID INNER JOIN URCCountry uc ON ua.CountryID = uc.CountryIDinner join ( select u3.utilityid, sit3.description as [description], rst3.description as [status] from utility u3 inner join utilityindustry i3 on u3.utilityid=i3.utilityid inner join utilitysubindustry si3 on i3.industryid=si3.industryid inner join utilityregstatus rs3 on si3.subindustryid=rs3.subindustryid inner join regulatorystatustypes rst3 on rs3.statustype=rst3.typeid left outer join subindustrytypes sit3 on si3.subindustrytype=sit3.typeid where rs3.dateeffective=(select max(dateeffective) from utilityregstatus where subindustryid=rs3.subindustryid) ) as sit on u.utilityid=sit.utilityid LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID LEFT OUTER JOIN AnnualReport ar ON ui.IndustryID = ar.UtilityIndustryID AND ly.LastYearOnFile = ar.Year LEFT OUTER JOIN AnnualReportClassTypes arct ON ar.ReportClass = arct.TypeID WHERE '-- If industry = 0 then water/sewerIF @IndustryID = 0 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct ui.utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID LEFT OUTER JOIN AnnualReport ar ON ui.IndustryID = ar.UtilityIndustryID AND ly.LastYearOnFile = ar.Year WHERE industryType = 3' + @industryCriteria + ') AND ui.UtilityID in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ')' ENDELSE IF @IndustryID = 3 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 3' + @industryCriteria + ') AND ui.UtilityID not in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ')' ENDELSE IF @IndustryID = 4 BEGIN SET @strSQL = @strSQL + 'ui.UtilityID in (select distinct ui.utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 4' + @industryCriteria + ') AND ui.UtilityID not in (select distinct utilityID from utilityIndustry ui LEFT OUTER JOIN UtilitySubIndustryview usi ON ui.industryID = usi.industryID LEFT OUTER JOIN (SELECT UtilityIndustryID, max(year) as LastYearOnFile FROM AnnualReport GROUP BY UtilityIndustryID) ly ON ui.IndustryID = ly.UtilityIndustryID WHERE industryType = 3' + @industryCriteria + ')' ENDELSE BEGIN SET @strSQL = @strSQL + 'ui.IndustryType = ' + CAST(@IndustryID AS NVARCHAR(15)) + @industryCriteria ENDIF @FeeBilledOnly = 1 BEGIN SET @strSQL = @strSQL + ' AND u.feeBill = ''YES''' ENDIf @Category = 1 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'', ''CLEC-R'')' ENDIf @Category = 2 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'')' ENDIf @Category = 3 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-R'')' ENDIf @Category = 4 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''AOS'')' ENDIf @Category = 5 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CAP'')' ENDIf @Category = 7 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''WIRELESS'')' ENDIf @Category = 9 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-F'', ''AOS'', ''CAP'', ''IXC'', ''WIRELESS'')' ENDIf @Category = 10 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''ILEC'')' ENDIf @Category = 11 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''IXC'')' ENDIf @Category = 12 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''ILEC'', ''CLEC-R'', ''CLEC-F'')' ENDIf @Category = 13 BEGIN SET @strSQL = @strSQL + ' AND sit.Description IN (''CLEC-R'', ''TOLLRESELLER'')' ENDIF @Category = 14 BEGIN SET @strSQL = @strSQL + ' AND sit.Description = ''TOLLRESELLER''' ENDIF @Category = 16 BEGIN SET @strSQL = @strSQL + ' AND sit.Description = ''REMC''' ENDSET @orderBy = ' ORDER BY'SET @orderByCount = 0IF @SortBy1 = 'c' BEGIN SET @orderBy = @orderBy + ' arct.Description' ENDELSE IF @SortBy1 = 'n' BEGIN SET @orderBy = @orderBy + ' u.Name' ENDELSE IF @SortBy1 = 'o' BEGIN SET @orderBy = @orderBy + ' uot.Description' ENDIF @SortBy2 = 'c' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' arct.Description' ENDELSE IF @SortBy2 = 'n' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' u.Name' ENDELSE IF @SortBy2 = 'o' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' uot.Description' ENDIF @SortBy3 = 'c' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' arct.Description' ENDELSE IF @SortBy3 = 'n' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' u.Name' ENDELSE IF @SortBy3 = 'o' BEGIN SET @orderByCount = @orderByCount + 1 IF @orderByCount > 0 SET @orderBy = @orderBy + ', ' SET @orderBy = @orderBy + ' uot.Description' ENDIF @orderByCount > 0 SET @strSQL = @strSQL + @orderByEXECUTE(@strSQL)RETURN @@ERRORquote: Originally posted by X002548 So where's the execute?Do a PRINT of @strSQ and see what was created...post that hereBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
|
 |
|
X002548
Not Just a Number
15586 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-29 : 16:06:20
|
Unless we see your PRINT(@SQL) result we cant suggest anything as we dont know what query its building up------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|