将varbinary数据插入SQL Server数据库

时间:2021-09-04 15:03:11

I have this table:

我有这个表:

   (<NUM_TRF int
   ,<NAME, varchar(255),>
   ,<DESCRIPTION, text,>
   ,<REPORT, varbinary(max),>)

I try to create a script in SQL Server 2008, in order to insert a line on my local database,

我尝试在SQL Server 2008中创建一个脚本,以便在本地数据库中插入一行,

INSERT INTO [MY_DB_APP].[dbo].[CONNECT_USER]
VALUES(1, 'name', 'description', Cast('wahid' As varbinary(max)) )
GO

but I get this error:

但是我得到了这个错误:

String or binary data would be truncated.
The statement has been terminated.

字符串或二进制数据将被截断。声明已被终止。

1 个解决方案

#1


15  

Two issues:

两个问题:

Issue #1: don't use TEXT anymore - it's deprecated. Use a VARCHAR(n) with a suitable size of n, or if you really must (only if you REALLY must), use VARCHAR(MAX)

问题1:不要再使用文本——它已经被弃用了。使用大小为n的VARCHAR(n),或者如果您确实需要(仅当您确实需要时),则使用VARCHAR(MAX)

CREATE TABLE dbo.CONNECT_USER
(
    NUM_TRF int,
    NAME varchar(255),
    DESCRIPTION varchar(1000),
    REPORT varbinary(max)
)

I would personally also avoid writing EVERYTHING IN ALL CAPS - this just makes it so much harder to read! And I would try to avoid very generic column names like Name or Description - those are not very intuitive, and might collide with other table's columns and / or with SQL Server reserved keywords. Try to use more expressive, more context-related column names that make sense in your environment (ConnectUsername or whatever)

我个人也会避免把所有的东西都写进大写——这只会让阅读变得更加困难!我将尽量避免使用非常通用的列名,比如名称或描述——它们不是很直观,可能会与其他表的列和/或SQL Server保留的关键字发生冲突。尽量使用更有表现力的、与上下文相关的、在您的环境中有意义的列名(ConnectUsername或其他)

Issue #2: when doing an INSERT, I would recommend to always define the column you want to insert into. This avoids unpleasant surprises when a table is restructured or new columns are added:

问题2:在进行插入时,我建议始终定义要插入的列。这避免了在重新构造表或添加新列时出现令人不快的意外:

INSERT INTO [MY_DB_APP].[dbo].[CONNECT_USER](NUM_TRF, NAME, DESCRIPTION, REPORT)
VALUES(1, 'name', 'description', CAST('wahid' AS VARBINARY(MAX)))
GO

#1


15  

Two issues:

两个问题:

Issue #1: don't use TEXT anymore - it's deprecated. Use a VARCHAR(n) with a suitable size of n, or if you really must (only if you REALLY must), use VARCHAR(MAX)

问题1:不要再使用文本——它已经被弃用了。使用大小为n的VARCHAR(n),或者如果您确实需要(仅当您确实需要时),则使用VARCHAR(MAX)

CREATE TABLE dbo.CONNECT_USER
(
    NUM_TRF int,
    NAME varchar(255),
    DESCRIPTION varchar(1000),
    REPORT varbinary(max)
)

I would personally also avoid writing EVERYTHING IN ALL CAPS - this just makes it so much harder to read! And I would try to avoid very generic column names like Name or Description - those are not very intuitive, and might collide with other table's columns and / or with SQL Server reserved keywords. Try to use more expressive, more context-related column names that make sense in your environment (ConnectUsername or whatever)

我个人也会避免把所有的东西都写进大写——这只会让阅读变得更加困难!我将尽量避免使用非常通用的列名,比如名称或描述——它们不是很直观,可能会与其他表的列和/或SQL Server保留的关键字发生冲突。尽量使用更有表现力的、与上下文相关的、在您的环境中有意义的列名(ConnectUsername或其他)

Issue #2: when doing an INSERT, I would recommend to always define the column you want to insert into. This avoids unpleasant surprises when a table is restructured or new columns are added:

问题2:在进行插入时,我建议始终定义要插入的列。这避免了在重新构造表或添加新列时出现令人不快的意外:

INSERT INTO [MY_DB_APP].[dbo].[CONNECT_USER](NUM_TRF, NAME, DESCRIPTION, REPORT)
VALUES(1, 'name', 'description', CAST('wahid' AS VARBINARY(MAX)))
GO