Questions tagged [newid]

NEWID() is a SQL Server function which creates a unique value of type uniqueidentifier.

NEWID() is a SQL Server function which creates a unique value of type uniqueidentifier.

Reference

MSDN article

73 questions
60
votes
4 answers

newid() inside sql server function

I have to insert a fake column at the result of a query, which is the return value of a table-value function. This column data type must be unique-identifier. The best way (I think...) is to use newid() function. The problem is, I can't use newid()…
Florjon
  • 3,569
  • 3
  • 22
  • 29
48
votes
5 answers

"order by newid()" - how does it work?

I know that If I run this query select top 100 * from mytable order by newid() it will get 100 random records from my table. However, I'm a bit confused as to how it works, since I don't see newid() in the select list. Can someone explain? Is there…
Tola Odejayi
  • 3,019
  • 9
  • 31
  • 46
32
votes
3 answers

SQL Server : does NEWID() always gives a unique ID?

Does the NEWID() function never give me the same ID as it already did? Let's say I select a NEWID() and it returns '1' (just as an example). Will it never return '1' again? Is it impossible?
user2254739
  • 321
  • 1
  • 3
  • 5
25
votes
5 answers

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

I am getting below error when I run sql query while copying data from one table to another, Msg 8170, Level 16, State 2, Line 2 Insufficient result space to convert uniqueidentifier value to char. My sql query is, INSERT INTO dbo.cust_info…
Sam Keith
  • 339
  • 1
  • 5
  • 5
9
votes
3 answers

Use NEWID() without losing distinct?

I am trying to create a new data extract from a (badly designed) sql database. The customer requires that I add a distinctidentifier which I am attempting to do using the NEWID() function. Unfortunately this leads to multiple duplicate records being…
Smoggie Tom
  • 306
  • 4
  • 12
9
votes
1 answer

SQL - 5% random sample by group

I have a table with about 10 million rows and 4 columns, no primary key. Data in Column 2 3 4 (x2 x3 and x4) are grouped by 50 groups identified in column1 X1. To get a random sample of 5% from table, I have always used SELECT TOP 5 PERCENT * FROM…
Siftekhar
  • 153
  • 2
  • 7
8
votes
1 answer

Get last inserted UNIQUEIDENTIFIER in SQL Server 2000

The OUTPUT clause is compatible with SQL Server 2005 but not SQL Server 2000. How do I convert this command to work in SQL Server 2000? CREATE TABLE sample ( ID uniqueidentifier NOT NULL DEFAULT newid(), Title varchar(30) NOT NULL ) INSERT INTO…
Shay
  • 1,680
  • 2
  • 26
  • 39
6
votes
1 answer

SQL - NEWID() producing the same guid on an update query

In a table I have created a new column that's going to hold a randomized alphanumeric string. I've been using the NEWID() function. From my understanding, NEWID should just create a new guid per row but all my rows end up with the same guid. My code…
Robyn Dias
  • 95
  • 1
  • 1
  • 7
6
votes
2 answers

Inconsistent results with NEWID() and PERSISTED computed column

I am getting odd results when using NEWID() in combination with a persistent computed column. Am I using some function wrong? Not using persisted when creating the column, and therefore calculating values when selecting them, will return correct…
Kristofer
  • 675
  • 7
  • 15
6
votes
1 answer

Why does using NEWID() use more space than NEWSEQUENTIALID()?

I'm in the middle of doing a bit of research, and I've come across an anomaly that I can't explain (and I've not been able to find anything on Google). Consider the following SQL: CREATE TABLE MyGuid ( ID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT…
Spikeh
  • 3,540
  • 4
  • 24
  • 49
5
votes
4 answers

Changing newid() to newsequentialid() on an existing table

At the moment we have a number of tables that are using newid() on the primary key. This is causing large amounts of fragmentation. So I would like to change the column to use newsequentialid() instead. I imagine that the existing data will remain…
cbp
  • 25,252
  • 29
  • 125
  • 205
4
votes
2 answers

Is MsSQL NEWID bad like MySQL Rand()

according to this article: http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/ using ORDER BY RAND() is a bad idea because: The problem is, MySQL will have to perform RAND() operation (which takes processing power) for every single…
Esben
  • 1,943
  • 1
  • 18
  • 37
4
votes
3 answers

How to use newid() to assign a value to a stored procedure variable?

I am trying to generate a new GUID and assign that value to NewReportID. But, I am unsure that if I replace @NewReportID everywhere in the procedure by newid() that it will generate a new GUID for each line. What do I have to generate just one GUID…
Bulvak
  • 1,794
  • 8
  • 31
  • 63
3
votes
2 answers

Remove autogenerate GUID column from Add Entry pages in Dynamic Data Web Application

I have a Dynamic Data Entities Web Application that uses a database with GUIDs as primary keys for tables. All of the GUID fields have the default value set to NEWID() so SQL Server will generate a new GUID for any record. However in my dynamic data…
kjv
  • 11,047
  • 34
  • 101
  • 140
3
votes
1 answer

Odoo 8 Many2many validate and delete lines

How I wanna check if the product_id of a new line added to the mrp.bom.line (Many2many) is equal to a fixed id ('=17'), which may never be added to a BoM. But I can't seem to do this cus he stores the new line as a "NewId", he only writes it to the…
Jesse
  • 727
  • 13
  • 44
1
2 3 4 5