Sep 5, 2013

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.


If will execute following sql query in sql server:

SELECT NEWSEQUENTIALID()

We will get error message :

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.

Cause: Function NEWSEQUENTIALID() can only be used in the expression of default value of any column of a table. For example:

CREATE TABLE OrderHistory(
    OrderId BIGINT PRIMARY KEY,
    OrderKey UNIQUEIDENTIFIER DEFAULT(NEWSEQUENTIALID())
)

Insert statement:

INSERT INTO  OrderHistory(OrderId,OrderKey) VALUES(100,DEFAULT) 
INSERT INTO  OrderHistory(OrderId) VALUES(100)

Solution:

There is another function NEWID() which we can use instead of NEWSEQUENTIALID(). For example:

SELECT NEWID()

No comments:

Post a Comment