字符串或二进制数据将在输入数据库时​​截断

时间:2013-09-06 15:19:56

标签: java sql sql-server

如何获取当前时间并将其输入SQL Server。有我的表:

CREATE TABLE [dbo].[Audit](
    [PK_Audit] [int] IDENTITY(1,1) NOT NULL,
    [SequenceNumber] [int] NOT NULL,
    [TableName] [varchar](50) NOT NULL,
    [RunDate] [datetime] NOT NULL,
    [StartTime] [datetime] NOT NULL,
    [EndTime] [datetime] NOT NULL,
    [Description] [varchar](max) NULL,
    [Status] [varchar](50) NOT NULL,
    [ProcessName] [nchar](10) NOT NULL,
    [InsertCount] [int] NOT NULL,
    [UpdateCount] [int] NULL,
    [DeleteCount] [int] NULL,
 CONSTRAINT [PK_Audit] PRIMARY KEY CLUSTERED 
(
    [PK_Audit] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我正在尝试使用calander来获取时间并将其输入数据库:

Calendar cal = Calendar.getInstance();
java.util.Date start = cal.getTime();

对我而言,您似乎需要将其转换为SQL理解的内容,因此我执行了以下操作:

audit.setDate(4, new java.sql.Date(start.getTime()));

当我执行查询时,我收到以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您可以使用SQL Server getdate()功能:

update Audit set StartTime=getdate(), . . . where . . .

insert into Audit (StartTime, . . .) values (getdate(), . . .)