text: ﻿
  CREATE PROCEDURE [dbo].aspnet_Applications_CreateApplication
      @ApplicationName      nvarchar(256),
      @ApplicationId        uniqueidentifier OUTPUT
  AS
  BEGIN
      SELECT  @ApplicationId = ApplicationId FROM dbo.aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
  
      IF(@ApplicationId IS NULL)
      BEGIN
          DECLARE @TranStarted   bit
          SET @TranStarted = 0
  
          IF( @@TRANCOUNT = 0 )
          BEGIN
  	        BEGIN TRANSACTION
  	        SET @TranStarted = 1
          END
          ELSE
      	    SET @TranStarted = 0
  
          SELECT  @ApplicationId = ApplicationId
          FROM dbo.aspnet_Applications WITH (UPDLOCK, HOLDLOCK)
          WHERE LOWER(@ApplicationName) = LoweredApplicationName
  
          IF(@ApplicationId IS NULL)
          BEGIN
              SELECT  @ApplicationId = NEWID()
              INSERT  dbo.aspnet_Applications (ApplicationId, ApplicationName, LoweredApplicationName)
              VALUES  (@ApplicationId, @ApplicationName, LOWER(@ApplicationName))
          END
  
  
          IF( @TranStarted = 1 )
          BEGIN
              IF(@@ERROR = 0)
              BEGIN
  	        SET @TranStarted = 0
  	        COMMIT TRANSACTION
              END
              ELSE
              BEGIN
                  SET @TranStarted = 0
                  ROLLBACK TRANSACTION
              END
          END
      END
  END
  