2

如何获取当前时间并将其输入 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]

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

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.

我究竟做错了什么?

4

1 回答 1

3

您可以使用 SQL Servergetdate()功能:

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

或者

insert into Audit (StartTime, . . .) values (getdate(), . . .)
于 2013-09-06T15:26:28.723 回答