简体   繁体   中英

Bringing stored procedure into Power BI from SQL Server: Error converting data type nvarchar to bigint

I am trying to bring in a stored procedure from SQL Server to use in Power BI for dashboards and reports. I get the following error message:

Details: "Microsoft SQL: Error converting data type nvarchar to bigint."

We use the stored procedure for basically all of our reports in Jaspersoft with no issue.

What could be the issue here?


    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 



EDIT: Here is the code for the 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

Although the @CurrentProjectId variable is nvarchar type, you are trying to assign a value converted to BigInt with CAST (ColumnData AS BigInt) code.

If the type of stored_procedure2 @PrjId parameter is PrjId BigInt; make @CurrentProjectId Variable type BigInt. If the type of @PrjId parameter is nvarchar, do not transform CAST (ColumnData AS BigInt), use the value directly from ColumnData.

I ran through the same problem. The solution below may help you as well. Lets say you have 5 parameters to run a stored procedures, While using SQL. you may only choose enter the values of three of them and it may still run (It depends actually, The some parameters have some default values, if you do not import it. the default values will be used.).

However, in Power BI, you MUST NOT miss any of those parameters. If the stored procedure needs 5 parameters you MUST enter 5 values for those parameters.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM