ROWGUIDCOL

ROWGUIDCOL is a property that can be assigned to a uniqueidentifier column in SQL server. It uniquely identifies a row within the table. This property helps in replication process to identify each row during merge replication. There can only be one ROWGUIDCOL column per table, and it can't alter once it's set without dropping and recreating the table. It generates a globally unique identifier (GUID) to populate column values automatically if the column also has the NEWID() or NEWSEQUENTIALID() function defined as the default value.

Example

CREATE TABLE Test_GUID
(
ID uniqueidentifier NOT NULL ROWGUIDCOL,
Name varchar(50)
)
-- Insert two rows
INSERT INTO Test_GUID (ID, Name)
VALUES (NEWID(), 'Test 1'), (NEWID(), 'Test 2')
-- Display rows
SELECT * FROM Test_GUID

Output

| ID | Name |
| ------------------------------------ | ----- |
| 6F9619FF-8B86-D011-B42D-00C04FC964FF | Test 1|
| 6F9619FF-8B86-D011-B42D-00C04FC964DE | Test 2|

Explanation

In this example, a column ‘ID’ is defined as a uniqueidentifier and assigned the property ROWGUIDCOL. This makes the ‘ID’ column a row globally unique identifier column which will store a unique value for each row, generated by the NEWID() function. Two rows are inserted into the table with uniqueidentifier values generated by the NEWID() function for each row, and finally the rows of the table are displayed. The ROWGUIDCOL property is used to specify the unique row identifier column in a table which is primarily used with replication.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.