SQL 如何在代码编辑器中插入 NEWID() / GUID / UUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26315207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a NEWID() / GUID / UUID into the code editor?
提问by Jens Mühlenhoff
Many code editors have a built-in menu item or keyboard function to get a UUID, for example when I press CTRL+ SHIFT+ Gin Delphi it inserts a GUID at the current position in the source code.
许多代码编辑器都有一个内置的菜单项或键盘功能来获取 UUID,例如当我在 Delphi 中按CTRL+ SHIFT+ 时G,它会在源代码的当前位置插入一个 GUID。
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.
我知道我可以SELECT NEWID()
用来生成 UUID,但我必须转到查询结果,将生成的 UUID 复制到我的剪贴板(删除之前的任何内容),然后返回代码并替换查询,这是一个可怕的处理方式。
Is there any function (maybe using IntelliSense code snippets?) to do this in SQL Server Management Studio that I haven't found yet?
是否有任何函数(可能使用 IntelliSense 代码片段?)在 SQL Server Management Studio 中执行此操作但我还没有找到?
Some background on why I need this function, I often write SQL Scripts like this:
关于为什么我需要这个函数的一些背景,我经常写这样的 SQL 脚本:
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:
我不能只使用 call NEWID()
,因为我稍后(在另一个文件中)想使用以下方法引用特定行:
WHERE table_id = '112C8DD8-346B-426E-B06C-75BBA97DCD63'
How can I insert a UUID into the code editor?
如何在代码编辑器中插入 UUID?
回答by M.Ali
NEWID()
itself is a function. when called returns a GUID value.
NEWID()
本身就是一个函数。调用时返回一个 GUID 值。
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.
您不必将其放在单独的窗口中,然后从那里复制粘贴值。只需将该函数放在您想要 GUID 值的位置,当在运行时执行查询时,将使用该函数返回的值。
For instance in an Insert statement
例如在 Insert 语句中
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.
如果您希望 col3 具有 GUID 值,则无需复制粘贴从 NEWID() 函数返回的值,而是使用该函数本身。在运行时,guid 值将被重新调整并插入 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.
同样,您不必复制粘贴从 NEWID() 函数返回的值,只需使用函数本身即可。
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 ...
另一个选项是假设您在代码中无法调用NEWID()
函数的某个地方。您将声明一个类型为 UNIQUEIDENTIFIER 的变量,调用该函数将其值存储到该变量中,然后在您的代码中使用该变量,例如...
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.
出于某种奇怪的原因,如果您想向 SSMS 添加快捷方式来为您生成 GUID。你需要做两件事。
- Create a stored Procedure which returns GUID value .
- Add a key shortcut to call that stored Procedure.
- 创建一个返回 GUID 值的存储过程。
- 添加一个快捷键来调用该存储过程。
Proc Definition
过程定义
CREATE PROCEDURE get_Guid
AS
SELECT NEWID();
Add it to shortcuts
将其添加到快捷方式
From your SSMS goto Tools --> Options --> Environment --> Keyboard
从您的 SSMS 转到工具 --> 选项 --> 环境 --> 键盘
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.
将存储过程的名称添加到您想要的快捷方式。单击确定。关闭 SSMS 并重新打开它,您就可以开始了。
As shown in the above snipshot, now if you press CTRL+ 0it will generate a GUID value for you in the same query window.
如上面的截图所示,现在如果你按下CTRL+0它会在同一个查询窗口中为你生成一个 GUID 值。
回答by Danny Varod
If you have the SQL Promptextension installed, you can create a new snippet like so...
如果你安装了SQL Prompt扩展,你可以像这样创建一个新的片段......
Snippet:guid
片段:guid
Description:new GUID
描述:new GUID
Code:
代码:
'$GUID$'
Now type guid
in your text editor and you'll get a new GUID surrounded by '
s.
现在guid
在您的文本编辑器中输入,您将获得一个由'
s包围的新 GUID 。