aspnet_UsersInRoles_AddUsersToRoles cannot resolve collation conflict for equal to operation

Discussion in 'Databases' started by Angel, Jun 16, 2008.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello you clever people

    I have a problem.

    Locally its fine, but on my remote SQL server well, it's being awkward....just when I think my project is nearing its end, i am nearing the end of my tether.

    Unfortunatelythe followingsolutions re the collation don't work for me.

    My local database has no collation name in the settings and the remote one has the collation name: SQL_Latin1_General_CP1_CI_AS

    I tried this in the line:
    DECLARE @tbNames table(Name nvarchar(256) COLLATE DATABASE_DEFAULT NOT NULL PRIMARY KEY)

    and this one

    DECLARE @tbNames table(Name nvarchar(256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL PRIMARY KEY)

    All the other tables etc seem to be up there on the remote server.

    I copied them over and they were fine. I think I did all this a bit backwards so might be why as I am clearly no expert...otherwise I would not be here.

    Another fix was to run the the aspnet_regsql.exe again on the remote server using the cmd line...it all looked great until it failed with the same error message

    __________________________________________________


    EXEC sp_executesql @SqlToExec
    SQL Exception:
    System.Data.SqlClient.SqlException: Cannot resolve collation conflict for equal
    to operation.
    Cannot resolve collation conflict for equal to operation.
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolea
    n breakConnection)
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
    , Boolean breakConnection)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
    ect stateObj)
    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
    dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
    ParserStateObject stateObj)
    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName,
    Boolean async)
    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult res
    ult, String methodName, Boolean sendToPipe)
    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at System.Web.Management.SqlServices.ExecuteFile(String file, String server,
    String database, String dbFileName, SqlConnection connection, Boolean sessionSta
    te, Boolean isInstall, SessionStateType sessionStatetype)
    Help most gratefully accepted.
    ___________________________________________________________

    this is the stored proc:


    CREATE PROCEDURE dbo.aspnet_UsersInRoles_AddUsersToRoles
    @ApplicationName nvarchar(256),
    @UserNames nvarchar(4000),
    @RoleNames nvarchar(4000),
    @CurrentTimeUtc datetime
    AS
    BEGIN
    DECLARE @AppId uniqueidentifier
    SELECT @AppId = NULL
    SELECT @AppId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
    IF (@AppId IS NULL)
    RETURN(2)
    DECLARE @TranStarted bit
    SET @TranStarted = 0
    IF( @@TRANCOUNT = 0 )
    BEGIN
    BEGIN TRANSACTION
    SET @TranStarted = 1
    END
    DECLARE @tbNamestable(Name nvarchar(256) NOT NULL PRIMARY KEY)
    DECLARE @tbRolestable(RoleId uniqueidentifier NOT NULL PRIMARY KEY)
    DECLARE @tbUserstable(UserId uniqueidentifier NOT NULL PRIMARY KEY)
    DECLARE @Numint
    DECLARE @Posint
    DECLARE @NextPosint
    DECLARE @Namenvarchar(256)
    SET @Num = 0
    SET @Pos = 1
    WHILE(@Pos <= LEN(@RoleNames))
    BEGIN
    SELECT @NextPos = CHARINDEX(N',', @RoleNames, @Pos)
    IF (@NextPos = 0 OR @NextPos IS NULL)
    SELECT @NextPos = LEN(@RoleNames) + 1
    SELECT @Name = RTRIM(LTRIM(SUBSTRING(@RoleNames, @Pos, @NextPos - @Pos)))
    SELECT @Pos = @NextPos+1
    INSERT INTO @tbNames VALUES (@Name)
    SET @Num = @Num + 1
    END
    INSERT INTO @tbRoles
    SELECT RoleId
    FROM dbo.aspnet_Roles ar, @tbNames t
    WHERE LOWER(t.Name) = ar.LoweredRoleName AND ar.ApplicationId = @AppId
    IF (@@ROWCOUNT <> @Num)
    BEGIN
    SELECT TOP 1 Name
    FROM @tbNames
    WHERE LOWER(Name) NOT IN (SELECT ar.LoweredRoleName FROM dbo.aspnet_Roles ar, @tbRoles r WHERE r.RoleId = ar.RoleId)
    IF( @TranStarted = 1 )
    ROLLBACK TRANSACTION
    RETURN(2)
    END
    DELETE FROM @tbNames WHERE 1=1
    SET @Num = 0
    SET @Pos = 1
    WHILE(@Pos <= LEN(@UserNames))
    BEGIN
    SELECT @NextPos = CHARINDEX(N',', @UserNames, @Pos)
    IF (@NextPos = 0 OR @NextPos IS NULL)
    SELECT @NextPos = LEN(@UserNames) + 1
    SELECT @Name = RTRIM(LTRIM(SUBSTRING(@UserNames, @Pos, @NextPos - @Pos)))
    SELECT @Pos = @NextPos+1
    INSERT INTO @tbNames VALUES (@Name)
    SET @Num = @Num + 1
    END
    INSERT INTO @tbUsers
    SELECT UserId
    FROM dbo.aspnet_Users ar, @tbNames t
    WHERE LOWER(t.Name) = ar.LoweredUserName AND ar.ApplicationId = @AppId
    IF (@@ROWCOUNT <> @Num)
    BEGIN
    DELETE FROM @tbNames
    WHERE LOWER(Name) IN (SELECT LoweredUserName FROM dbo.aspnet_Users au, @tbUsers u WHERE au.UserId = u.UserId)
    INSERT dbo.aspnet_Users (ApplicationId, UserId, UserName, LoweredUserName, IsAnonymous, LastActivityDate)
    SELECT @AppId, NEWID(), Name, LOWER(Name), 0, @CurrentTimeUtc
    FROM @tbNames
    INSERT INTO @tbUsers
    SELECT UserId
    FROMdbo.aspnet_Users au, @tbNames t
    WHERE LOWER(t.Name) = au.LoweredUserName AND au.ApplicationId = @AppId
    END
    IF (EXISTS (SELECT * FROM dbo.aspnet_UsersInRoles ur, @tbUsers tu, @tbRoles tr WHERE tu.UserId = ur.UserId AND tr.RoleId = ur.RoleId))
    BEGIN
    SELECT TOP 1 UserName, RoleName
    FROM dbo.aspnet_UsersInRoles ur, @tbUsers tu, @tbRoles tr, aspnet_Users u, aspnet_Roles r
    WHEREu.UserId = tu.UserId AND r.RoleId = tr.RoleId AND tu.UserId = ur.UserId AND tr.RoleId = ur.RoleId
    IF( @TranStarted = 1 )
    ROLLBACK TRANSACTION
    RETURN(3)
    END
    INSERT INTO dbo.aspnet_UsersInRoles (UserId, RoleId)
    SELECT UserId, RoleId
    FROM @tbUsers, @tbRoles
    IF( @TranStarted = 1 )
    COMMIT TRANSACTION
    RETURN(0)
    END
    GO
    ___________________________________________________

    and this one

    ___________________________________________________

    CREATE PROCEDURE dbo.aspnet_UsersInRoles_RemoveUsersFromRoles
    @ApplicationName nvarchar(256),
    @UserNames nvarchar(4000),
    @RoleNames nvarchar(4000)
    AS
    BEGIN
    DECLARE @AppId uniqueidentifier
    SELECT @AppId = NULL
    SELECT @AppId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
    IF (@AppId IS NULL)
    RETURN(2)

    DECLARE @TranStarted bit
    SET @TranStarted = 0
    IF( @@TRANCOUNT = 0 )
    BEGIN
    BEGIN TRANSACTION
    SET @TranStarted = 1
    END
    DECLARE @tbNames table(Name nvarchar(256) NOT NULL PRIMARY KEY)
    DECLARE @tbRoles table(RoleId uniqueidentifier NOT NULL PRIMARY KEY)
    DECLARE @tbUsers table(UserId uniqueidentifier NOT NULL PRIMARY KEY)
    DECLARE @Num int
    DECLARE @Pos int
    DECLARE @NextPos int
    DECLARE @Name nvarchar(256)
    DECLARE @CountAll int
    DECLARE @CountU int
    DECLARE @CountR int

    SET @Num = 0
    SET @Pos = 1
    WHILE(@Pos <= LEN(@RoleNames))
    BEGIN
    SELECT @NextPos = CHARINDEX(N',', @RoleNames, @Pos)
    IF (@NextPos = 0 OR @NextPos IS NULL)
    SELECT @NextPos = LEN(@RoleNames) + 1
    SELECT @Name = RTRIM(LTRIM(SUBSTRING(@RoleNames, @Pos, @NextPos - @Pos)))
    SELECT @Pos = @NextPos+1
    INSERT INTO @tbNames VALUES (@Name)
    SET @Num = @Num + 1
    END
    INSERT INTO @tbRoles
    SELECT RoleId
    FROM dbo.aspnet_Roles ar, @tbNames t
    WHERE LOWER(t.Name) = ar.LoweredRoleName AND ar.ApplicationId = @AppId
    SELECT @CountR = @@ROWCOUNT
    IF (@CountR <> @Num)
    BEGIN
    SELECT TOP 1 N'', Name
    FROM @tbNames
    WHERE LOWER(Name) NOT IN (SELECT ar.LoweredRoleName FROM dbo.aspnet_Roles ar, @tbRoles r WHERE r.RoleId = ar.RoleId)
    IF( @TranStarted = 1 )
    ROLLBACK TRANSACTION
    RETURN(2)
    END

    DELETE FROM @tbNames WHERE 1=1
    SET @Num = 0
    SET @Pos = 1

    WHILE(@Pos <= LEN(@UserNames))
    BEGIN
    SELECT @NextPos = CHARINDEX(N',', @UserNames, @Pos)
    IF (@NextPos = 0 OR @NextPos IS NULL)
    SELECT @NextPos = LEN(@UserNames) + 1
    SELECT @Name = RTRIM(LTRIM(SUBSTRING(@UserNames, @Pos, @NextPos - @Pos)))
    SELECT @Pos = @NextPos+1
    INSERT INTO @tbNames VALUES (@Name)
    SET @Num = @Num + 1
    END
    INSERT INTO @tbUsers
    SELECT UserId
    FROM dbo.aspnet_Users ar, @tbNames t
    WHERE LOWER(t.Name) = ar.LoweredUserName AND ar.ApplicationId = @AppId
    SELECT @CountU = @@ROWCOUNT
    IF (@CountU <> @Num)
    BEGIN
    SELECT TOP 1 Name, N''
    FROM @tbNames
    WHERE LOWER(Name) NOT IN (SELECT au.LoweredUserName FROM dbo.aspnet_Users au, @tbUsers u WHERE u.UserId = au.UserId)
    IF( @TranStarted = 1 )
    ROLLBACK TRANSACTION
    RETURN(1)
    END
    SELECT @CountAll = COUNT(*)
    FROMdbo.aspnet_UsersInRoles ur, @tbUsers u, @tbRoles r
    WHERE ur.UserId = u.UserId AND ur.RoleId = r.RoleId
    IF (@CountAll <> @CountU * @CountR)
    BEGIN
    SELECT TOP 1 UserName, RoleName
    FROM @tbUsers tu, @tbRoles tr, dbo.aspnet_Users u, dbo.aspnet_Roles r
    WHERE u.UserId = tu.UserId AND r.RoleId = tr.RoleId AND
    tu.UserId NOT IN (SELECT ur.UserId FROM dbo.aspnet_UsersInRoles ur WHERE ur.RoleId = tr.RoleId) AND
    tr.RoleId NOT IN (SELECT ur.RoleId FROM dbo.aspnet_UsersInRoles ur WHERE ur.UserId = tu.UserId)
    IF( @TranStarted = 1 )
    ROLLBACK TRANSACTION
    RETURN(3)
    END
    DELETE FROM dbo.aspnet_UsersInRoles
    WHERE UserId IN (SELECT UserId FROM @tbUsers)
    AND RoleId IN (SELECT RoleId FROM @tbRoles)
    IF( @TranStarted = 1 )
    COMMIT TRANSACTION
    RETURN(0)
    END
    GO
    _________________________________________________________
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page