title: Inserting GUID (uniqueidentifier) value in stored procedure
body: <p>I have this stored procedure which stores information in several tables and one of the key variables is that I can feed the procedure with a guid value to bind these tables.</p>
  
  <p>It goes something like</p>
  
  <pre><code>USE [MyDatabase]
  GO
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  ALTER PROCEDURE [dbo].[my_createCompany]
      @CompanyName nvarchar(255),
      @CompanyDescription nvarchar(255),
      @ParentGuid uniqueidentifier
  AS
      BEGIN
  
          DECLARE @CompanyGuid uniqueidentifier
          SET @CompanyGuid = NEWID()
  
          SET NOCOUNT ON
      Insert into [dbo].[tblPROCompany]
      (
          [CompanyGuid],
          [CompanyName],
          [CompanyDescription],
          [ParentGuid]
      )
      VALUES
      (
          @CompanyGuid,
          @CompanyName,
          @CompanyDescription,
          @ParentGuid
      )
  END
  </code></pre>
  
  <p>It looks right, but when assigning a GUID to the variable @ParentGuid the procedure fails. When I look at the output it something like this</p>
  
  <pre><code>USE [MyDatabase]
  GO    
  DECLARE @return_value int    
  EXEC    @return_value = [dbo].[my_createCompany]
          @CompanyName = N'Asd',
          @CompanyDescription = NULL,
          @ParentGuid = 4864DE55-60FB-4A69-814F-428B0178F4BB
  
  SELECT  'Return Value' = @return_value
  
  GO
  </code></pre>
  
  <p>And the error of course
      Msg 102, Level 15, State 1, Line 7
      Incorrect syntax near 'DE55'.</p>
  
  <p>So it doesn't encapsulate the @ParentGuid as it should. For now I've made a workaround declaring the @ParentGuid as a varchar</p>
  
  <pre><code>@ParentGuid varchar(37), --uniqueidentifier
  </code></pre>
  
  <p>and the converting it to what the database wants before the insert</p>
  
  <pre><code>if @ParentGuid is not null
  BEGIN
      DECLARE @ParentGuidConverted uniqueidentifier
      SET @ParentGuidConverted = convert(uniqueidentifier, @ParentGuid)
  END
  </code></pre>
  
  <p>Is there a better way of doing this?</p>
  
  <p>Thanks</p>