如何在SQL Server中生成并手动插入一个uniqueidentifier? [英] How to generate and manually insert a uniqueidentifier in sql server?

查看:529
本文介绍了如何在SQL Server中生成并手动插入一个uniqueidentifier?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表中手动创建一个新用户,但是无法在不引发异常的情况下生成 UniqueIdentifier类型...

I'm trying to manually create a new user in my table but impossible to generate a "UniqueIdentifier" type without threw an exception ...

这是我的例如:

DECLARE @id uniqueidentifier
SET @id = NEWID()

INSERT INTO [dbo].[aspnet_Users]
           ([ApplicationId]
           ,[UserId]
           ,[UserName]
           ,[LoweredUserName]
           ,[LastName]
           ,[FirstName]
           ,[IsAnonymous]
           ,[LastActivityDate]
           ,[Culture])
     VALUES
           ('ARMS'
           ,@id
           ,'Admin'
           ,'admin'
           ,'lastname'
           ,'firstname'
           ,0
           ,'2013-01-01 00:00:00'
           ,'en')
GO

行异常->
消息8169,级别16,状态2,第4行
无法将字符串转换为uniqueidentifier。

Trow exception -> Msg 8169, Level 16, State 2, Line 4 Failed to convert a character string to uniqueidentifier.

我使用NEWID()方法,但是它不起作用...

I use the NEWID() method but it's not work...

http://www.dailycoding.com/Posts/generate_new_guid_uniqueidentifier_in_sql_server.aspx

推荐答案

ApplicationId必须为 UniqueIdentifier 类型。如果这样做,您的代码可以正常工作:

ApplicationId must be of type UniqueIdentifier. Your code works fine if you do:

DECLARE @TTEST TABLE
(
  TEST UNIQUEIDENTIFIER
)

DECLARE @UNIQUEX UNIQUEIDENTIFIER
SET @UNIQUEX = NEWID();

INSERT INTO @TTEST
(TEST)
VALUES
(@UNIQUEX);

SELECT * FROM @TTEST

因此,我认为这样做是安全的假定 ApplicationId 不是正确的数据类型。

Therefore I would say it is safe to assume that ApplicationId is not the correct data type.

这篇关于如何在SQL Server中生成并手动插入一个uniqueidentifier?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆