if col_length('dbo.SMSLog','PK_SMSLog') is null
BEGIN
	
-- Create New Field
	ALTER TABLE dbo.SMSLog ADD PK_SMSLog INT NOT NULL DEFAULT 0;
-- End Create New Field
END

	GO

if NOT EXISTS (SELECT * FROM IdentityFields WHERE DBTableName = 'TEXTMESSAGEIDENTITY')
BEGIN
-- Fill PK_SMSlog with data	
	DECLARE @Key as INT
	DECLARE @Dummy as INT
	SET @Key = 0
	
	DECLARE cur CURSOR FOR SELECT PK_SMSLog FROM dbo.SMSLog FOR UPDATE;

	OPEN cur
	FETCH NEXT FROM cur INTO @Dummy
	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @Key = @Key + 1
		UPDATE dbo.SMSLog 
		SET PK_SMSLog = @Key 
		WHERE CURRENT OF cur
		
		FETCH NEXT FROM cur INTO @Dummy
	END;

	CLOSE cur;
	DEALLOCATE cur;
	
--End Fill PK_SMSlog with data

--Creating primary key
	IF NOT EXISTS (SELECT id FROM SysIndexes (NOLOCK) WHERE SysIndexes.name = 'PK_SMSLog')
	BEGIN
	  ALTER TABLE dbo.SMSLog 
	  ADD CONSTRAINT PK_SMSLog PRIMARY KEY CLUSTERED (PK_SMSLog);
	END;
	
--End Creating primary key
	
--Inserting new key in IdentityFields
	INSERT INTO dbo.IdentityFields(DBTableName, HotelCustomerNumber, IdentityField, FKDepartment, ChangeDate)
	VALUES ('TEXTMESSAGEIDENTITY',-1,@Key+1,-1,GETDATE());
--End Inserting new key in IdentityFields
	

END;

GO