将 GUID 插入 SQL Server [英] Inserting GUID into SQL Server

查看:14
本文介绍了将 GUID 插入 SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,如果我想将 GUID(用户 ID)插入 MS SQL 中的表中,但我不断收到关于作为 guid 值一部分的连字符-"的错误,这是我在下面定义的过程;

I have a stored proc were I want to insert a GUID (user id) into a table in MS SQL but I keep getting an error about the hyphen '-' that is part of the guid value, here's my proc defined below;

@userID uniqueidentifier,
@bookID int,
@dateReserved datetime,
@status bit

INSERT INTO Reservation(BookId, DateReserved, [Status], UserId)
VALUES (@bookID, @dateReserved, @status, @userID)

但是,如果存储过程在 Management Studio 中执行,则当我在值周围加上单引号时,它运行良好.如何处理 guid 插入而不会出现存储过程中的问题?

But when I put single quotes around the value if the stored proc is executed in Management Studio, it runs fine. How can I handle the guid insertion without problems from my stored proc?

谢谢大家.

更新这是 sql 执行

DECLARE @return_value int

EXEC    @return_value = [dbo].[usp_ReserveBook]
    @userID = AE019609-99E0-4EF5-85BB-AD90DC302E70,
    @bookID = 7,
    @dateReserved = N'09/03/2009',
    @status = 1

SELECT  'Return Value' = @return_value

这是错误信息

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '-'.

推荐答案

只需从 varchar 中强制转换即可.

Just cast it from a varchar.

DECLARE @return_value int

EXEC    @return_value = [dbo].[usp_ReserveBook]
        @userID = CONVERT(uniqueidentifier, 'AE019609-99E0-4EF5-85BB-AD90DC302E70'),
        @bookID = 7,
        @dateReserved = N'09/03/2009',
        @status = 1

SELECT  'Return Value' = @return_value

这篇关于将 GUID 插入 SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆