Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Database Primary Key Max Entry Fix

Posted on February 28, 2013October 26, 2015 By David Kittell

Two options with this fix, official script from Ektron or to fix the individual tables that are currently having the issue.

CREATE PROCEDURE [dbo].[fixmaxentries]
AS
BEGIN
	UPDATE max_entries
	SET max_table_number = (
			SELECT MAX(PreferenceId) + 2
			FROM notification_preference
			)
	WHERE table_name = 'notification_preference';

	UPDATE max_entries
	SET max_table_number = (
			SELECT MAX(PreferenceId) + 2
			FROM notification_preference_default
			)
	WHERE table_name = 'notification_preference_default';
END
exec fixmaxentries

Individual Table(s)

Update
	max_entries
SET
	max_table_number = (
		SELECT
			MAX(PreferenceId)
		FROM
			notification_preference
	)
WHERE
	table_name = 'notification_preference'

Update
	max_entries
SET
	max_table_number = (
		SELECT
			MAX(PreferenceId)
		FROM
			notification_preference_default
	)
WHERE
	table_name = 'notification_preference_default'

SELECT TOP 1000 [table_name]
      ,[max_table_number]
  FROM [max_entries]

Official fix from Ektron, specific to 8.0.2

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_fixmaxentries]') AND type in (N'P', N'PC'))
drop procedure [dbo].[cms_fixmaxentries]
go
create procedure [dbo].[cms_fixmaxentries]
as
begin
	begin try
		begin tran;
		declare @serverindex bigint
		select @serverindex =ISNULL(server_index,1) from settings
		if(@serverindex >0)
		begin
			declare @tbl table(tablename nvarchar(255) not null primary key clustered,columnname nvarchar(100))
			insert into @tbl(tablename,columnname )
			select t.table_name ,column_name  from (select  ku.table_name,ku.ordinal_position,column_name   from information_schema.table_constraints as tc   inner join   information_schema.key_column_usage as ku  on tc.constraint_type = 'primary key' and tc.constraint_name = ku.constraint_name
			where ku.TABLE_NAME not in ('dynamic_data_tbl','UrlAliasAuto_tbl','UrlAliasManual_tbl','UrlAliasRegEx_tbl','user_to_group_tbl','folder_to_template_tbl','content_edit','form_data_tbl','max_entries') and  column_name not in ('ref_id','ref_type','enum_type','pref_name','flag_def_lang') and column_name not like '%language' and column_name not like '%language_id'    )t  join max_entries m on (t.table_name=m.table_name)
			order by table_name, ordinal_position
			insert into @tbl(tablename,columnname ) values('dynamic_data_tbl','id')
			insert into @tbl(tablename,columnname ) values('form_data_tbl','form_data_id')
			insert into @tbl(tablename,columnname ) values('UrlAliasAuto_tbl','AutoId')
			insert into @tbl(tablename,columnname ) values('UrlAliasManual_tbl','AliasId')
			insert into @tbl(tablename,columnname ) values('UrlAliasRegEx_tbl','RegExId')
			declare @startvalue bigint,@endvalue bigint,@tname nvarchar(100),@identifier nvarchar(100),@currtbl nvarchar(100),@tablemax bigint ,@newvalue bigint,@sql nvarchar(4000)
			set @startvalue=(@serverindex-1)*2147483647+1
			set @endvalue=(@serverindex)*2147483647
			set @tname='';
			set @newvalue=@startvalue ;
			while(1=1)
			begin
			select top 1 @tname=tablename,@identifier =columnname from @tbl where tablename >@tname order by tablename asc
			if(@@rowcount>0)
			begin
				if(@tname='users')
					set @sql='select @tablemax=max('+@identifier+') from '+@tname+' where user_id<>18611864 and user_id<>999999999 and '+@identifier+'>'+convert(varchar,@startvalue)+' and '+@identifier+'<'+convert(varchar,@endvalue)
				else if (@tname='usergroups')
					set @sql='select @tablemax=max('+@identifier+') from '+@tname+' where usergroup_id<>888888 and '+@identifier+'>'+convert(varchar,@startvalue)+' and '+@identifier+'<'+convert(varchar,@endvalue)
				else
					set @sql='select @tablemax=max('+@identifier+') from '+@tname+' where '+@identifier+'>'+convert(varchar,@startvalue)+' and '+@identifier+'<'+convert(varchar,@endvalue)
				exec sp_executesql @sql ,N'@tablemax bigint out', @tablemax  out
				if(@tablemax>@newvalue ) set @newvalue =@tablemax
			end
			else break;
			end
			if(@newvalue>@startvalue) update max_entries set max_table_number=@newvalue+5
			begin
				update max_entries set max_table_number=@startvalue where max_table_number<@startvalue
			end
		end
		commit tran;
	end try
	begin catch
		if(@@trancount>0) rollback tran;
		print @sql
		declare @msg nvarchar(2000)
		set @msg=ERROR_MESSAGE()
		raiserror(@msg, 16, 1)
	end catch
	return
end
go
exec [cms_fixmaxentries]
go
Originally Posted on February 28, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

C# Ektron Code Ektron MSSQL SQL

Post navigation

Previous post
Next post

Related Posts

Token String

Posted on July 8, 2014October 26, 2015

This PHP function will assist in splitting a string based on characters. $string = "This istan examplenstring"; /* Use tab and newline as tokenizing characters as well */ $tok = strtok($string, " nt"); while ($tok !== false) { echo "Word=$tok<br />"; $tok = strtok(" nt"); } $first_token = strtok(‘/something’, ‘/’);…

Read More

Ektron Current Version

Posted on October 23, 2013October 26, 2015

This script will display the current Ektron version. Works In: 8.0.2, 8.5, and 8.7 SELECT convert(VARCHAR, version) AS "Current Ektron Version" FROM settings Source: http://www.skonet.com/Articles_Archive/Helpful_Sql_Scripts_for_Ektron_CMS_400_Net.aspx Originally Posted on October 23, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any…

Read More

Bash – Mass NSlookup

Posted on July 19, 2018

When you are moving from one server public IP to another you could use some help knowing things are changed. This bash script will do an NSLookup on various DNS servers. #!/bin/sh # Digkittell.net.sh # # # Created by David Kittell on 1/6/18. # netDomain="kittell.net" dnsServerList="68.94.156.132|10.40.20.1|8.8.8.8|192.168.10.21|192.168.10.20|10.40.20.6|208.67.220.220|9.9.9.9|209.18.47.61|4.2.2.2|168.63.129.16|172.26.38.1" clear echo "AT&T DNS…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes