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型的吧,转换后如何比较???
-- 执行SQL查询EXEC sp_executesql @sql为什么最后需要把@startRowIndex @startRowIndex 转换成NVARCHAR?RowRank应该是int型的吧,转换后如何比较???
SQL Server 将所有文字系统目录数据存储在包含 Unicode 数据类型的列中。数据库对象(如表、视图和存储过程)的名称存储在 Unicode 列中。这样就可以只使用 Unicode 来开发应用程序,从而有利于避免所有代码页转换问题。
nvarchar就是专门用来存储unicode字符的。 如果不是nvarchar型的可以转换一下
http://technet.microsoft.com/zh-cn/library/ms187828(SQL.100).aspx