2011-07-18 97 views
-3

我有4個表:需要幫助的SQL Server中的存儲過程,2008年

表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 [email protected]) 
set @ContentID=(select min(ContentID) from dev_content where [email protected]) 
SET @[email protected] 
IF(@Rowcount>=1) 

    BEGIN 
     WHILE @Rowcount>1 
     BEGIN 
      SET @[email protected] 
      set @ApprovalStatusID= 
CASE 
WHEN exists (SELECT VersionID from DEV_ContentVersion WHERE [email protected]) 
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 [email protected] and [email protected])))) else 5 end 
set @StatusName= 
case 
when (@ApprovalStatusID != 5) 
then 
(SELECT StatusName FROM DEV_ApprovalStatus WHERE [email protected]) 
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 [email protected] AND [ContentID] = @TEMP 
       SET @TEMP= @TEMP+1 
     END 
    END 
    else 
    print 'error' 

當我得到ň節數ROWA就是它每1個內容識別1行,返回而不是我想所有的返回值作爲一個表。

+0

我認爲你的查詢有點壞 - 在你賦值給'@ ApprovalStatusID'的時候,你真的打算純粹基於'ContentID'搜索'DEV_ContentVersion',但是然後根據'ContentID'和' SiteID'?我注意到,同樣的陳述使用'@ ContentID' - 它只分配一次,而不是'@ TEMP',它在循環中遞增。你能否說這些是否是故意的? –

回答

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 
    [email protected] 

但我不得不承認,我不知道是什麼DISTINCT本來是在那裏做什麼。