ALTER PROCEDURE dbo.GetProductsPagedAndSorted(         @sortExpression nvarchar(100),         @startRowIndex int,         @maximumRows int)AS -- 确保指定了 @sortExpressionIF LEN(@sortExpression) = 0         SET @sortExpression = 'ProductID' -- 组合查询DECLARE @sql nvarchar(4000)SET @sql = 'SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued, CategoryName, SupplierName                          FROM (SELECT ProductID, ProductName, p.SupplierID, p.CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued,                  c.CategoryName, s.CompanyName AS SupplierName,                    ROW_NUMBER() OVER (ORDER BY ' + @sortExpression + ') AS RowRank                          FROM Products AS p                                            INNER JOIN Categories AS c ON                                                    c.CategoryID = p.CategoryID                                            INNER JOIN Suppliers AS s ON                                                    s.SupplierID = p.SupplierID) AS ProductsWithRowNumbers                          WHERE     RowRank > ' + CONVERT(nvarchar(10), @startRowIndex) + '                                   AND RowRank <= (' + CONVERT(nvarchar(10), @startRowIndex) + ' + '                                    + CONVERT(nvarchar(10), @maximumRows) + ')'
-- 执行SQL查询EXEC sp_executesql @sql为什么最后需要把@startRowIndex @startRowIndex 转换成NVARCHAR?RowRank应该是int型的吧,转换后如何比较???

解决方案 »

  1.   

    为什么最后需要把@startRowIndex @startRowIndex 转换成NVARCHAR?RowRank应该是int型的吧,转换后如何比较???当只使用字符数据和代码页时,在一个数据库内很难以多种语言存储数据。也很难为数据库找到一种能存储所有需要的语言特定字符的代码页。此外,当运行不同代码页的不同客户端读取和更新特殊字符时,很难保证正确转换这些字符。支持国际化客户端的数据库应始终使用 Unicode 数据,而不应使用非 Unicode 数据类型
    SQL Server 将所有文字系统目录数据存储在包含 Unicode 数据类型的列中。数据库对象(如表、视图和存储过程)的名称存储在 Unicode 列中。这样就可以只使用 Unicode 来开发应用程序,从而有利于避免所有代码页转换问题。
    nvarchar就是专门用来存储unicode字符的。 如果不是nvarchar型的可以转换一下 
      

  2.   

    参考微软官方文档
    http://technet.microsoft.com/zh-cn/library/ms187828(SQL.100).aspx