工作中一些常用到的代码记录下来,方便自己查找也方便其他需要人士参考。
废话不多说,这是一个向上向下排序的功能,首先使用存储过程 整好 如下:
SQL:
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[sp_BannerOrder]
-- Add the parameters for the stored procedure here
(
@tablename nvarchar(50), --表名
@colname nvarchar(50), --排序字段
@keyid nvarchar(50), --表主键字段
@keyidvalue int, --表主键字段值1
@order nvarchar(20), -- 列表默认的排序方式,asc或desc
@orderDirection nvarchar(20), --排序方向,up或down
@where nvarchar(2000) --查询条件
)
AS
BEGIN
declare @ordertmp1 int; --临时排序值id1
declare @ordertmp2 int; --临时排序值id2
declare @tmpkeyidvaule nvarchar(50);
declare @sql nvarchar(2000);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @ParmDefinition2 nvarchar(500);
if @order='asc'
begin
SET @sql = N'SELECT @ordertmp1OUT='[email protected]+' from '[email protected]+' where '[email protected]+'='+cast(@keyidvalue as nvarchar(50));
SET @ParmDefinition = N'@ordertmp1OUT nvarchar(20) OUTPUT';
EXECUTE sp_executesql @sql, @ParmDefinition, @[email protected] OUTPUT;
if @orderDirection='up'
begin
SET @sql = N'SELECT top 1 @ordertmp2OUT='[email protected]+',@tmpkeyidvauleOUT='[email protected]+' from '[email protected]+' where '[email protected]+'<'+cast(@ordertmp1 as nvarchar(50))+' and '[email protected]+' order by '[email protected]+' desc';
end
else
begin
SET @sql = N'SELECT top 1 @ordertmp2OUT='[email protected]+',@tmpkeyidvauleOUT='[email protected]+' from '[email protected]+' where '[email protected]+'>'+cast(@ordertmp1 as nvarchar(50))+' and '[email protected]+' order by '[email protected]+' asc';
end
SET @ParmDefinition = N'@ordertmp2OUT nvarchar(20) OUTPUT, @tmpkeyidvauleOUT nvarchar(20) OUTPUT';
EXECUTE sp_executesql @sql, @ParmDefinition, @[email protected] OUTPUT, @[email protected] OUTPUT;
end
else
begin
SET @sql = N'SELECT @ordertmp1OUT='[email protected]+' from '[email protected]+' where '[email protected]+'='+cast(@keyidvalue as nvarchar(50));
SET @ParmDefinition = N'@ordertmp1OUT nvarchar(20) OUTPUT';
EXECUTE sp_executesql @sql, @ParmDefinition, @[email protected] OUTPUT;
if @orderDirection='up'
begin
SET @sql = N'SELECT top 1 @ordertmp2OUT='[email protected]+',@tmpkeyidvauleOUT='[email protected]+' from '[email protected]+' where '[email protected]+'>'+cast(@ordertmp1 as nvarchar(50))+' and '[email protected]+' order by '[email protected]+' asc';
新闻热点
疑难解答