Sql Server - 结果空间不足,无法将 uniqueidentifier 值转换为 char

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5628968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:07:03  来源:igfitidea点击:

Sql Server - Insufficient result space to convert uniqueidentifier value to char

sqlsql-servernewid

提问by Sam Keith

I am getting below error when I run sql query while copying data from one table to another,

当我在将数据从一个表复制到另一个表时运行 sql 查询时出现以下错误,

Msg 8170, Level 16, State 2, Line 2 Insufficient result space to convert uniqueidentifier value to char.

消息 8170,级别 16,状态 2,第 2 行 结果空间不足,无法将 uniqueidentifier 值转换为字符。

My sql query is,

我的 sql 查询是,

INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
NEWID(),
first_name,
last_name
FROM dbo.tmp_cust_info

My create table scripts are,

我的创建表脚本是,

CREATE TABLE [dbo].[cust_info](
    [uid] [varchar](32) NOT NULL,
    [first_name] [varchar](100) NULL,
    [last_name] [varchar](100) NULL)

CREATE TABLE [dbo].[tmp_cust_info](
    [first_name] [varchar](100) NULL,
    [last_name] [varchar](100) NULL)

I am sure there is some problem with NEWID(), if i take out and replace it with some string it is working.

我确定NEWID()存在一些问题,如果我取出并用一些字符串替换它,它正在工作。

I appreciate any help. Thanks in advance.

我很感激任何帮助。提前致谢。

回答by Remus Rusanu

A guid needs 36 characters (because of the dashes). You only provide a 32 character column. Not enough, hence the error.

guid 需要 36 个字符(因为有破折号)。您只提供 32 个字符的列。不够,因此错误。

回答by RichardTheKiwi

You need to use one of 3 alternatives

您需要使用 3 种选择之一

1, A uniqueidentifier column, which stores it internally as 16 bytes. When you select from this column, it automatically renders it for displayusing the 8-4-4-4-12 format.

1,一个uniqueidentifier列,内部存储为16个字节。当您从此列中选择时,它会使用 8-4-4-4-12 格式自动呈现以供显示

CREATE TABLE [dbo].[cust_info](
    [uid] uniqueidentifier NOT NULL,
    [first_name] [varchar](100) NULL,
    [last_name] [varchar](100) NULL)

2, not recommendedChange the field to char(36) so that it fits the format, including dashes.

2、不推荐将字段更改为char(36),使其符合格式,包括破折号。

CREATE TABLE [dbo].[cust_info](
    [uid] char(36) NOT NULL,
    [first_name] [varchar](100) NULL,
    [last_name] [varchar](100) NULL)

3, not recommendedStore it without the dashes, as just the 32-character components

3、不推荐不带破折号存放,只存放32个字符的组件

INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
replace(NEWID(),'-',''),
first_name,
last_name
FROM dbo.tmp_cust_info

回答by Chanderkant Sharma

Increase length of your uid column from varchar(32) ->varchar(36) because guid take 36 characters Guid.NewGuid().ToString() -> 36 characters outputs: 12345678-1234-1234-1234-123456789abc

从 varchar(32) ->varchar(36) 增加 uid 列的长度,因为 guid 需要 36 个字符 Guid.NewGuid().ToString() -> 36 个字符输出:12345678-1234-1234-1234-123456789abc

回答by Jasel

I received this error when I was trying to perform simple string concatenation on the GUID. Apparently a VARCHARis not big enough.

当我尝试对 GUID 执行简单的字符串连接时收到此错误。显然aVARCHAR不够大。

I had to change:

我不得不改变:

SET @foo = 'Old GUID: {' + CONVERT(VARCHAR, @guid) + '}';

SET @foo = 'Old GUID: {' + CONVERT(VARCHAR, @guid) + '}';

to:

到:

SET @foo = 'Old GUID: {' + CONVERT(NVARCHAR(36), @guid) + '}';

SET @foo = 'Old GUID: {' + CONVERT(NVARCHAR(36), @guid) + '}';

...and all was good. Huge thanks to the prior answers on this one!

......一切都很好。非常感谢之前对此的回答!