Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I pass in a uniqueidentifier/GUID to a stored procedure

I have this SP

USE [TestDB]
GO
/****** Object:  StoredProcedure [dbo].[sp_test]    Script Date: 06/12/2010 11:47:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[sp_test]  
    @id uniqueidentifier
AS
BEGIN

    select * from TestTbl where ProductId= @id

END

I then went to the SP with ms sql 2005 and clicked execute. It comes up with a box where I entered in the GUID. I copied and pasted it straight from my test database.

I get this error.

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

So why can't I sent in GUIDs? even ones that are copied right from the database and must be valid as they where allowed into the db.

like image 263
chobo2 Avatar asked Jun 12 '10 18:06

chobo2


People also ask

Can GUID be used as primary key?

GUIDs can be considered as global primary keys. Local primary keys are used to uniquely identify records within a table. On the other hand, GUIDs can be used to uniquely identify records across tables, databases, and servers.

What is the datatype for GUID?

The GUID data type is a 16 byte binary data type. This data type is used for the global identification of objects, programs, records, and so on. The important property of a GUID is that each value is globally unique.

What is the Uniqueidentifier datatype in SQL?

Uniqueidentifier is a Microsoft SQL Server data type that is used to store Globally Unique Identifiers (GUIDs). It can store 16 bytes of data. The Developer tool treats the Uniqueidentifier data type as String.

How is GUID stored?

The GUID is stored as byte-array, that is to say binary(16). You could also store it as UInt128, or two bigints.


1 Answers

Two hints:

  • first of all, do not call your stored procedures sp_(something) - Microsoft specifically warns against that

We recommend that you do not create any stored procedures using sp_ as a prefix. SQL Server uses the sp_ prefix to designate system stored procedures. The name you choose may conflict with some future system procedure.

  • secondly: I have no trouble calling your stored proc like this:

    EXEC proc_test 'B551F2C8-8380-491B-A51F-436E51CDD08F'
    

How are you calling your stored proc?? Show us!

like image 137
marc_s Avatar answered Sep 24 '22 04:09

marc_s