Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Get NEWSEQUENTIALID Expand / Collapse
Author
Message
Posted Tuesday, October 19, 2010 12:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, June 03, 2012 10:05 PM
Points: 3, Visits: 40
Comments posted to this topic are about the item Get NEWSEQUENTIALID
Post #1006731
Posted Tuesday, October 19, 2010 10:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 1:29 PM
Points: 17, Visits: 133
Good idea.

Maybe I'm missing something, but, I don't think you need @NextSequentialID at all, or the XxX column, or some other complexity in the table.

This seems to work:
CREATE PROCEDURE dbo.sp_NewSequentialId
(@Id AS UNIQUEIDENTIFIER OUTPUT)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @NewSequentialId AS TABLE (Id UNIQUEIDENTIFIER DEFAULT(NEWSEQUENTIALID()))

INSERT INTO @NewSequentialId DEFAULT VALUES;

SELECT @Id = Id
FROM @NewSequentialId;
END
GO

Post #1007146
Posted Tuesday, October 19, 2010 2:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, June 03, 2012 10:05 PM
Points: 3, Visits: 40
thanks a lot...

"INSERT INTO @NewSequentialId DEFAULT VALUES;"

actually i don't know i can use DEFAULT VALUES :)-

Post #1007281
Posted Tuesday, October 19, 2010 2:43 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, June 03, 2012 10:05 PM
Points: 3, Visits: 40
Yes, that should be the right way to do.
Thanks gil.milbauer...

CREATE PROCEDURE dbo.sp_NewSequentialId
(@Id AS UNIQUEIDENTIFIER OUTPUT)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @NewSequentialId AS TABLE (Id UNIQUEIDENTIFIER DEFAULT(NEWSEQUENTIALID()))

INSERT INTO @NewSequentialId DEFAULT VALUES;

SELECT @Id = Id
FROM @NewSequentialId;
END
GO
Post #1007320
Posted Tuesday, October 26, 2010 2:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 01, 2013 10:16 AM
Points: 117, Visits: 180
I think I'm missing something too...

The purpose of the newsequentialid() is to be sequential in the context of a given table, yes? Therefore the SP would need to know the last uniqueidentifier used in a particular table in order to generate the next sequential uniqueidentifier, yes?

Perhaps the SP would need to accept a table name as a parameter, which it would query to find the last uniqueidentifier that would then use as the 'seed' value in the temp table, then inserting a new row to get the newsequentialid.

No?

Sorry if I'm missing something
Post #1010584
Posted Tuesday, October 26, 2010 10:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 1:29 PM
Points: 17, Visits: 133
Even though it has "Sequential" in name, the values themselves are not really adjacent to each other. It's just that the next value will be greater than any previous value from the function on this computer, so there's no need to reference the last value in the table. I guess that the "sequential" part means that if you use it as a clustered key, each row will be stored sequentially in the order they were created.
Post #1011003
Posted Wednesday, October 27, 2010 1:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 01, 2013 10:16 AM
Points: 117, Visits: 180
Ok... I guess I didn't really understand how the uniqueidentifier is generated - looks like it involves a sort of hex timestamp for the first 4 octets.

Thanks!
Post #1011307
Posted Wednesday, June 15, 2011 5:43 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 9:15 AM
Points: 55, Visits: 352
Hello SQL-ers,

It would seem that the very exact sounding error message is misleading...

"The newsequentialid() built-in function can only be used in a DEFAULT expression for a column of type 'uniqueidentifier' in a CREATE TABLE or ALTER TABLE statement. It cannot be combined with other operators to form a complex scalar expression."

One of our our devs kindly disproved this by inserting into a table with a newsequentialid() default column, just by a slightly different insert syntax.

Imagine the table has two columns, the second being the default column in question.

I was using:

insert [thetable] select 1

...which got the error msg - (oh no, I'm going to have to use newid() which causes fragmentation etc)

Whereas he succeeded using a column list in the insert defintion, ie:

insert [thetable] (col1) select 1

...which resulted in 1 in the first column and a correct newsequentialid() in the second column.

(this is all part of manually cranking the prep of tables for some merge replication crusade )
Post #1125617
Posted Wednesday, June 15, 2011 12:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 1:29 PM
Points: 17, Visits: 133
Paul,

It seems that you had just tried to supply the wrong number of columns in the original insert, and has nothing to do with newsequentialid() (it could have been any datatype with a default). If you don't specify the list of columns to be inserted, then SQL Server will expect all of the non-identity columns to be supplied by the subsequent value-providing clause.

I'm surprised you didn't get this error:
Column name or number of supplied values does not match table definition.


Specifying the column list (always a good practice) told SQL Server which column(s) to expect and to use the default value for any other column(s).
Post #1125979
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse