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

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

问题描述

我有一个存储的proc,我想在MS SQL的表中插入一个GUID(用户ID),但是我不断收到有关guid值的连字符'-'的错误,这是我在下面定义的proc ;

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中执行了存储的proc时,如果将单引号引起来的话,它将运行良好.如何在没有存储程序问题的情况下处理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 exec

Update Here's the sql exec

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天全站免登陆