在SQL

2014-03-26 53 views
0
Create Proc [dbo].[SprptAssessmentCompartion_Subject] 
      @intQstnMasterID int, 
      @intAssessmentID int, 
      @intQstnMasterID1 int, 
      @intAssessmentID1 int, 
      @intUserID int 

As 

Begin 

select DISTINCT vchSubject,intUserID,SUM(FrstId) as 'FrstId' ,SUM(ScndId) as 'ScndId' from 
( SELECT 
    intSubjectID, 
    intUserID, 
    ISNULL([@intAssessmentID],0) as 'FrstId', 
    ISNULL([@intAssessmentID1],0) as 'ScndId', 
    FinalsCORE 

FROM 
(
    select DISTINCT intSubjectID,intUserID,intAssignmnetID,SUM(KidTtl) as 'KidScore',COUNT(intSubjectID) AS 'FinalsCORE' ,(SUM(KidTtl) /COUNT(intSubjectID)*100) as 'Ct' from 
    (
     select Answetble.vchAssignmentName ,intAssignmnetID,intUserID,intSlNo,isnull(QuiestionPaper.vchTopic,'-') as 'VchTopic',isnull(Answetble.fltMark,0) as 'KidTtl' ,QuiestionPaper.intSubjectID from QuiestionMapping 
     inner join QuiestionPaper on QuiestionPaper.intQstnID=QuiestionMapping.intQstnID 
     Left outer join 
     (
      select distinct intQstnID,intAssessmentID,intAssignmnetID,intUserID,intQstnMasterID,fltMark,fltTotalMark,intAssignedByUserID,vchAssignmentName from 
      (
      SELECT * from ManageAssessment 
      inner join Assignment ON Assignment.intAssignmnetID=ManageAssessment.intAssessmentID) as B 
      INNER JOIN UserMapping ON UserMapping.intPTUserID= intAssignedByUserID 
      where [email protected] and [email protected] AND [email protected] 
      OR([email protected] and [email protected] AND [email protected]) 
     ) 
     as Answetble on Answetble.intQstnID=QuiestionPaper.intQstnID 
     where [email protected] OR [email protected] 
    )as A 
    GROUP BY intAssignmnetID,intSubjectID,intUserID 


) 
AS t 
PIVOT 
(
    Max(Ct) 
    FOR intAssignmnetID IN([@intAssessmentID] ,[@intAssessmentID1]) 
) AS p 
)as B 

inner join subject on Subject.intSubjectID=b.intSubjectID 

group by subject.vchSubject,B.intUserID 
End 

這裏@intAssessmentID在存儲過程中傳遞的列值的一列,當我使用這個存儲過程中我會得到錯誤在SQL

消息8114,級別16,狀態1,過程SprptAssessmentCompartion_Subject,第47行
將數據類型nvarchar轉換爲int時出錯。
Msg 473,Level 16,State 1,Procedure SprptAssessmentCompartion_Subject,Line 47
錯誤值「@intAssessmentID」在PIVOT運算符中提供。

任何人都可以幫我解決這個錯誤嗎?

回答

2

這樣你不能指定PIVOT的列,你必須使用動態SQL。

Create Proc [dbo].[SprptAssessmentCompartion_Subject] 
      @intQstnMasterID int, 
      @intAssessmentID int, 
      @intQstnMasterID1 int, 
      @intAssessmentID1 int, 
      @intUserID int 

As 

Begin 

declare @sql as nvarchar(max) = N'select DISTINCT vchSubject,intUserID,SUM(FrstId) as ''FrstId'' ,SUM(ScndId) as ''ScndId'' from 
( SELECT 
    intSubjectID, 
    intUserID, 
    ISNULL([' + CONVERT(NVARCHAR(20), @intAssessmentID) + N'],0) as ''FrstId'', 
    ISNULL([' + CONVERT(NVARCHAR(20), @intAssessmentID1) + N'],0) as ''ScndId'', 
    FinalsCORE 

FROM 
(
    select DISTINCT intSubjectID,intUserID,intAssignmnetID,SUM(KidTtl) as ''KidScore'',COUNT(intSubjectID) AS ''FinalsCORE'' ,(SUM(KidTtl) /COUNT(intSubjectID)*100) as ''Ct'' from 
    (
     select Answetble.vchAssignmentName ,intAssignmnetID,intUserID,intSlNo,isnull(QuiestionPaper.vchTopic,''-'') as ''VchTopic'',isnull(Answetble.fltMark,0) as ''KidTtl'' ,QuiestionPaper.intSubjectID from QuiestionMapping 
     inner join QuiestionPaper on QuiestionPaper.intQstnID=QuiestionMapping.intQstnID 
     Left outer join 
     (
      select distinct intQstnID,intAssessmentID,intAssignmnetID,intUserID,intQstnMasterID,fltMark,fltTotalMark,intAssignedByUserID,vchAssignmentName from 
      (
      SELECT * from ManageAssessment 
      inner join Assignment ON Assignment.intAssignmnetID=ManageAssessment.intAssessmentID) as B 
      INNER JOIN UserMapping ON UserMapping.intPTUserID= intAssignedByUserID 
      where [email protected] and [email protected]ntAssessmentID AND [email protected] 
      OR([email protected] and [email protected] AND [email protected]) 
     ) 
     as Answetble on Answetble.intQstnID=QuiestionPaper.intQstnID 
     where [email protected] OR [email protected] 
    )as A 
    GROUP BY intAssignmnetID,intSubjectID,intUserID 


) 
AS t 
PIVOT 
(
    Max(Ct) 
    FOR intAssignmnetID IN([' + CONVERT(NVARCHAR(20), @intAssessmentID) + N'] ,[' + CONVERT(NVARCHAR(20), @intAssessmentID1) + N']) 
) AS p 
)as B 

inner join subject on Subject.intSubjectID=b.intSubjectID 

group by subject.vchSubject,B.intUserID' 

exec sp_executesql @sql 

End