简体   繁体   English

错误:将SQL表ID转换为GUID时,int与uniqueidentifier不兼容

[英]Error: int is incompatible with uniqueidentifier when converted SQL table ID to GUID

I'm converting table ID from int to UNIQUEIDENTIFIER. 我正在将表ID从int转换为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 int与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 并且由于值0仍然得到错误

I use it in my ASP.NET code as follows: 我在我的ASP.NET代码中使用它如下:

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? 我应该保留还是更好地保留通常的表标识ID,并为具有唯一标识符的GUID添加另一个标识?

Use an empty GUID instead of the zero: 使用空GUID而不是零:

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

In the ASP.NET code you compare the value to the empty GUID value: 在ASP.NET代码中,您将值与空GUID值进行比较:

If isUser <> GUID.Empty Then

The Problem is When you try to assign @isUser = 0 , A uniqueidentifier cannot be set to Zero, But you can do something like this.... 问题是当你尝试分配@isUser = 0时,一个uniqueidentifier不能设置为Zero,但你可以这样做......

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. 一旦为@isUser变量分配了NULL值,就可以进行进一步的检查并填充变量或返回一个值。 doing something like.... 做某事......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM