Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error converting data type varchar to bigint in stored procedure

I'm trying to call this procedure with the usp_TimesheetsAuditsLoadAllbyId 42747, NULL command.

But I always get an error

Msg 8114, Level 16, State 5, Procedure usp_TimesheetsAuditsLoadAllById, Line 9
Error converting data type varchar to bigint.

The ID of TimesheetsAudits table is a bigint type. I tried several types of conversions and casts, but I'm really stuck right now.

Hope somebody can help. Thanks

ALTER PROCEDURE [dbo].[usp_TimesheetsAuditsLoadAllById]
(
    @Id INT,
    @StartDate DATETIME
)
AS
BEGIN
   SET NOCOUNT ON

   SELECT TOP 51 * 
   FROM 
      (SELECT TOP 51 
          ID,
          Type, 
          ReferrerId,
          CAST(Description AS VARCHAR(MAX)) AS Description,
          OnBehalfOf,
          Creator,
          DateCreated 
       FROM 
          TimesheetsAudits 
       WHERE 
          (ReferrerID = @Id) AND
          (@StartDate IS NULL OR DateCreated < @StartDate)
       ORDER BY
          DateCreated DESC

       UNION

       SELECT TOP 51 
          tia.ID,
          tia.Type, 
          tia.ReferrerId,
          '[Day: ' + CAST(DayNr AS VARCHAR(5)) + '] ' + CAST(tia.Description AS VARCHAR(MAX)) AS Description,
          tia.OnBehalfOf,
          tia.Creator,
          tia.DateCreated 
       FROM 
          TimesheetItemsAudits tia
       INNER JOIN 
          TimesheetItems ti ON tia.ReferrerId = ti.ID
       WHERE 
          (ti.TimesheetID = @Id) AND
          (@StartDate IS NULL OR tia.DateCreated < @StartDate)
       ORDER BY 
          tia.DateCreated DESC) t
   ORDER BY 
       t.DateCreated DESC
END

Table definition for tables from comments:

CREATE TABLE [dbo].[TimesheetsAudits]( 
  [ID] [bigint] IDENTITY(1,1) NOT NULL, 
  [Type] [tinyint] NOT NULL, 
  [ReferrerId] [varchar](15) NOT NULL, 
  [Description] [text] NULL, 
  [OnBehalfOf] [varchar](10) NULL, 
  [Creator] [varchar](10) NOT NULL, 
  [DateCreated] [datetime] NOT NULL
)



CREATE TABLE [dbo].[TimesheetItemsAudits]( 
  [ID] [bigint] IDENTITY(1,1) NOT NULL, 
  [Type] [tinyint] NOT NULL, 
  [ReferrerId] [varchar](15) NOT NULL, 
  [Description] [text] NULL, 
  [OnBehalfOf] [varchar](10) NULL, 
  [Creator] [varchar](10) NOT NULL, 
  [DateCreated] [datetime] NOT NULL
)
like image 591
davser Avatar asked Aug 11 '14 20:08

davser


1 Answers

You perform an INNER JOIN of [dbo].[TimesheetsAudits] and TimesheetItems ti ON tia.ReferrerId = ti.ID

tia.[ReferrerId] is varchar and ti.[ID] is [bigint].

I'd expect a value in tia.[ReferrerId] that cannot be converted to bigint.

Try the following:

SELECT [ReferrerId] FROM TimesheetItemsAudits WHERE ISNUMERIC(ReferrerId) = 0

This may help you to find the "offending rows".

like image 80
Lmu92 Avatar answered Nov 19 '22 12:11

Lmu92