将存储过程从SQL Server导入Power BI:将数据类型nvarchar转换为bigint时出错

时间:2019-04-25 15:22:56

标签: sql sql-server stored-procedures parameters powerbi

我正在尝试从SQL Server引入存储过程以在Power BI中用于仪表板和报告。我收到以下错误消息:

详细信息:“ Microsoft SQL:将数据类型nvarchar转换为bigint时出错。”

我们基本上在Jaspersoft中使用存储过程存储所有报告,没有问题。

这里可能是什么问题?


    DECLARE @temp1 VARCHAR(max)
    DECLARE @res NVARCHAR(max) 
    DECLARE @sub NVARCHAR(max) 
    DECLARE @fields NVARCHAR(max) 

    SET @temp1= '$P!{ProjectID}' 

    EXEC stored_procedure 
      @ListOfProjects = @temp1, 
      @wbs1 = 1, 
      @wbs2 = 1, 
      @groupCode1 =1, 
      @groupCode2 =1, 
      @groupCode3 =1, 
      @groupCode4 =1, 
      @groupCode5 =1, 
      @groupCode6 =1, 
      @groupCode7 =1, 
      @groupCode8 =1, 
      @groupCode9 =1, 
      @ResultSqlQuery = @res output 
    --EXECUTE sp_executesql @res 



编辑:这是stored_procedure的代码

USE [Database]
GO
/****** Object:  StoredProcedure [stored_procedure]    Script Date: 4/25/2019 10:45:11 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[stored_procedure] 
@ListOfProjects nvarchar(max),
@wbs1 bit = 0,
@wbs2 bit =0 ,
@groupCode1 bit =0 ,
@groupCode2 bit =0 ,
@groupCode3 bit =0,
@groupCode4 bit =0 ,
@groupCode5 bit =0,
@groupCode6 bit =0 ,
@groupCode7 bit =0,
@groupCode8 bit =0,
@groupCode9 bit =0,
@ResultSqlQuery nvarchar(max) ='' OUTPUT 



    AS
    SET NOCOUNT ON;
    DECLARE @CurrentProjectId nvarchar(max)
    DECLARE @TotalCount INT;
    DECLARE @Counter INT;
    DECLARE @ResultString nvarchar(max);
    DECLARE @Temp nvarchar(max);


    SET @Counter = 0;
    SET @ResultString = '';
    SET @TotalCount = 0;
    SET @TotalCount = ( SELECT count(*) FROM dbo.ufn_split_string(@ListOfProjects) ) ;
        WHILE @Counter < @TotalCount
            BEGIN
                SET @CurrentProjectId =  ( SELECT CAST(ColumnData AS BigInt)FROM dbo.ufn_split_string(@ListOfProjects) where row = @Counter )

                --PRINT @CurrentProjectName

                IF @Counter <> 0
                BEGIN
                SET @ResultString = @ResultString + +' UNION ALL ' 
                END


                EXECUTE  stored_procedure2 @PrjId = @CurrentProjectId ,
                @wbs1 = @wbs1,
                @wbs2 = @wbs2 ,
                @groupCode1 =@groupCode1 ,
                @groupCode2 =@groupCode2 ,
                @groupCode3 =@groupCode3 ,
                @groupCode4 =@groupCode4,
                @groupCode5 =@groupCode5 ,
                @groupCode6 =@groupCode6,
                @groupCode7 =@groupCode7,
                @groupCode8 =@groupCode8,
                @groupCode9 =@groupCode9, 
                @SqlQuery = @Temp OUTPUT;


                SET @ResultString = @ResultString + ' (' + @Temp  +') '

                SET  @Counter = @Counter+1 

            END
            SET @ResultSqlQuery = @ResultString

            RETURN
            --EXECUTE sp_executesql @ResultString

3 个答案:

答案 0 :(得分:0)

尽管@CurrentProjectId变量是nvarchar类型,但是您正在尝试分配使用CAST(ColumnData AS BigInt)代码转换为BigInt的值。

答案 1 :(得分:0)

如果stored_procedure2 @PrjId参数的类型为PrjId BigInt;使@CurrentProjectId变量类型为BigInt。  如果@PrjId参数的类型为nvarchar,请不要转换CAST(ColumnData AS BigInt),请直接使用ColumnData中的值。

答案 2 :(得分:0)

我遇到了同样的问题!以下解决方案也可以为您提供帮助。 可以说您有5个参数来运行存储过程。 使用SQL时,您只能选择输入其中三个的值,它仍然可以运行(实际上取决于。某些参数具有一些默认值,如果不导入,则将使用默认值。)。 / p>

但是,在Power BI中,您一定不要错过任何这些参数。如果存储过程需要5个参数,则必须为这些参数输入5个值。

祝你好运。