MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Managing SQL Server Database and Application Metadata - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

Managing SQL Server Database and Application Metadata

Last post 08-27-2008 8:18 AM by jubdavis. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 08-19-2008 12:30 AM

    Managing SQL Server Database and Application Metadata

    This post is related to this tip: Managing SQL Server Database and Application Metadata

    http://www.mssqltips.com/tip.asp?tip=1565

  • 08-20-2008 12:19 AM In reply to

    • ALZDBA
    • Top 50 Contributor
    • Joined on 07-19-2008
    • Posts 6

    Re: Managing SQL Server Database and Application Metadata

    Are you also challenging your metadata to the actual usage ?

    (by sampling sysprocesses (sql2000) or using a login trigger on sql2005/2008)

    I'm using this proc on sql2000 for license checking and connection tracking:

    /* ALZDBA dd 20080715
      Inventory active connections for License checking.
    */
    use master
    go
    if not object_id('dbo.sp_DBA_ConnectionTracker') is null
    begin
       drop procedure dbo.sp_DBA_ConnectionTracker
    end
    go
    Create procedure dbo.sp_DBA_ConnectionTracker
    as
    begin
    /* will be executed regularly by a sqlagent job */
    Set nocount ON

    if object_id('dbo.T_DBA_ConnectionTracker') is null
    begin
     print 'Table [T_DBA_ConnectionTracker] Created';
     CREATE TABLE [dbo].[T_DBA_ConnectionTracker](
      [hostname] [varchar](128) NOT NULL,
      [program_name] [varchar](128) NOT NULL,
      [nt_domain] [varchar](128) NOT NULL,
      [nt_username] [varchar](128) NOT NULL,
      [loginame] [varchar](128) NOT NULL,
      [dbid] smallint not null,
      [tsRegistration] datetime not null default getdate(),
      [tsLastUpdate] datetime not null default getdate()
       ) ;
     Create clustered index clX_DBA_ConnectionTracker on [dbo].[T_DBA_ConnectionTracker] ([tsRegistration]);
     Create index X_DBA_ConnectionTracker on [dbo].[T_DBA_ConnectionTracker] ([loginame]);
    end

    /* Update registered connections */
    update T
     Set [tsLastUpdate] = getdate()
    from T_DBA_ConnectionTracker T
    inner join sysprocesses P
     on  P.hostname     = T.hostname
     and P.program_name = T.program_name
     and P.nt_domain    = T.nt_domain
     and P.nt_username  = T.nt_username
     and P.loginame     = T.loginame
     and P.dbid         = T.dbid
     and P.loginame not like '%\ServiceSQL%'; -- exclude SQLServer service accounts
     
    /* Register new connections */
    insert into T_DBA_ConnectionTracker (hostname, program_name, nt_domain, nt_username, loginame, dbid)
    Select distinct rtrim(P.hostname), rtrim(P.program_name), rtrim(P.nt_domain), rtrim(P.nt_username), rtrim(P.loginame), isnull(dbid,-1)
    from sysprocesses P
    Where not exists ( Select *
         from T_DBA_ConnectionTracker T
           Where  P.hostname     = T.hostname
           and P.program_name = T.program_name
           and P.nt_domain    = T.nt_domain
           and P.nt_username  = T.nt_username
           and P.loginame     = T.loginame
           and P.dbid         = T.dbid ) ;

    /*

     Select *
     from dbo.T_DBA_ConnectionTracker
     Order by hostname, program_name, nt_domain, nt_username, loginame

    */
    end


    go

    /* Install corresponding job */
    BEGIN TRANSACTION           
      DECLARE @JobID BINARY(16) 
      DECLARE @ReturnCode INT   
      SELECT @ReturnCode = 0    
    IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1
      EXECUTE msdb.dbo.sp_add_category @name = N'[Uncategorized (Local)]'
    IF (SELECT COUNT(*) FROM msdb.dbo.sysjobs WHERE name = N'DBA_ConnectionTracker') > 0
      PRINT N'The job "DBA_ConnectionTracker" already exists so will not be replaced.'
    ELSE
    BEGIN

      -- Add the job
      EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'DBA_ConnectionTracker', @owner_login_name = N'sa', @description = N'To support Connection Inventory for License check.', @category_name = N'[Uncategorized (Local)]', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0
      IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

      -- Add the job steps
      EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'ConnectionTracker', @command = N'exec sp_DBA_ConnectionTracker', @database_name = N'master', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2
      IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
      EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1

      IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

      -- Add the job schedules
      EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'EveryMinute', @enabled = 1, @freq_type = 4, @active_start_date = 20080715, @active_start_time = 0, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 1, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959
      IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

      -- Add the Target Servers
      EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)'
      IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

    END
    COMMIT TRANSACTION         
    GOTO   EndSave             
    QuitWithRollback:
      IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
    EndSave:

  • 08-27-2008 8:18 AM In reply to

    Re: Managing SQL Server Database and Application Metadata

    What parameters are needed to execute the pAddApplication proc?

     

    It's looking for ServerName, DatabaseName and ApplicationName.

Page 1 of 1 (3 items)