SQL 从字符串转换为 uniqueidentifier 时转换失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1476861/
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 03:45:39  来源:igfitidea点击:

Conversion failed when converting from a character string to uniqueidentifier

sqlsql-server-2008asp-classic

提问by jamesmhaley

Created a stored procedure in SQL 9 (2005) and have since upgraded to SQL 10 (2008). Since then, the following stored procedure has stopped working and thrown up the above error:

在 SQL 9 (2005) 中创建了一个存储过程,并已升级到 SQL 10 (2008)。从那时起,以下存储过程停止工作并引发上述错误:

ALTER PROCEDURE [dbo].[GetModifiedPages] 
    @vPortalUID         nvarchar(32) = ''
AS
BEGIN
    -- Convert GUID to UI
    DECLARE @nPortalUID AS uniqueidentifier
    SET @nPortalUID = CAST(@vPortalUID AS uniqueidentifier)

The passed in param @vPortalUID contains: 2A66057D-F4E5-4E2B-B2F1-38C51A96D385. I execute the stored proc like this:

传入的参数@vPortalUID 包含:2A66057D-F4E5-4E2B-B2F1-38C51A96D385。我像这样执行存储过程:

EXEC GetModifiedPages '2A66057D-F4E5-4E2B-B2F1-38C51A96D385'

It falls over. I have tried Convert aswell. Still no joy. Have also had the value going in with { } around it. I removed these programatically and manually as above.

它倒下。我也试过转换。还是没有快乐。在它周围也有 { } 的价值。我如上所述以编程方式和手动方式删除了这些。

If you are interested I am running the SP from an ASP Classic page, although that should not affect this as the above code was run using SSMS.

如果您有兴趣,我正在从 ASP Classic 页面运行 SP,尽管这不会影响到这一点,因为上面的代码是使用 SSMS 运行的。

Thanks in advance for your help. James

在此先感谢您的帮助。詹姆士

回答by KM.

this fails:

这失败了:

 DECLARE @vPortalUID NVARCHAR(32)
 SET @vPortalUID='2A66057D-F4E5-4E2B-B2F1-38C51A96D385'
 DECLARE @nPortalUID AS UNIQUEIDENTIFIER
 SET @nPortalUID = CAST(@vPortalUID AS uniqueidentifier)
 PRINT @nPortalUID

this works

这有效

 DECLARE @vPortalUID NVARCHAR(36)
 SET @vPortalUID='2A66057D-F4E5-4E2B-B2F1-38C51A96D385'
 DECLARE @nPortalUID AS UNIQUEIDENTIFIER
 SET @nPortalUID = CAST(@vPortalUID AS UNIQUEIDENTIFIER)
 PRINT @nPortalUID

the difference is NVARCHAR(36), your input parameter is too small!

不同的是NVARCHAR(36),你的输入参数太小了!