1

当我试图执行这个存储过程时,我面临一个错误 int is int is incompatable with uniqueidentifier.my userid coloumn is bigint

使用 [iFlipOut_Dev] 去 / *对象:StoredProcedure [dbo].[SaveRegistration] 脚本日期:2013 年 4 月 14 日下午 3:29:40* / SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO

ALTER PROCEDURE [dbo].[SaveRegistration]  
(
   @p_UserId int,
   @p_FirstName nvarchar(150),
   @p_LastName nvarchar(50),
   @p_Gender nvarchar(10),
   @p_Email nvarchar(150),
   @p_Mobile nvarchar(12),
   @p_Phone nvarchar(12),
   @p_DateOfBirth nvarchar(50),
   @p_Address nvarchar(250),
   @p_ZipCode nvarchar(10),
   @p_City nvarchar(100),
   @p_StateId smallint,
   @p_LoginId int,
   @p_UserStatusId smallint,
   @p_LanguageId smallint,
   @p_ModifiedBy bigint,
   @p_CountryId smallint,
   @p_UserImage nvarchar(20),
   @p_publicRegistrationId nvarchar(50),
   @p_TimeZone_Id smallint

   )
AS 
   BEGIN

      SET  NOCOUNT  ON


      IF 
         CASE 
            WHEN EXISTS 
               (
                  SELECT 
                     UD.UserId, 
                     UD.FirstName, 
                     UD.LastName, 
                     UD.Gender, 
                     UD.email, 
                     UD.Mobile, 
                     UD.phone, 
                     UD.DateOfBirth, 
                     UD.Address, 
                     UD.ZipCode, 
                     UD.City, 
                     UD.StateId, 
                     UD.LoginId, 
                     UD.UserStatusId, 
                     UD.LanguageId, 
                     UD.ModifiedBy, 
                     UD.ModifiedDate, 
                     UD.CountryId, 
                     UD.UserImage, 
                     UD.PublicRegistrationId, 
                     UD.TimeZone_Id,
                     UD.IsActivated
                  FROM dbo.UserDetails as UD
                  WHERE UD.UserId = @p_UserId
               ) THEN 1
            ELSE 0
         END <> 0
         /*
         *  Added UserId Coloumn to be inserted in the table
         */

         UPDATE dbo.UserDetails
            SET 
               FirstName = @p_FirstName, 
               LastName = @p_LastName, 
               Gender = @p_Gender, 
               Email = @p_Email, 
               Mobile = @p_Mobile, 
               Phone = @p_Phone, 
               DateOfBirth = @p_DateOfBirth, 
               Address = @p_Address, 
               ZipCode = @p_ZipCode, 
               City = @p_City, 
               StateId = @p_StateId, 
               LoginId = @p_LoginId, 
               UserStatusId = @p_UserStatusId, 
               LanguageId = @p_LanguageId, 
               ModifiedBy = @p_ModifiedBy, 
               ModifiedDate = isnull(getdate(), getdate()), 
               CountryId = @p_CountryId, 
               UserImage = @p_UserImage, 
               PublicRegistrationId=@p_publicregistrationId,
               TimeZone_Id = @p_TimeZone_Id

         WHERE UserDetails.UserId = @p_UserId
      ELSE 
         BEGIN

            /*
            *  added userid coloumn in the insert statement
            */

            INSERT dbo.UserDetails(
               dbo.UserDetails.FirstName, 
               dbo.UserDetails.LastName, 
               dbo.UserDetails.Gender, 
               dbo.UserDetails.email, 
               dbo.UserDetails.Mobile, 
               dbo.UserDetails.Phone, 
               dbo.UserDetails.DateOfBirth, 
               dbo.UserDetails.Address, 
               dbo.UserDetails.ZipCode, 
               dbo.UserDetails.City, 
               dbo.UserDetails.StateId, 
               dbo.UserDetails.LoginId, 
               dbo.UserDetails.UserStatusId, 
               dbo.UserDetails.LanguageId, 
               dbo.UserDetails.ModifiedBy, 
               dbo.UserDetails.ModifiedDate, 
               dbo.UserDetails.CountryId, 
               dbo.UserDetails.UserImage, 
               dbo.UserDetails.PublicRegistrationId, 
               dbo.UserDetails.TimeZone_Id)


               VALUES (
                  @p_FirstName, 
                  @p_LastName, 
                  @p_Gender, 
                  @p_Email, 
                  @p_Mobile, 
                  @p_Phone, 
                  @p_DateOfBirth, 
                  @p_Address, 
                  @p_ZipCode, 
                  @p_City, 
                  @p_StateId, 
                  @p_LoginId, 
                  @p_UserStatusId, 
                  @p_LanguageId, 
                  @p_ModifiedBy, 
                  isnull(getdate(), getdate()), 
                  @p_CountryId, 
                  @p_UserImage, 
                  newid(), 
                  @p_TimeZone_Id)





            SET @p_UserId = scope_identity()

         END

      SELECT @p_UserId AS UserId

   END
4

1 回答 1

1

是的。Int 和 GUID 数据类型不兼容。您能否指定发生错误的确切位置。

我也很少重写你的 sp -

ALTER PROCEDURE [dbo].[SaveRegistration]  
(
   @p_UserId int,
   @p_FirstName nvarchar(150),
   @p_LastName nvarchar(50),
   @p_Gender nvarchar(10),
   @p_Email nvarchar(150),
   @p_Mobile nvarchar(12),
   @p_Phone nvarchar(12),
   @p_DateOfBirth nvarchar(50),
   @p_Address nvarchar(250),
   @p_ZipCode nvarchar(10),
   @p_City nvarchar(100),
   @p_StateId smallint,
   @p_LoginId int,
   @p_UserStatusId smallint,
   @p_LanguageId smallint,
   @p_ModifiedBy bigint,
   @p_CountryId smallint,
   @p_UserImage nvarchar(20),
   @p_publicRegistrationId nvarchar(50),
   @p_TimeZone_Id smallint

   )
AS BEGIN

      SET  NOCOUNT  ON

      IF EXISTS (
            SELECT 1
            FROM dbo.UserDetails UD
            WHERE UD.UserId = @p_UserId
        ) BEGIN


         UPDATE dbo.UserDetails
            SET 
               FirstName = @p_FirstName, 
               LastName = @p_LastName, 
               Gender = @p_Gender, 
               Email = @p_Email, 
               Mobile = @p_Mobile, 
               Phone = @p_Phone, 
               DateOfBirth = @p_DateOfBirth, 
               Address = @p_Address, 
               ZipCode = @p_ZipCode, 
               City = @p_City, 
               StateId = @p_StateId, 
               LoginId = @p_LoginId, 
               UserStatusId = @p_UserStatusId, 
               LanguageId = @p_LanguageId, 
               ModifiedBy = @p_ModifiedBy, 
               ModifiedDate = getdate(), 
               CountryId = @p_CountryId, 
               UserImage = @p_UserImage, 
               PublicRegistrationId=@p_publicregistrationId,
               TimeZone_Id = @p_TimeZone_Id
         WHERE UserDetails.UserId = @p_UserId

      END
      ELSE BEGIN

            INSERT dbo.UserDetails(
               FirstName, 
               LastName, 
               Gender, 
               email, 
               Mobile, 
               Phone, 
               DateOfBirth, 
               Address, 
               ZipCode, 
               City, 
               StateId, 
               LoginId, 
               UserStatusId, 
               LanguageId, 
               ModifiedBy, 
               ModifiedDate, 
               CountryId, 
               UserImage, 
               PublicRegistrationId, 
               TimeZone_Id)
            SELECT
                  @p_FirstName, 
                  @p_LastName, 
                  @p_Gender, 
                  @p_Email, 
                  @p_Mobile, 
                  @p_Phone, 
                  @p_DateOfBirth, 
                  @p_Address, 
                  @p_ZipCode, 
                  @p_City, 
                  @p_StateId, 
                  @p_LoginId, 
                  @p_UserStatusId, 
                  @p_LanguageId, 
                  @p_ModifiedBy, 
                  getdate(), 
                  @p_CountryId, 
                  @p_UserImage, 
                  newid(), 
                  @p_TimeZone_Id

            SELECT @p_UserId = scope_identity()

         END

      SELECT @p_UserId AS UserId

   END

清理意味着:

IF 
         CASE 
            WHEN EXISTS 
               (
                  SELECT 
                     UD.UserId, 
                     UD.FirstName, 
                     UD.LastName, 
                     UD.Gender, 
                     UD.email, 
                     UD.Mobile, 
                     UD.phone, 
                     UD.DateOfBirth, 
                     UD.Address, 
                     UD.ZipCode, 
                     UD.City, 
                     UD.StateId, 
                     UD.LoginId, 
                     UD.UserStatusId, 
                     UD.LanguageId, 
                     UD.ModifiedBy, 
                     UD.ModifiedDate, 
                     UD.CountryId, 
                     UD.UserImage, 
                     UD.PublicRegistrationId, 
                     UD.TimeZone_Id,
                     UD.IsActivated
                  FROM dbo.UserDetails as UD
                  WHERE UD.UserId = @p_UserId
               ) THEN 1
            ELSE 0
         END <> 0

可以优化到:

 IF EXISTS (
            SELECT 1
            FROM dbo.UserDetails UD
            WHERE UD.UserId = @p_UserId
        ) BEGIN
于 2013-04-15T09:21:45.443 回答