How to insert a NEWID() / GUID / UUID into the code editor?

116,940

Solution 1

NEWID() itself is a function. when called returns a GUID value.

You do not have to put it in a separate window and then copy paste value from there. Just simply put that function there where you want the GUID value and when the query is executed at run time the value returned by this function will be used.

For instance in an Insert statement

INSERT INTO TableName (Col1 , Col2, Col3)
VALUES (1 , 'Value 1', NEWID())  

If you want col3 to have a GUID value you do not need to copy paste the value returned from NEWID() function but you use the function itself. At runtime a guid value will be retuned and inserted into col3.

Similarly if you were updating

UPDATE TableName 
  SET Col3 = NEWID()
WHERE <Some Condition>

Again you dont have to copy paste the value returned from NEWID() function just use the function itself.

Another Option would be suppose you are somewhere inside your code where you cannot call the NEWID() function . You would Declare a variable of type UNIQUEIDENTIFIER call the function store its value to that variable and then use that variable inside you code something like ...

DECLARE @GUID_Value UNIQUEIDENTIFIER;
 SET @GUID_Value = NEWID();

-- Now use this variable anywhere in your code.  

Adding to Keyboard Shortcut

For some strange reason if you want to add a shortcut to your SSMS to generate GUIDs for you. You would need to two thing.

  1. Create a stored Procedure which returns GUID value .
  2. Add a key shortcut to call that stored Procedure.

Proc Definition

CREATE PROCEDURE get_Guid
AS 
 SELECT NEWID();

Add it to shortcuts

From your SSMS goto Tools --> Options --> Environment --> Keyboard

add the stored procedure's name to the shortcut you want to. Click OK. Close SSMS and reopen it again and you are good to go.

enter image description here

As shown in the above snipshot, now if you press CTRL + 0 it will generate a GUID value for you in the same query window.

Solution 2

If you have the SQL Prompt extension installed, you can create a new snippet like so...

Snippet: guid

Description: new GUID

Code:

'$GUID$'

Now type guid in your text editor and you'll get a new GUID surrounded by 's.

Solution 3

For SQL server, and maybe others.

One can insert a string-encoded Guid/uuid in SQL server using the convert function. See the zeroed Guid in the example below.

INSERT INTO [schema].[table]
           ([Id]
           ,[stuff])
     VALUES
           (Convert(uniqueidentifier, N'00000000-0000-0000-0000-000000000000')
           ,'Interesting Stuff'
)
Share:
116,940
Jens Mühlenhoff
Author by

Jens Mühlenhoff

Software developer: Delphi SQL (various dialects) Haskell Vala (for Linux development) C++ (modern C++ rocks) C (if it can not be avoided ...) x86 assembly At work we mostly develop Windows solutions (with an emphasis on database independency). I'm using Gentoo and Debian GNU/Linux at home and have extensive Linux knowledge. I also like to play with Debian GNU/Hurd from time to time :). Please vote at this Feedback page: https://feedback.azure.com/forums/908035-sql-server/suggestions/32892454-need-a-way-to-set-the-default-encoding-for-query-f

Updated on January 20, 2021

Comments

  • Jens Mühlenhoff
    Jens Mühlenhoff over 3 years

    Many code editors have a built-in menu item or keyboard function to get a UUID, for example when I press CTRL + SHIFT + G in Delphi it inserts a GUID at the current position in the source code.

    I know that I can use SELECT NEWID() to generate a UUID, but I have to go to the query result, copy the generated UUID to my clipboard (killing whatever was in there before) and then go back to the code and replace the query which is an awful way of dealing with this.

    Is there any function (maybe using IntelliSense code snippets?) to do this in SQL Server Management Studio that I haven't found yet?

    Some background on why I need this function, I often write SQL Scripts like this:

    INSERT INTO table (table_id, value) 
    VALUES ('112C8DD8-346B-426E-B06C-75BBA97DCD63', 'ABC');
    

    I can't use just call NEWID(), because I later (in another file) want to refer to the specific row using:

    WHERE table_id = '112C8DD8-346B-426E-B06C-75BBA97DCD63'
    

    How can I insert a UUID into the code editor?