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

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

I know that I can use SELECT NEWID() to generate the UUID, but I need to go to the result of the request, copy the generated UUID to the clipboard (killing everything that was there before), and then return to the code and replace the request is a terrible way to handle with this.

Is there any feature (possibly using IntelliSense code snippets?) For this in SQL Server Management Studio that I haven't found yet?

To find out why I need this function, I often write SQL scripts, for example:

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

I cannot use just calling NEWID() , because later on (in another file) I want to access a specific line using:

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

How can I insert a UUID in a code editor?

+18
source share
2 answers

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

You do not need to put it in a separate window, and then copy the value of the fingerprint. Just put this function where you want to get the GUID value, and when the request is executed at run time, the value returned by this function will be used.

For example, in the 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 the paste the value returned from the NEWID () function, but you use this function. At runtime, the guid value will be reconfigured and inserted into col3.

Similarly, if you update

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

Again, you don’t need to copy the insert, the value returned from the NEWID () function, just the function itself is used.

Another option would be to assume that you are somewhere inside your code where you cannot call the NEWID() function. You would declare a variable of type UNIQUEIDENTIFIER, calling the function to save its value to this variable, and then use this variable inside the code, for example ...

 DECLARE @GUID_Value UNIQUEIDENTIFIER; SET @GUID_Value = NEWID(); -- Now use this variable anywhere in your code. 

Add to keyboard shortcut

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

  • Create a stored procedure that returns a GUID value.
  • Add a key shortcut to call the stored procedure.

Definition of Proc

 CREATE PROCEDURE get_Guid AS SELECT NEWID(); 

Add it to shortcuts

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

add the name of the stored procedure to the shortcut you want. Click OK. Close SSMS and open it again and you will go well.

enter image description here

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

+34
source

If you have the SQL Prompt extension installed, you can create a new fragment, for example ...

Fragment: guid

Description: new GUID

Code:

 '$GUID$' 

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

+1
source

Source: https://habr.com/ru/post/976608/


All Articles