How could I add some items to the tempdb anytime SQL Server starts up?
I’m no expert at this, but our ASP SessionState is stored in the DB and for some reason the tempdb items used for the session state get dropped anytime the server restarts. Not only do I need to recreate the items, but I also have to recreate the User mappings to tempdb. I have a script that does it, but I can’t figure out how to run it on SQL startup
-- Use TempDB
use tempdb
go
-- Create Temp tables if they don't exist
IF NOT EXISTS(
SELECT 1 FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME = 'ASPStateTempSessions')
BEGIN
EXECUTE [ASPState].[dbo].[CreateTempTables]
END
-- If ASPSessionState user isn't mapped to temp db, map it
IF IS_MEMBER('ASPSessionState') IS NULL
create user ASPSessionState from login ASPSessionState
-- Give ASPSessionState user read/write permissions to tempdb
exec sp_addrolemember db_datareader, ASPSessionState
go
exec sp_addrolemember db_datawriter , ASPSessionState
go
Um, if you’ve used the standard settings to enable ASP.Net session state in tempdb, the system should have generated a stored proc (
ASPState_Startup) as follows in the master database. This stored proc is configured to run automatically on SQL Server startup:So, the temp tables should be being recreated anyway, unless something has been altered since installing.
If additional permissions are required, I’d look to extending the existing
CreateTempTablesprocedure inASPState.If this isn’t working correctly, you might try using the
aspnet_regsqlcommand (found under%Windir%\Microsoft.Net\Framework\<framework version– to remove then re-add session state support to the server. You’d want to use-ssremovethen-ssadd, but I’d suggest passing/?first to see all of the applicable options.