如何在存储过程中使用pivot

时间:2016-05-24 11:35:32

标签: sql sql-server stored-procedures birt

我有一个动态的sql stmt,我试图在BIRT的DataSet中使用(对不起我的英语我是法语),问题是BIRT无法创建数据集,I没有找到这份报告的解决方案。所以我选择使用存储过程,但是当我尝试在birt上调用该过程时,我仍然没有得到任何结果。有人可以帮我用动态列获取结果对象吗?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET NOCOUNT ON 
GO
-- =============================================
-- Author:      Reda
-- Create date: 24/05/2016
-- Description: Retourne des données test
-- =============================================

CREATE PROCEDURE NomP


AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

END
GO
DECLARE @sqlCommand varchar(MAX)
DECLARE @ListeAffaires TABLE(Nom_APPEL nvarchar(MAX)) 

    INSERT INTO @ListeAffaires(Nom_APPEL)
        SELECT DISTINCT AF_NOMAPPEL FROM F_AFFAIRES  INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
        WHERE SOC_CODE = 'RUS01'

declare @Affaires NVARCHAR(MAX)

SET @Affaires = STUFF(
            (
                SELECT distinct ',ISNULL(' + QUOTENAME(AF_NOMAPPEL) + ', ''0'') ' + QUOTENAME(AF_NOMAPPEL)
               FROM F_AFFAIRES  INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
                                INNER JOIN VUE_VALORISATION_TEMPS_PASSES ON VTPS_CODE_AFFAIRE = AF_CODE_AFFAIRE
                    WHERE SOC_CODE = 'RUS01' AND   VTPS_date_FICHE >= '02/11/2015'
                FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

declare @Affairess NVARCHAR(MAX)
SET @Affairess = ''
SELECT @Affairess = @Affairess +'['+ Nom_APPEL + '],' FROM @ListeAffaires 
SET @Affairess = LEFT (@Affairess, LEN(@Affairess) - 1)


SET @sqlCommand = 'DECLARE @SYNTHESETEMPS TABLE (  VTPS_CODE_COLLAB varchar(250)
                                   , VTPS_NOM_COLLAB varchar(250)
                                   , VTPS_PRENOM_COLLAB varchar(250)
                                   , TSRV_LIBELLE varchar(250)
                                   , VTPS_COL_ALPHANUM01 varchar(10)
                                   , AF_NOMAPPEL NVARCHAR(MAX)
                                   , VTPS_DUREE float(24)
                                   )

    INSERT INTO @SYNTHESETEMPS (
                               VTPS_CODE_COLLAB
                             , VTPS_NOM_COLLAB
                             , VTPS_PRENOM_COLLAB
                             , TSRV_LIBELLE
                             , VTPS_COL_ALPHANUM01
                             , AF_NOMAPPEL
                             , VTPS_DUREE
                               )
    SELECT VTPS_CODE_COLLAB
          , VTPS_NOM_COLLAB
          , VTPS_PRENOM_COLLAB
          , TSRV_LIBELLE
          , VTPS_COL_ALPHANUM01
          , AF_NOMAPPEL
         , VTPS_DUREE
 FROM VUE_VALORISATION_TEMPS_PASSES
 INNER JOIN F_P_COLLABORATEUR ON VTPS_CODE_COLLAB = CLB_CODE 
 INNER JOIN F_AFFAIRES ON VTPS_CODE_AFFAIRE = AF_CODE_AFFAIRE 
 INNER JOIN T_COL_SERVICE ON CLB_TSRV_IDENT = TSRV_IDENT 
 INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
 WHERE SOC_CODE = ''RUS01'' AND   VTPS_date_FICHE >= ''02/11/2015''
    SELECT  VTPS_NOM_COLLAB +'' '' + VTPS_PRENOM_COLLAB AS "Remaining hours"
          , TSRV_LIBELLE AS "Service"
          , VTPS_COL_ALPHANUM01 AS "Categories"
          ,'+ @Affaires +'
 FROM @SYNTHESETEMPS 
    PIVOT (SUM(VTPS_DUREE)
     FOR 
      AF_NOMAPPEL IN ('+@Affairess+')) PVT '
EXEC(@sqlCommand)



EXEC NomP;
`

1 个答案:

答案 0 :(得分:0)

对不起,我没有注意`

CREATE PROCEDURE NomP
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @sqlCommand varchar(MAX)

DECLARE @ListeAffaires TABLE(Nom_APPEL nvarchar(MAX)) 

    INSERT INTO @ListeAffaires(Nom_APPEL)
        SELECT DISTINCT AF_NOMAPPEL FROM F_AFFAIRES  INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
        WHERE SOC_CODE = 'RUS01'

declare @Affaires NVARCHAR(MAX)

SET @Affaires = STUFF(
            (
                SELECT distinct ',ISNULL(' + QUOTENAME(AF_NOMAPPEL) + ', ''0'') ' + QUOTENAME(AF_NOMAPPEL)
               FROM F_AFFAIRES  INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
                                INNER JOIN VUE_VALORISATION_TEMPS_PASSES ON VTPS_CODE_AFFAIRE = AF_CODE_AFFAIRE
                    WHERE SOC_CODE = 'RUS01' AND   VTPS_date_FICHE >= '02/11/2015'
                FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

declare @Affairess NVARCHAR(MAX)
SET @Affairess = ''
SELECT @Affairess = @Affairess +'['+ Nom_APPEL + '],' FROM @ListeAffaires 
SET @Affairess = LEFT (@Affairess, LEN(@Affairess) - 1)


SET @sqlCommand = 'DECLARE @SYNTHESETEMPS TABLE (  VTPS_CODE_COLLAB varchar(250)
                                   , VTPS_NOM_COLLAB varchar(250)
                                   , VTPS_PRENOM_COLLAB varchar(250)
                                   , TSRV_LIBELLE varchar(250)
                                   , VTPS_COL_ALPHANUM01 varchar(10)
                                   , AF_NOMAPPEL NVARCHAR(MAX)
                                   , VTPS_DUREE float(24)
                                   )

    INSERT INTO @SYNTHESETEMPS (
                               VTPS_CODE_COLLAB
                             , VTPS_NOM_COLLAB
                             , VTPS_PRENOM_COLLAB
                             , TSRV_LIBELLE
                             , VTPS_COL_ALPHANUM01
                             , AF_NOMAPPEL
                             , VTPS_DUREE
                               )
    SELECT VTPS_CODE_COLLAB
          , VTPS_NOM_COLLAB
          , VTPS_PRENOM_COLLAB
          , TSRV_LIBELLE
          , VTPS_COL_ALPHANUM01
          , AF_NOMAPPEL
         , VTPS_DUREE
 FROM VUE_VALORISATION_TEMPS_PASSES
 INNER JOIN F_P_COLLABORATEUR ON VTPS_CODE_COLLAB = CLB_CODE 
 INNER JOIN F_AFFAIRES ON VTPS_CODE_AFFAIRE = AF_CODE_AFFAIRE 
 INNER JOIN T_COL_SERVICE ON CLB_TSRV_IDENT = TSRV_IDENT 
 INNER JOIN T_P_SOCIETE ON AF_CODE_SOCIETE = SOC_CODE
 WHERE SOC_CODE = ''RUS01'' AND   VTPS_date_FICHE >= ''02/11/2015''
    SELECT  VTPS_NOM_COLLAB +'' '' + VTPS_PRENOM_COLLAB AS "Remaining hours"
          , TSRV_LIBELLE AS "Service"
          , VTPS_COL_ALPHANUM01 AS "Categories"
          ,'+ @Affaires +'
 FROM @SYNTHESETEMPS 
    PIVOT (SUM(VTPS_DUREE)
     FOR 
      AF_NOMAPPEL IN ('+@Affairess+')) PVT '
EXEC(@sqlCommand)


END