cs_Link_UpdateSortOrder
ALTER procedure [dbo].cs_Link_UpdateSortOrder
(
@LinkID int,
@SettingsID int,
@MoveUp bit
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
/**//*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
BEGIN
set nocount on
/**//*当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。
当 SET NOCOUNT 为 OFF 时,返回计数。即使当 SET NOCOUNT 为 ON 时,也更新
@@ROWCOUNT 函数。*/
DECLARE @currentSortValue int --当前类型的值
DECLARE @replaceSortValue int --替换类型的值
DECLARE @replaceLinkID int --替换连接ID
DECLARE @LinkCategoryID int --连接种类ID
-- Get the current sort order
--获取当前排序类型
SELECT @currentSortValue = SortOrder, @LinkCategoryID = LinkCategoryID FROM cs_Links WHERE LinkID = @LinkID and SettingsID = @SettingsID
-- Move the item up or down?
---
IF (@MoveUp = 1)
BEGIN
SELECT @replaceSortValue = coalesce(f.SortOrder, -1), @replaceLinkID = coalesce(f.LinkID, -1)
FROM cs_Links f
inner join (
select top 1 *
from cs_Links
WHERE LinkCategoryID = @LinkCategoryID and SortOrder < @currentSortValue and SettingsID = @SettingsID order by SortOrder DESC
) as pf on
pf.LinkID = f.LinkID and f.SettingsID = @SettingsID
/**//*先查询替换种类的值和替换连接的ID,如果都存在就更新*/
if( @replaceSortValue != -1 And @replaceSortValue is Not Null )
begin
UPDATE cs_Links SET SortOrder = @currentSortValue WHERE LinkID = @replaceLinkID and SettingsID = @SettingsID
UPDATE cs_Links SET SortOrder = @replaceSortValue WHERE LinkID = @LinkID and SettingsID = @SettingsID
END
END
ELSE
BEGIN
SELECT @replaceSortValue = coalesce(f.SortOrder, -1), @replaceLinkID = coalesce(f.LinkID, -1)
FROM cs_Links f
inner join (
select top 1 *
FROM cs_Links
WHERE LinkCategoryID = @LinkCategoryID and SortOrder > @currentSortValue and SettingsID = @SettingsID order by SortOrder ASC
) as pf on
pf.LinkID = f.LinkID and f.SettingsID = @SettingsID
if( @replaceSortValue != -1 And @replaceSortValue is Not Null )
begin
UPDATE cs_Links SET SortOrder = @currentSortValue WHERE LinkID = @replaceLinkID and SettingsID = @SettingsID
UPDATE cs_Links SET SortOrder = @replaceSortValue WHERE LinkID = @LinkID and SettingsID = @SettingsID
end
END
/**//*COALESCE 返回其参数中第一个非空表达式*/
END
ALTER procedure [dbo].cs_Link_UpdateSortOrder
(
@LinkID int,
@SettingsID int,
@MoveUp bit
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
/**//*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
BEGIN
set nocount on
/**//*当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。
当 SET NOCOUNT 为 OFF 时,返回计数。即使当 SET NOCOUNT 为 ON 时,也更新
@@ROWCOUNT 函数。*/
DECLARE @currentSortValue int --当前类型的值
DECLARE @replaceSortValue int --替换类型的值
DECLARE @replaceLinkID int --替换连接ID
DECLARE @LinkCategoryID int --连接种类ID
-- Get the current sort order
--获取当前排序类型
SELECT @currentSortValue = SortOrder, @LinkCategoryID = LinkCategoryID FROM cs_Links WHERE LinkID = @LinkID and SettingsID = @SettingsID
-- Move the item up or down?
---
IF (@MoveUp = 1)
BEGIN
SELECT @replaceSortValue = coalesce(f.SortOrder, -1), @replaceLinkID = coalesce(f.LinkID, -1)
FROM cs_Links f
inner join (
select top 1 *
from cs_Links
WHERE LinkCategoryID = @LinkCategoryID and SortOrder < @currentSortValue and SettingsID = @SettingsID order by SortOrder DESC
) as pf on
pf.LinkID = f.LinkID and f.SettingsID = @SettingsID
/**//*先查询替换种类的值和替换连接的ID,如果都存在就更新*/
if( @replaceSortValue != -1 And @replaceSortValue is Not Null )
begin
UPDATE cs_Links SET SortOrder = @currentSortValue WHERE LinkID = @replaceLinkID and SettingsID = @SettingsID
UPDATE cs_Links SET SortOrder = @replaceSortValue WHERE LinkID = @LinkID and SettingsID = @SettingsID
END
END
ELSE
BEGIN
SELECT @replaceSortValue = coalesce(f.SortOrder, -1), @replaceLinkID = coalesce(f.LinkID, -1)
FROM cs_Links f
inner join (
select top 1 *
FROM cs_Links
WHERE LinkCategoryID = @LinkCategoryID and SortOrder > @currentSortValue and SettingsID = @SettingsID order by SortOrder ASC
) as pf on
pf.LinkID = f.LinkID and f.SettingsID = @SettingsID
if( @replaceSortValue != -1 And @replaceSortValue is Not Null )
begin
UPDATE cs_Links SET SortOrder = @currentSortValue WHERE LinkID = @replaceLinkID and SettingsID = @SettingsID
UPDATE cs_Links SET SortOrder = @replaceSortValue WHERE LinkID = @LinkID and SettingsID = @SettingsID
end
END
/**//*COALESCE 返回其参数中第一个非空表达式*/
END
cs_Link_CreateUpdateDelete
ALTER PROCEDURE dbo.cs_Link_CreateUpdateDelete
@DeleteLink bit=0,
@LinkCategoryID int=0,
@Title nvarchar(100)='',
@Url nvarchar(255)='',
@Rel nvarchar(100)='',
@Description nvarchar(2000)='',
@IsEnabled bit=1,
@SortOrder int=0,
@SettingsID int,
@LinkID int=0 out
AS
SET Transaction Isolation Level Read UNCOMMITTED
/**//*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
-- Are we deleting?
-- 我们是否删除
if @DeleteLink = 1 -- 如果删除
begin
DELETE FROM cs_Links WHERE LinkID = @LinkID
end
/**//*删除*/
-- Are we updating?
-- 我们是否更新
if @LinkID > 0 --如果大于0,则表示存在
begin
UPDATE cs_Links SET
Title = @Title,
Url = @Url,
Rel = @Rel,
[Description] = @Description,
IsEnabled = @IsEnabled,
SortOrder = @SortOrder
WHERE LinkID = @LinkID and LinkCategoryID = @LinkCategoryID and SettingsID = @SettingsID
end
/**//*更新*/
else
begin
If( @SortOrder = 0 ) -- 如果排序类型为0
Begin
Select @SortOrder = max(SortOrder) + 1 from cs_Links where LinkCategoryID = @LinkCategoryID and SettingsID = @SettingsID
if(@SortOrder is null)
Select @SortOrder = 0
End
/**//*插入数据*/
INSERT INTO cs_Links (LinkCategoryID, Title, Url, Rel, [Description], IsEnabled, SortOrder, SettingsID)
VALUES (@LinkCategoryID, @Title, @Url, @Rel, @Description, @IsEnabled, @SortOrder, @SettingsID)
set @LinkID = @@IDENTITY
end
ALTER PROCEDURE dbo.cs_Link_CreateUpdateDelete
@DeleteLink bit=0,
@LinkCategoryID int=0,
@Title nvarchar(100)='',
@Url nvarchar(255)='',
@Rel nvarchar(100)='',
@Description nvarchar(2000)='',
@IsEnabled bit=1,
@SortOrder int=0,
@SettingsID int,
@LinkID int=0 out
AS
SET Transaction Isolation Level Read UNCOMMITTED
/**//*设置事务隔离级别,Read UNCOMMITTED 执行脏读或 0 级隔离锁定,这表示不发出共享锁,
也不接受排它锁。当设置该选项时,可以对数据执行未提交读或脏读;在事务结束前可以更
改数据内的数值,行也可以出现在数据集中或从数据集消失。该选项的作用与在事务内所有
语句中的所有表上设置 NOLOCK 相同。这是四个隔离级别中限制最小的级别。*/
-- Are we deleting?
-- 我们是否删除
if @DeleteLink = 1 -- 如果删除
begin
DELETE FROM cs_Links WHERE LinkID = @LinkID
end
/**//*删除*/
-- Are we updating?
-- 我们是否更新
if @LinkID > 0 --如果大于0,则表示存在
begin
UPDATE cs_Links SET
Title = @Title,
Url = @Url,
Rel = @Rel,
[Description] = @Description,
IsEnabled = @IsEnabled,
SortOrder = @SortOrder
WHERE LinkID = @LinkID and LinkCategoryID = @LinkCategoryID and SettingsID = @SettingsID
end
/**//*更新*/
else
begin
If( @SortOrder = 0 ) -- 如果排序类型为0
Begin
Select @SortOrder = max(SortOrder) + 1 from cs_Links where LinkCategoryID = @LinkCategoryID and SettingsID = @SettingsID
if(@SortOrder is null)
Select @SortOrder = 0
End
/**//*插入数据*/
INSERT INTO cs_Links (LinkCategoryID, Title, Url, Rel, [Description], IsEnabled, SortOrder, SettingsID)
VALUES (@LinkCategoryID, @Title, @Url, @Rel, @Description, @IsEnabled, @SortOrder, @SettingsID)
set @LinkID = @@IDENTITY
end