SET NOCOUNT ON
go

/* If upgrading and settings are below minimal settings, then reset them.
** This procedure must be run with server started single user.
** These are MINIMAL, not optimal settings.
** Any settings greater than these are not disturbed.
** The exit() and raiserror below are isql.exe constructs ONLY.   Cannot be run from isqlw, etc
*/

/* Cant be run if updates to system catalogs not allowed (i.e. not started single user.) */
if (select value from syscurconfigs where config = 102) <> 1
	raiserror('Cannot run chk_mins.sql unless updates to system tables are enabled.',20,127) with log

go

declare @cfgmem int, @numusers int, @opendb float,@locks float,@numdb int,
@openobj  float, @memneed  int, @anychange smallint, @allow_on smallint
select @numusers=value from master.dbo.sysconfigures where config=103
select @opendb=value from master.dbo.sysconfigures where config=105
select @openobj=value from master.dbo.sysconfigures where config=107
select @locks=value from master.dbo.sysconfigures where config=106
select @cfgmem= value from master.dbo.sysconfigures where config=104
select @allow_on = value from master.dbo.sysconfigures where config=102


if (@numusers < 10) 
	begin
	select @numusers=10,@anychange=1
	update sysconfigures set value = @numusers where config = 103
	end

/* Open databases mmust be at least 20 or the number of dbs on the system */
select @numdb=count(*) from sysdatabases
if (@numdb < 20) select @numdb=20

if ( @opendb < @numdb )
	begin
	select @opendb=@numdb,@anychange=1
	update sysconfigures set value = @opendb where config = 105
	end

if (@openobj < 500 )
	begin
	select @openobj=500,@anychange=1
	update sysconfigures set value = @openobj where config = 107
	end
	
if (@locks < 5000 )
	begin
	select @locks=5000,@anychange=1
	update sysconfigures set value = @locks where config = 106
	end
		
select @memneed =  2300 + (28*@numusers) + (.5*@opendb) + (.04*@openobj) + (.0156*@locks)

if @memneed < 2800 select @memneed = 2800
if @memneed > @cfgmem 
begin
	select @anychange=1
        update sysconfigures set value = @memneed where config = 104
end

if (@anychange = 1)
	begin
	/* 118 is an old entry.   If its there, delete it . */
	delete sysconfigures where config = 118

        /* reconfiguring resets curconfigs.   If its currently off
	** we need to set it on before reconfiguring, so it doesn't
	** change syscurconfigs, then switch back off after reconfiguring.
	** This proc is only run in single user mode, so the current
	** value should always be on.  Dont change the configured value.
	*/
	update sysconfigures set value=1 where config=102 and @allow_on = 0
	reconfigure with override

	update sysconfigures set value=0 where config=102 and @allow_on = 0
        end

exit(select isnull(@anychange,0) )
go




