需要有关SQL Server 2008中的存储过程的帮助

时间:2011-07-18 06:10:20

标签: sql-server-2008

我有4张桌子:

表1

ContentID(pk)
ContentGUID
Description
SiteID
CategoryID
StartDate
DisplayName
ParentId
AssignedAuthors
AssignedEditors
CreatedBy
U.UserName
Created
ModifiedBy
UserName
Modified
Priority

表2

ContentID(fk)
versionid(pk)
page

表3

apprhistroyid(pk)
verstionid(fk)
approvalstatusid(fk)

表4

approvalstatusid(pk)
statusname

我想要table1和table4的全部细节。我尝试过以下程序:

   ALTER PROCEDURE [dbo].[DEV_SiteAdmin_Menu_GetItem_ALL_TEMP] 
@SiteID Int

   AS
DECLARE @VersionID int, @ApprovalStatusID int,@StatusName nvarchar(max),@ContentID Int,@Rowcount int
DECLARE @TEMP INT

--set @ApprovalStatusID=5;
--set @StatusName ='New';
set @Rowcount=(select max(ContentID)from dev_content where SiteID=@SiteID)
set @ContentID=(select min(ContentID) from dev_content where SiteID=@SiteID)
SET @TEMP=@ContentID
IF(@Rowcount>=1) 

    BEGIN
        WHILE @Rowcount>1
        BEGIN
            SET @Rowcount=@Rowcount-1
            set @ApprovalStatusID= 
CASE 
WHEN exists (SELECT VersionID from DEV_ContentVersion WHERE ContentID=@TEMP)
THEN (SELECT ApprovalStatusID FROM DEV_ApprovalStatus WHERE ApprovalStatusID=(SELECT ApprovalStatusID FROM DEV_ApprovalHistory WHERE VersionID=(SELECT VersionID from DEV_ContentVersion WHERE ContentID=(select ContentID from dev_content where ContentID=@ContentID and SiteID=@SiteID)))) else 5   end
set @StatusName=
case
when (@ApprovalStatusID != 5)
then 
(SELECT StatusName FROM DEV_ApprovalStatus WHERE ApprovalStatusID=@ApprovalStatusID) 
else
'New'
end
        SELECT DISTINCT [ContentID]
                  ,[ContentGUID]
                  ,[Description]
                  ,[SiteID]
                  ,[CategoryID]
                  ,[StartDate]
                  ,[DisplayName]
                  ,[ParentId]
                  ,[AssignedAuthors]
                  ,[AssignedEditors]
                  ,[CreatedBy]
                  ,U.UserName 'CreatedUser'
                  ,dbo.fnGetUserNamesFromUserIds([AssignedAuthors],',') 'Authors'
                  ,dbo.fnGetUserNamesFromUserIds([AssignedEditors],',') 'Editors'
                  ,[Created]
                  ,[ModifiedBy]
                  ,U1.UserName 'ModifiedUser'
                  ,[Modified]
                  ,[Priority]
                  ,@ApprovalStatusID [ApprovalStatusID]
                  ,@StatusName [StatusName]
              FROM [DEV_Content] C
              INNER JOIN dbo.aspnet_Users U ON U.UserId=C.CreatedBy
              INNER JOIN dbo.aspnet_Users U1 ON U1.UserId=C.ModifiedBy
              WHERE C.SiteID=@SiteID AND [ContentID] = @TEMP
              SET @TEMP= @TEMP+1
         END
     END
     else
     print 'error'

我得到的是rowa的n号,它返回每1个有争议的1行,而我希望所有返回值都作为一个表。

1 个答案:

答案 0 :(得分:0)

我认为您的整个查询可归结为:

SELECT 
    [ContentID]
    ,[ContentGUID]
    ,[Description]
    ,[SiteID]
    ,[CategoryID]
    ,[StartDate]
    ,[DisplayName]
    ,[ParentId]
    ,[AssignedAuthors]
    ,[AssignedEditors]
    ,[CreatedBy]
    ,U.UserName as CreatedUser
    ,dbo.fnGetUserNamesFromUserIds([AssignedAuthors],',') as Authors
    ,dbo.fnGetUserNamesFromUserIds([AssignedEditors],',') as Editors
    ,[Created]
    ,[ModifiedBy]
    ,U1.UserName as ModifiedUser
    ,[Modified]
    ,[Priority]
    ,COALESCE(ah.ApprovalStatusID,5) as ApprovalStatusID
    ,COALESCE(ast.StatusName,'New' as StatusName
FROM
    [DEV_Content] C
        INNER JOIN
    dbo.aspnet_Users U
        ON
            U.UserId=C.CreatedBy
        INNER JOIN
    dbo.aspnet_Users U1
        ON
            U1.UserId=C.ModifiedBy
        left join
    DEV_ContentVersion cv
        inner join
    DEV_ApprovalHistory ah
        on
            cv.VersionID = ah.VersionID
        inner join
    DEV_ApprovalStatus ast
        on
            ah.ApprovalStatusID = ast.ApprovalStatusID
        on
            C.ContentID = cv.ContentID

WHERE
    C.SiteID=@SiteID

但我不得不承认,我不确定DISTINCT在那里的意图是什么。