我有一个存储过程,它返回具有完全相同结构的多个结果集(根据通过While循环构建的条件选择相同的数据)。我需要将所有这些结果集作为一个表加载到单个数据集中。最好的方法是什么?我正在使用SQL Server 2012
Alter PROCEDURE proc_GetAllComUserRights
AS
DECLARE @User as nvarchar(50)
DECLARE @RowCount as int
select @Rowcount=count(*) from Com_Users;
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
While (@Rowcount > 0)
BEGIN
Select @Rowcount = @Rowcount - 1;
Select @User = UserID FROM Com_Users order by UserID desc OFFSET @Rowcount ROWS FETCH NEXT 1 ROWS ONLY;
Select "UserID" = @User,
Com_UserAllRights.UserRightGUID,
Com_UserAllRights.KeyName,
Com_UserAllRights.SubKey,
Com_UserAllRights.RightName,
Com_UserAllRights.[Description],
"Approved" = (Select Count(*)
FROM Com_UserApprovedRights
WHERE Com_UserApprovedRights.UserGUID = com_User.UserGUID
AND Com_UserApprovedRights.UserRightGUID = Com_UserAllRights.UserRightGUID),
"GroupApproved" = (Select Count(*)
FROM Com_UserGroupApprovedRights
WHERE Com_UserGroupApprovedRights.GroupGUID = com_user.GroupGUID
AND Com_UserGroupApprovedRights.UserRightGUID = Com_UserAllRights.UserRightGUID )
From Com_UserAllRights LEFT OUTER JOIN
Com_UserApprovedRights as ApprovedRights on Com_UserAllRights.UserRightGUID = ApprovedRights.UserRightGUID Left Outer Join
(SELECT * FROM Com_users WHERE UserID = @User) Com_User On approvedrights.UserGUID = com_User.UserGUID
ORDER BY UserID, KeyName, SubKey
END
END
GO
答案 0 :(得分:1)
以下是表变量的解决方案。
Alter PROCEDURE proc_GetAllComUserRights
AS
DECLARE @Result AS TABLE( UserID NVARCHAR(50),
UserRightGUID UNIQUEIdentifier,
KeyName NVARCHAR(50),
SubKey NVARCHAR(50),
RightName NVARCHAR(50),
Approved INT,
GroupApproved INT)
DECLARE @User as nvarchar(50)
DECLARE @RowCount as int
select @Rowcount=count(*) from Com_Users;
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
While (@Rowcount > 0)
BEGIN
Select @Rowcount = @Rowcount - 1;
Select @User = UserID FROM Com_Users order by UserID desc OFFSET @Rowcount ROWS FETCH NEXT 1 ROWS ONLY;
INSERT INTO @Result
Select "UserID" = @User,
Com_UserAllRights.UserRightGUID,
Com_UserAllRights.KeyName,
Com_UserAllRights.SubKey,
Com_UserAllRights.RightName,
Com_UserAllRights.[Description],
"Approved" = (Select Count(*)
FROM Com_UserApprovedRights
WHERE Com_UserApprovedRights.UserGUID = com_User.UserGUID
AND Com_UserApprovedRights.UserRightGUID = Com_UserAllRights.UserRightGUID),
"GroupApproved" = (Select Count(*)
FROM Com_UserGroupApprovedRights
WHERE Com_UserGroupApprovedRights.GroupGUID = com_user.GroupGUID
AND Com_UserGroupApprovedRights.UserRightGUID = Com_UserAllRights.UserRightGUID )
From Com_UserAllRights LEFT OUTER JOIN
Com_UserApprovedRights as ApprovedRights on Com_UserAllRights.UserRightGUID = ApprovedRights.UserRightGUID Left Outer Join
(SELECT * FROM Com_users WHERE UserID = @User) Com_User On approvedrights.UserGUID = com_User.UserGUID
ORDER BY UserID, KeyName, SubKey
END
SELECT * FROM @Result
END
答案 1 :(得分:0)
使用DataSet.Merge
。它将指定的DataTable及其架构合并到当前的DataSet中。