开发者

SELECT my GUID into a variable using SQL Stored Procedures?

开发者 https://www.devze.com 2023-03-19 06:29 出处:网络
I have a stored procedure in SQL Server 2008 that I want to store a GUID that I Select into a local variable for further use.I am trying it as below (and with several other variations), but am getting

I have a stored procedure in SQL Server 2008 that I want to store a GUID that I Select into a local variable for further use. I am trying it as below (and with several other variations), but am getting a syntax error near '@myvars'...

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

Any help is appreciated. Than开发者_运维百科ks!


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

quick test:

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

OUTPUT:

------------------------------------
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)
0

精彩评论

暂无评论...
验证码 换一张
取 消