Farmer
Farmer

Reputation: 10983

Insert statement when having GUID as primary key

Is there a way to insert a row in SQL CE using the INSERT Statement when I have a GUID field.

In C#, I use

ID = Guid.NewGuid()

But in SQL console I can't find a keyword to do this.

I've found just this one :

> Insert into Customer (ID, Name) values ('f5c7181e-e117-4a98-bc06-733638a3a264','MyCustomer')

What about if I have a lot of rows to insert ?

Upvotes: 2

Views: 6152

Answers (4)

jeremy
jeremy

Reputation: 914

In C#, GUID are lower case so use LOWER(NEWID()) in SQL if you want them to look similar.

Upvotes: 0

ccStars
ccStars

Reputation: 815

To have a GUID on a field you can set the default of a column to be NEWSEQUENTIALID(). So, for example:

CREATE TABLE tableName (
    Guid UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID(),
    Name VARCHAR(100)
);

Then insert into the table by executing the following query:

INSERT INTO tableName (Name) VALUES ('Test');

I'm not sure if NEWSEQUENTIALID() is supported in CE or not, as I've never used CE for development.

Upvotes: 1

Madhivanan
Madhivanan

Reputation: 13700

Can't you use NEWID() function?

Upvotes: 1

Ram Singh
Ram Singh

Reputation: 6918

try to use Newid() in Sql server to generate GUID

Upvotes: 6

Related Questions