Chapter 2: Transact-SQL Functions
newid
Description
Generates human-readable, globally unique IDs (GUIDs) in two different formats, based on arguments you provide. The length of the human-readable format of the GUID value is either 32 bytes (with no dashes) or 36
bytes (with dashes).
Syntax
newid([optionflag])
Parameters
option flag
-
0, or no value – the GUID generated is human-readable (varchar), but does not include dashes. This argument, which is the default, is useful for converting values intovarbinary.
-
-1 – the GUID generated is human-readable (varchar) and includes dashes.
-
-0x0 – returns the GUID as avarbinary.
-
Any other value fornewidreturns NULL.
Examples
Example 1
Creates a table withvarcharcolumns 32 bytes long, then usesnewidwith no arguments with theinsertstatement:
create table t (UUID varchar(32))
go
insert into t values (newid())
insert into t values (newid())
go
select * from t
UUID
--------------------------------
f81d4fae7dec11d0a76500a0c91e6bf6
7cd5b7769df75cefe040800208254639
Example 2
Produces a GUID that includes dashes:
select newid(1)
------------------------------------
b59462af-a55b-469d-a79f-1d6c3c1e19e3
Example 3
Returns a new GUID of typevarbinaryfor every row that is returned from the query:
select newid(0x0) from sysobjects
Example 4
Usesnewidwith thevarbinarydatatype:
sp_addtype binguid, "varbinary(16)"
create default binguid_dflt
as
newid(0x0)
sp_bindefault "binguid_dflt","binguid"
create table T1 (empname char(60), empid int, emp_guid binguid)
insert T1 (empname, empid) values ("John Doe", 1)
insert T1 (empname, empid( values ("Jane Doe", 2)
Usage
-
newidgenerates two values for the globally unique ID (GUID) based on arguments you pass tonewid. The default argument generates GUIDs without dashes.
By defaultnewidreturns new values for every filtered row.
-
You can usenewidin defaults, rules, and triggers, similar to other functions.
-
Make sure the length of thevarcharcolumn is at least 32 bytes for the GUID format without dashes, and at least 36 bytes for the GUID format with dashes. The column length is truncated
if it is not declared with these minimum required lengths. Truncation increases the probability of duplicate values.
-
An argument of zero is equivalent to the default.
-
Because GUIDs are globally unique, they can be transported across domains without generating duplicates.
Standards
ANSI SQL – Compliance level: Transact-SQL extension.
Permissions
Any user can executenewid.
分享到:
相关推荐
在SQL Server中,随机函数有rand(),NewID(),其中rand是在0到1内随机取数,NewID则是生成随机的uniqueidentifier唯一标识符。 SELECT * FROM Northwind..Orders ORDER BY NEWID() –随机排序 SELECT TOP 10 * ...
- `newid([optionflag])`:生成一个32位或36位的可读随机字符串。 #### SQL Debugger ASE 12.5.0.3 还提供了一个命令行调试工具 sqldbgr,该工具可用于调试存储过程和触发器,极大地提高了开发效率和问题定位能力...
SQL Server: 代码如下:Select TOP N * From TABLE Order By NewID() view plaincopy to clipboardprint?Select TOP N * From TABLE Order By NewID() Select TOP N * From TABLE Order By NewID()NewID()函数将...
782316445881261DeepTesting_realme-release_20210426_newID_signed.apk
/ip address set [find comment=2] address=$NewID2 network=$NewID2 broadcast=$NewID2 /ip route set [find comment=2] gateway=$NewID2} } :if ([/in pppoe-c get [find name=ADSL-WAN3] running]=true) do { :...
代码如下:CREATE TABLE Ceshi( id VARCHAR(50) PRIMARY KEY NOT NULL, cname VARCHAR(30) )GO INSERT INTO CeshiSELECT NEWID(),’jack1′ UNIONSELECT NEWID(),’jack2′ UNIONSELECT NEWID(),’jack3′ UNION...
在SQL Server中,如果需要从一张表中随机抽取指定数量的数据记录,可以利用`NEWID()`函数结合`ORDER BY`子句来实现这一功能。下面将详细介绍这一过程。 #### 二、`NEWID()`函数解释 `NEWID()`函数是SQL Server中的...
`NEWID()`函数生成一个新的全局唯一标识符(GUID),每次调用都会返回不同的值,因此通过`ORDER BY NEWID()`可以将数据行进行无序排列,再配合`TOP 10`即可获取前10条随机记录。示例如下: ```sql SELECT TOP 10 * ...
`NEWID()` 函数则是一种生成这种唯一标识符的有效方式。 #### 示例详解 下面的示例展示了如何在 SQL Server 中使用 `NEWID()` 函数为 `uniqueidentifier` 类型的变量赋值,并打印出这个值。 ```sql DECLARE @myid...
`NEWID()`函数会生成一个唯一无序的GUID,因此通过`ORDER BY NEWID()`可以达到随机排序的效果。例如: ```sql SELECT TOP 50 [id] FROM [dbo].[RANDTEST] GROUP BY ID ORDER BY NEWID(); ``` 这条SQL语句从`...
3.newID = rs.Fields(“recordID”) 4.newID为刚添加的记录的ID值 ASP+SQL Server 2000 1.要获取的ID值字段属性必须设为:自动编号(我们假设字段名为recordID) 2.添加记录代码模式: 代码如下:Cn.Execute”INSERT ...
具体来说,newid()函数可与SQL的SELECT和UPDATE语句结合使用,直接在数据库层面完成随机抽取试题的功能。 为了演示这一过程,我们可以设计一个名为test的数据库以及其中的一个名为mytest的表。表mytest包括两个字段...
ID3(Iterative Dichotomiser 3)是决策树算法的一种早期版本,由Ross Quinlan在1986年提出。这个算法主要用于分类任务,通过递归地选择最佳属性来构建决策树。"New ID3_v2_strongmev_decisiontree_id3_剪枝_决策树_...
`NEWID()`函数返回一个随机生成的唯一标识符(GUID),每次调用时都会产生不同的值。因此,将其用于`ORDER BY`子句中可以实现记录的随机排序。 ### SQL代码解析 提供的SQL代码片段是一个典型的随机排序示例: ```...
`TOP`关键字用来选取指定数量的记录,而`NEWID()`则生成一个全局唯一标识符(GUID),通过它我们可以实现随机性。 以下是一个基本的SQL查询示例,用于从名为"TableName"的表中随机抽取5条记录: ```sql SELECT TOP...
在SQL Server中,触发器是一种特殊的存储过程,它在特定的数据库操作(如INSERT、UPDATE、DELETE)发生时自动执行,以实现复杂的数据完整性规则或业务逻辑。本篇将深入探讨触发器的基本概念、类型、分类及如何实现...
$("#tabs ul").append("<li><a href='#" + newId + "'>" + newTabTitle + "</a></li>"); // 添加新的内容区域 $(newTabPageContent).appendTo("#tabs"); // 更新页签并激活新添加的页签 $("#tabs").tabs("add...