自己写的临时表应用
#region Query
/// <summary>
/// 获取记录
/// </summary>
public static List<PermissionLogView> GetList(int managerId)
{
string sql = @"DECLARE @temp table
(
id int identity(1,1),
PermissionID nvarchar(200)
)
declare @pid nvarchar(200)
declare @n int
declare @rows int
select @n=1
insert @temp(PermissionID) select DISTINCT PermissionID from [PermissionLog] WHERE PermissionType=0 AND ManagerID={0}
select @rows = @@rowcount
while @n <= @rows
begin
select @pid = PermissionID
from PermissionLog
where PermissionID=(select PermissionID from @temp where id = @n)
DECLARE @tempTable TABLE
(
[ID] INT,
[FullName] varchar(100)
)
DECLARE @ID INT
DECLARE @OldID INT
DECLARE @FullName NVARCHAR(50)
DECLARE @OldFullName NVARCHAR(50)
DECLARE @i INT
SET @ID=@pid
SET @OldID=@ID
SET @OldFullName=''
SET @i=0
WHILE(@ID>0)
BEGIN
if(@ID>0)
begin
select @FullName=Name from channel where ID=@ID;
IF(@i=0)
BEGIN
SET @OldFullName=(@FullName);
END
ELSE
BEGIN
SET @OldFullName=(@FullName+'->'+@OldFullName);
END
SET @i=@i+1;
SELECT @ID=ParentId from Channel where ID=@ID;
END
END
INSERT into @tempTable VALUES(@OldID,@OldFullName);
select @n = @n + 1
END ";
sql += @"SELECT um.ManagerName,mc.FullName,plog.* FROM [PermissionLog] plog WITH(NOLOCK)
JOIN UnionManager um WITH(NOLOCK) ON plog.ManagerID = um.ID
JOIN MediumCategory mc WITH(NOLOCK) ON mc.ID=plog.PermissionID
WHERE ManagerId={0}
UNION
SELECT um.ManagerName ,mc.FullName+'->'+m.Name AS FullName,plog.* FROM [PermissionLog] plog WITH(NOLOCK)
JOIN UnionManager um WITH(NOLOCK) ON plog.ManagerID = um.ID
JOIN Medium m WITH(NOLOCK) ON m.ID=plog.PermissionID
JOIN dbo.MediumCategory mc ON mc.ID=m.CategoryID
WHERE ManagerId={0}";
sql +=
@" UNION
SELECT um.ManagerName,FullName,plog.* FROM [PermissionLog] plog
JOIN UnionManager um WITH(NOLOCK) ON plog.ManagerID = um.ID
JOIN @tempTable temp ON plog.PermissionID=temp.ID";
sql += " WHERE ManagerId={0} ORDER BY plog.OperateTime desc ";
var dbHelper = new DbHelperSQL(ConnectionString.Vancl_Union);
return DataTableToList(dbHelper.GetDataTable(string.Format(sql, managerId)));
}