Question

I'm converting table ID from int to UNIQUEIDENTIFIER. I used to check the valid user using:

CREATE PROCEDURE [dbo].[UCP_ValidateUser]
   @UserName nvarchar(32),
   @isUser int Output
AS
Begin
   SELECT  
       @isUser = UserID  
   FROM 
       UCP_Users
   WHERE 
       UserName = @UserName

   IF @@RowCount < 1
      SELECT @isUser = 0
End
GO

but I'm getting error

int is incompatible with uniqueidentifier

Now tried to convert to:

CREATE PROCEDURE [dbo].[UCP_ValidateUser]
   @UserName nvarchar(32),
   @isUser UNIQUEIDENTIFIER Output
AS
Begin
   SELECT  
      @isUser = UserID  
   FROM 
      UCP_Users
   WHERE 
      UserName = @UserName

   if @@RowCount < 1
      SELECT @isUser = 0
End
GO

and still getting the error because of the value 0

I use it in my ASP.NET code as follows:

If isUser > 0 Then
    Return True
Else
    Return False
End If

My questions are:

  1. How to get this stored procedure to work?

  2. Should I keep or better to keep usual table identity ID and add another for GUID with unique identifier?

Was it helpful?

Solution 2

The Problem is When you try to assign @isUser = 0 , A uniqueidentifier cannot be set to Zero, But you can do something like this....

CREATE PROCEDURE [dbo].[UCP_ValidateUser]
@UserName nvarchar(32),
@isUser UNIQUEIDENTIFIER Output
AS
Begin
 SET NOCOUNT ON;

SELECT TOP 1 @isUser = UserID  
From UCP_Users
Where UserName=@UserName

if (@@RowCount <> 1)
 BEGIN
   SET @isUser = NULL
 END

End
GO

And once you have assigned NULL Value to the @isUser variable then you can do further checks and populate a variable or return a value. doing something like....

IF ( @isUser IS NULL)
 BEGIN
   /* Do something here */
 END
ELSE
 BEGIN
  /* Else do something */
 END

OTHER TIPS

Use an empty GUID instead of the zero:

SELECT  @isUser = '00000000-0000-0000-0000-000000000000'

In the ASP.NET code you compare the value to the empty GUID value:

If isUser <> GUID.Empty Then
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top