SQL Server Upgrade Advisor "Order by"


The SQL Upgrade Advisor gives me warnings about the order by of this query

"Column aliases in ORDER BY clause cannot be prefixed by table alias". I understand that if these columns exist in the table I should have no problems upgrading the database from SQL 2000 to SQL 2008?

  @nExpePeri Int,
  @nExpeNumb BigInt,
  @nDocuCode Int
  SELECT right(convert(varchar(4),D.nExpePeri),2) + '-' + right('000000' + convert(varchar(6),D.nExpeNumb),6) tNumeExpe,	
         D.nDocuNumb, D.cExpeAntg, 
         tTipoDocu = (select Tipo_Documento.sDocuDesc from Tipo_Documento where Tipo_Documento.cDocuType = D.cDocuType),
         cDocuCode = Right('000' + CONVERT(VARCHAR,d.cDocuCode),3),
         convert(varchar(10),D.dDocuDate,103) as dDocuDate,
         tDocuAdju = dbo.Concatena_Documentos_Adjuntos(D.nExpePeri, D.nExpeNumb, D.cDocuCode),
         tDocuRefe = dbo.Concatena_Documentos_Referencia(D.nExpePeri, D.nExpeNumb, D.cDocuCode),
         D.sDigiName, D.cConfFlag,
         tEstaDesc = (select sEstaDesc from estado_documento where cEstaCode = D.cEstaCode),
         Lugar = (SELECT sDistDesc FROM Distrito WHERE cDistCode = D.cDistCode AND cProvCode = D.cProvCode AND cDepaCode = D.cDepaCode),
         tUseAten = (SELECT IsNull(ra.InstTSigla,ra.InstTDescrip) + '-' + IsNull(rb.UOrgTSigla,rb.UOrgTDescrip) + '-' + IsNull(rc.PerTDescrip,'')
                       FROM oeimInstitucion ra, oeimUnidOrg rb, oeimPersonal rc
                      WHERE rb.UOrgCCodInst = ra.InstCCod AND rc.PerCCodUOrg = rb.UOrgCCod AND rc.PerCCod = D.cUseACode),
         D.nTramDias, --Lugar = dbo.ConcatenaLugar(cPaisCode, cCityCode)
         Lugar = dbo.TraerLugar(D.cPaisCode, D.cDepaCode,D.cProvCode, D.cCityCode)
    FROM Documento D 
   where D.nExpePeri = @nExpePeri
     and D.nExpeNumb = @nExpeNumb
     and D.cDocuCode = @nDocuCode
     and D.cElimFlag = 0
   order by D.nExpePeri asc, D.nExpeNumb asc, D.cDocuCode asc


Open in new window

I hope you can help me!

JnavarroMcIT ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
I don't have SQL 2008 but just remove the aliases from the Order By clause. Have a look at https://technet.microsoft.com/en-us/library/ee240807(v=sql.120).aspx

BTW, mainstream support for SQL 2008 has ended: https://blogs.msdn.microsoft.com/sqlreleaseservices/end-of-mainstream-support-for-sql-server-2008-and-sql-server-2008-r2/
Nitin SontakkeDeveloperCommented:
May be completely unrelated, but it appears that you have defined column Lugar twice.

Also, as per link provided by Éric Moreau, the following, could potentially be a problem:

cDocuCode = Right('000' + CONVERT(VARCHAR,d.cDocuCode),3)

Open in new window

PortletPaulEE Topic AdvisorCommented:
The third reference in the order by clause refers to a column (d.cDocuCode) that is also involved in a column alias (cDocuCode), so

order by D.nExpePeri asc, D.nExpeNumb asc, Right('000' + CONVERT(VARCHAR,d.cDocuCode),3) asc

Open in new window

order by D.nExpePeri asc, D.nExpeNumb asc, cDocuCode asc

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
larryhSr. Software EngineerCommented:
I think what it's asking for is an ORDER BY statement like the following:
order by D.nExpePeri asc, D.nExpeNumb asc, Right('000' + CONVERT(VARCHAR,d.cDocuCode),3) asc

Open in new window

You can't use the alias for the column ("cDocuCode") with a table alias ("D").  You have to re-specify the calculated column in the ORDER BY clause.
JnavarroMcIT ConsultantAuthor Commented:
Thank you very much!! I could correct it. Sorry for the delay!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.