消息8152字符串或二进制数据将被截断

时间:2019-07-01 02:26:47

标签: sql-server sql-insert create-table

我知道之前已经有人问过这个问题,但是没有一个答案能解决问题,需要另一只眼睛。我正在尝试创建一个表,并用一些值播种它。我没有任何价值约束,所以我不确定为什么会遇到错误。

CREATE TABLE [dbo].[AvailableTime]
(
    [Id] [INT] IDENTITY(1,1) NOT NULL,
    [TimeString] [NCHAR] NOT NULL,
    [TimeValue] [NCHAR] NOT NULL,
    [CompanyId] [INT] NOT NULL,
    [CompanyName] [NVARCHAR](MAX) NULL,
    [LocationId] [INT] NOT NULL,
    [LocationName] [NVARCHAR] NOT NULL,
    [IsClaimed] [BIT] NOT NULL,

    CONSTRAINT [PK_AvailableTime] 
        PRIMARY KEY CLUSTERED ([Id] 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]
GO

INSERT INTO dbo.AvailableTime (TimeString, TimeValue, CompanyId, CompanyName, 
                               LocationId, LocationName, IsClaimed) 
VALUES ('2019-01-07T00:00:00', '12:00 AM', 1, 'Company', 
        2, 'Inver Grove', 'FALSE');

3 个答案:

答案 0 :(得分:2)

绝对不要指定CHARVARCHARNCHARNVARCHAR列(或变量或参数),而不能指定显式长度

如果省略了特定的长度,在某些情况下,您将得到长度为恰好一个字符的变量或列!这通常是不需要您想要的!

ALSO::为什么将TimeStringTimeValue存储为NCHAR?没什么意义-使用最合适的数据类型-此处为DATETIME2(n)TIME

因此,如下定义您的表:

CREATE TABLE [dbo].[AvailableTime]
(
    [Id] [INT] IDENTITY(1,1) NOT NULL,
    [TimeString] DATETIM2(0) NOT NULL,
    [TimeValue] TIME(0) NOT NULL,
    [CompanyId] [INT] NOT NULL,
    [CompanyName] [NVARCHAR](MAX) NULL,
    [LocationId] [INT] NOT NULL,
    [LocationName] [NVARCHAR](100) NOT NULL,
    [IsClaimed] [BIT] NOT NULL,

你应该没事。

答案 1 :(得分:1)

您需要在表中声明CHARVARCHAR字段的大小:

CREATE TABLE [dbo].[AvailableTime]
(
    [Id] [INT] IDENTITY(1,1) NOT NULL,
    [TimeString] [NCHAR](50)  NOT NULL,
    [TimeValue] [NCHAR](50)  NOT NULL,
    [CompanyId] [INT] NOT NULL,
    [CompanyName] [NVARCHAR](MAX) NULL,
    [LocationId] [INT] NOT NULL,
    [LocationName] [NVARCHAR](50) NOT NULL,
    [IsClaimed] [BIT] NOT NULL,

    CONSTRAINT [PK_AvailableTime] 
        PRIMARY KEY CLUSTERED ([Id] 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]
GO

答案 2 :(得分:0)

您在插入查询时出错,最后一列的类型不匹配。

    public static string Run()
    {
        string fileName = @"C:\temp\text.py";

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"python", fileName)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        return output;
    }