我在SQL Server 2008中有一个存储过程,我想将我选择的GUID存储到一个局部变量中以供进一步使用。我正在尝试它如下(并与其他几个变化),但我得到一个语法错误附近的'@myvars'…
DECLARE @myvars uniqueidentifier;
SELECT ID
INTO @myvars
FROM Device
WHERE DeviceID=@DeviceID
任何帮助都是感激的。谢谢!
try:
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)