将数据类型varchar转换为存储过程中的bigint时出错

时间:2014-08-11 20:28:16

标签: sql sql-server tsql bigint

我正在尝试使用usp_TimesheetsAuditsLoadAllbyId 42747, NULL命令调用此过程。

但我总是收到错误

  

Msg 8114,Level 16,State 5,Procedure usp_TimesheetsAuditsLoadAllById,Line 9
  将数据类型varchar转换为bigint时出错。

ID表的TimesheetsAuditsbigint类型。我尝试了几种类型的转换和演员表,但我现在真的被卡住了。

希望有人可以提供帮助。谢谢

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

来自评论的表的表定义:

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
)

1 个答案:

答案 0 :(得分:6)

您执行[dbo]的内部联接。[TimesheetsAudits]和TimesheetItems ti ON tia.ReferrerId = ti.ID

tia。[ReferrerId]是varchar和ti。[ID]是[bigint]。

我期待tia中的值。[ReferrerId]无法转换为bigint。

尝试以下方法:

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

这可以帮助您找到&#34;违规行&#34;。