0

我们想在 Visual Studio 数据集设计器中为此查询的“排序依据”使用一个参数:

SELECT Students.ID, Students.Surname, Students.Forename, Students.DateOfBirth, Parents.MotherName, 
       Parents.FatherName, Parents.AddressLine1, Parents.AddressLine2, Parents.City, 
       Parents.State + '       ' AS State, Parents.Zip,
CASE 
   WHEN len(ltrim(rtrim(Parents.PrimaryPhone))) = '10' THEN 
      '(' + SUBSTRING(Parents.PrimaryPhone, 1, 3) + ')' + ' ' + 
            SUBSTRING(Parents.PrimaryPhone, 4, 3) + '-' + 
            SUBSTRING(Parents.PrimaryPhone, 7, 4)
   WHEN len(ltrim(rtrim(Parents.PrimaryPhone))) = '7' THEN 
      SUBSTRING(Parents.PrimaryPhone, 1, 3) + '-' + 
      SUBSTRING(Parents.PrimaryPhone, 4, 4)
   WHEN len(ltrim(rtrim(Parents.PrimaryPhone))) = '' THEN 
      ' ' 
END AS PrimaryPhone,
CASE 
   WHEN len(ltrim(rtrim(Parents.SecondaryPhone))) = '10' THEN 
      '(' + SUBSTRING(Parents.SecondaryPhone, 1, 3) + ')' + ' ' + 
            SUBSTRING(Parents.SecondaryPhone, 4, 3) + '-' + 
            SUBSTRING(Parents.SecondaryPhone, 7, 4)
   WHEN len(ltrim(rtrim(Parents.SecondaryPhone))) = '7' THEN 
      SUBSTRING(Parents.SecondaryPhone, 1, 3) + '-' + 
      SUBSTRING(Parents.SecondaryPhone, 4, 4)
   WHEN len(ltrim(rtrim(Parents.SecondaryPhone))) = '' THEN 
      ' ' 
END AS SecondaryPhone,
       Parents.HomeEmail
  FROM Parents INNER JOIN
       Students ON Parents.ID = Students.ParentID
 WHERE (Students.Forename LIKE '%' + @SearchValue + '%') OR
       (Students.Surname LIKE '%' + @SearchValue + '%') OR
       (@SearchValue = 'ALL')
ORDER BY @OrderByColumn

显示此错误:

Variables are only allowed when ordering by an expression referencing 
a column name.

我们需要帮助来完成消息所描述的内容吗?

4

1 回答 1

4

试试下面的查询.....

订购方式

案例@OrderByColumn='Students.ID' THEN Students.ID END,

CASE WHEN @OrderByColumn='Students.Surname' THEN Students.Surname END

于 2013-04-19T19:10:43.863 回答