Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 插入具有唯一标识符列的记录_Sql Server_Identifier - Fatal编程技术网

Sql server 插入具有唯一标识符列的记录

Sql server 插入具有唯一标识符列的记录,sql-server,identifier,Sql Server,Identifier,我想在名为[dbo].[Local]的表中插入新记录,该表包含以下列: [id] [uniqueidentifier] NOT NULL, [Component] [varchar](100) NULL, [Language] [varchar](10) NULL, [IsText] [bit] NULL, [key] [varchar](100) NULL, [value] [varchar](max) NULL, 而名为[id]的主键被聚集为: [id] ASC) WITH (PAD_I

我想在名为
[dbo].[Local]
的表中插入新记录,该表包含以下列:

[id] [uniqueidentifier] NOT NULL,
[Component] [varchar](100) NULL,
[Language] [varchar](10) NULL,
[IsText] [bit] NULL,
[key] [varchar](100) NULL,
[value] [varchar](max) NULL,
而名为[id]的主键被聚集为:

[id] ASC) 
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
insert命令如下所示。但是如何设置
[id]
列的值,因为在
[id]
列中输入值时出错。 如何将值自动插入此字段

INSERT INTO [dbo].[Local] ([id], [Component], [Language], [IsText], [key],[value])
VALUES (00000000-0000-0000-000-00000000000, 
        'Transport.Web', 'en', 1, 'ResourceTypeEmployee', 'TypeOffice')
GO

谢谢

您需要将要插入的值强制转换为
UNIQUEIDENTIFIER
-键入:

INSERT INTO [dbo].[Local]
    (         
    [id],
    [Component],
    [Language],
    [IsText],
    [key],
    [value])
VALUES
    (
     CAST('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER), 
     'Transport.Web',
     'en',
     1,
     'ResourceTypeEmployee',
     'TypeOffice'
    )
要插入新的随机
UNIQUEIDENTIFIER
,可以使用
NEWID()


插入选择面:

INSERT INTO foo 
([UNIQUEIDENTIFIER_col] ,[col2] , [...]) 
select NEWID() , [bar.selected_col1] , [...] from bar 
where (condition) 

您会得到什么错误?值“00000000-0000-0000-000-00000000000”只是查询中的一个示例。我实际上想要的是,每当我使用insert into语句添加新记录时,自动在uniqueidentifier中输入一个随机值。Select newid()可能重复,它将为您返回random
INSERT INTO foo 
([UNIQUEIDENTIFIER_col] ,[col2] , [...]) 
select NEWID() , [bar.selected_col1] , [...] from bar 
where (condition)