• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 52
  • Last Modified:

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!

3 Solutions
É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

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

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now