2

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 ?

Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
  • possible duplicate of [uniqueidentifier as Identity in SQLCE?](http://stackoverflow.com/questions/8763609/uniqueidentifier-as-identity-in-sqlce) – podiluska Aug 02 '12 at 13:09
  • You can use the guid generated from code, but you'll need to convert it to a VARCHAR(255). For example: `Insert into Customer (ID, Name) values (Convert(VARCHAR(255),'f5c7181e-e117-4a98-bc06-733638a3a264'),'MyCustomer')`. If you don't care about using the GUID generated in your C# code, then `Newid()` is perfect. See [Microsoft Documentation](https://msdn.microsoft.com/en-us/library/ms190348.aspx). – Jeremy Ray Brown Nov 16 '15 at 19:07

4 Answers4

5

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

Ram Singh
  • 6,664
  • 35
  • 100
  • 166
1

Can't you use NEWID() function?

Madhivanan
  • 13,470
  • 1
  • 24
  • 29
1

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.

lsoliveira
  • 4,560
  • 3
  • 20
  • 31
ccStars
  • 817
  • 2
  • 11
  • 34
0

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

jeremy
  • 904
  • 7
  • 15