James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

SQL Server "uniqueidentifier"

12:23 PM
You can use the NewID() or NewSequentialID() function to create a default GUID value in a UniqueIdentifier field.

Procedure:

  1. Create your table with a column for your GUID field. Let's call it MyGUID.
  2. Set the Data Type as uniqueidentifier.
  3. Set the Default Value or Binding to NewID().
  4. Save your changes.
Now when you add a row to the table the GUID will automatically be populated with a GUID. Notice that no { or } are included.

If you need to retroactively set the values you can run a SQL statement such as:

INSERT INTO tableName DEFAULT VALUES
NOTE: Of course this will set the default values for all fields that need it if they are null.
Or more selectively only change those that are null:
UPDATE tableName SET MyGUID=NEWID() WHERE MyGUID=null

0 comments:

 
Toggle Footer
Top