Inserting GUID into SQL Server

前端 未结 4 1355
既然无缘
既然无缘 2021-01-01 19:32

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

相关标签:
4条回答
  • 2021-01-01 19:55

    Try using this:

    sql_cmd.Parameters.AddWithValue
    ("@Company_ID",**Guid.Parse**("00000000-0000-0000-0000-000000000000");
    

    That will take a simple string rep. and turn it into a GUID obj.

    0 讨论(0)
  • 2021-01-01 19:58

    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
    
    0 讨论(0)
  • 2021-01-01 20:15

    You simply need to QUOTE your GUID:

    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
    

    Marc

    0 讨论(0)
  • 2021-01-01 20:15

    You need single quotes around your GUID....it's just a string to sql server.

    You could try letting the sp generate the GUID with the sql function newid() ...sql server only i think.

    And if you're pulling the GUID from another table, let the SP go get that GUID from that table.

    0 讨论(0)
提交回复
热议问题