Need help on a stored procedure in SQL Server 2008
I have 4 tables:
Table1
ContentID(pk)
ContentGUID
Description
SiteID
CategoryID
StartDate
DisplayName
ParentId
AssignedAuthors
AssignedEditors
CreatedBy
U.UserName
Created
ModifiedBy
UserName
Modified
Priority
Table2
ContentID(fk)
versionid(pk)
page
Table3
apprhistroyid(pk)
verstionid(fk)
approvalstatusid(fk)
Table4
approvalstatusid(pk)
statusname
I want the total details of table1 and table4. I have tried the following procedure:
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.fnGetUser开发者_如何转开发NamesFromUserIds([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'
Where I am getting n no.of rowa that is it returns per 1 contentid 1 row instead I want all return values as one table.
I think your whole query can be boiled down to:
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
But I have to admit, I'm not sure what the DISTINCT was meant to be doing in there.
精彩评论