使用SQL存储过程将我的GUID选择为变量?

时间:2011-07-11 19:51:45

标签: sql-server-2008 stored-procedures select guid uniqueidentifier

我在SQL Server 2008中有一个存储过程,我希望将我选择的GUID存储到本地变量中以供进一步使用。我正在尝试如下(以及其他几个变体),但在'@myvars'附近收到语法错误...

DECLARE @myvars uniqueidentifier;
SELECT ID 
INTO @myvars
FROM Device
WHERE DeviceID=@DeviceID

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:9)

尝试:

DECLARE @myvars uniqueidentifier --declare the local variable to store value in

SELECT @myvars=ID                --assign's the ID column into the variable
FROM Device
WHERE DeviceID=@DeviceID

快速测试:

DECLARE @myvars uniqueidentifier --declare the local variable to store value in
declare @x table (id uniqueidentifier, valueOf varchar(4))

insert @x values (NEWID (),'aa')
insert @x values (NEWID (),'bb')
insert @x values (NEWID (),'cc')

SELECT @myvars=ID                --assign's the ID column into the variable
FROM @x
WHERE valueOf='bb'

SELECT @myvars
select * from @x

输出:

------------------------------------
36503FD9-A299-4DC5-A7BD-67605FF47ACE

(1 row(s) affected)

id                                   valueOf
------------------------------------ -------
175DCBF8-C418-4B5B-9270-66C12980D489 aa
36503FD9-A299-4DC5-A7BD-67605FF47ACE bb
E4F075FF-BA4B-4BB5-AD9F-ADB03FE88590 cc

(3 row(s) affected)