| Business Application Development with: SQL Server, C#, VB, VB.Net, ASP, ASP.Net, and XML |
| N | S |
Novick Software Management • Design • Programming • Training • Consulting |
|
|
News Links Schedule Site Map Contact |
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
Table 1 AppSession Columns |
||
|
Column |
Data Type |
Description |
|
SessionID |
Int |
Identifies the session, used in other tables to tie traces together. |
|
ApplicationID |
Int |
Which application is responsible for the program. Used when more than one application is using the same database. |
|
SessionGUID |
UniqueIdentifier |
GUID for the session, provides a cross database unique key when needed. |
|
ProgramTypeCD |
Varchar(8) |
Type of program, Windows Forms, ASP.Net, SQL Job? From AppSessionProgramTypeCD |
|
ClientComputerName |
Varchar(32) |
Windows name of the computer where the client is running. |
|
ClientProgramName |
Varchar(128) |
Name of the program on the client side. |
|
ClientUSERID |
Varchar(64) |
Identifier of the user running the client program. |
|
ExecutedOnComputerName |
Varchar(32) |
Windows name of the computer where the program ran. |
|
UserID |
Varchar(64) |
Application User identifier. |
|
CreateDT |
Datetime |
When the AppSession row was created. |
|
LastUpDT |
Datetime |
When this AppSession row was last updated |
|
DurationDUR |
Datetime |
Clock time spent by the program expressed as a datetime. |
Valid program type codes for the ProgramTypeCD column are stored in
the AppSessionProgramTypeCD table. Here’s a script to create it and
populate it with the values that I normally use:
CREATE TABLE
AppSessionProgramTypeCD (
ProgramTypeCD varchar (32)
NOT NULL ,
[Description] nvarchar (255)
NOT NULL ,
CONSTRAINT
PK_AppSessionProgramTypeCD
PRIMARY KEY CLUSTERED
(ProgramTypeCD)
)
GO
SET QUOTED_IDENTIFIER OFF
GO
INSERT INTO AppSessionProgramTypeCD
VALUES ("ASP.NET","ASP.Net Session")
INSERT INTO AppSessionProgramTypeCD
VALUES ("SQLSP","Stored Procedure")
INSERT INTO AppSessionProgramTypeCD
VALUES ("SQLJOB"
,"Job run by
SQL Server Agent")
INSERT INTO AppSessionProgramTypeCD
VALUES ("WINFORMS"
,"Windows 32
bit .Net CLR Application")
GO
To make my coding life easier, I have a couple of stored procedures that kick off a session by creating the AppSession row. The first of these is usp_AppSession_Create, which handles the general case where I want to supply tall the parameters. Here’s the CREATE PROC script:
CREATE PROCEDURE
dbo.usp_AppSession_Create
@ApplicationID int = 1
-- Application ID
, @ProgramTypeCD varchar(32)
= 'SQLSP' -- Type of program
, @ClientProcessID int = 0
-- Process Identifier
, @ClientComputerName varchar(32)
-- Which Computer started this session.
, @ClientProgramName varchar(128)
-- Program starting session
, @ClientUserID varchar(20)
-- User ID of end user.
, @ExecutedOnComputer varchar(32)
-- Computer name where run
, @UserID varchar(20)
-- User id on this system.
, @SessID int OUTPUT
-- New Session ID
/* Creates an AppSession row based on the input parameters.
*
* Example:
DECLARE @RC int -- Return code
, @ProgName varchar(128)
, @Session_User varchar(20)
, @Host_Name varchar(32)
, @User_Name varchar(128)
, @SessID int
SELECT @ProgName = APP_NAME()
, @SEssion_User = SESSION_USER
, @Host_Name = host_name()
, @User_Name = User_Name()
EXEC @RC = usp_AppSession_Create 0
, 'Test Batch 1'
, @@SPID -- ClientProcessID
, @HOST_NAME -- ClientComputerName
, @ProgName
, @USER_NAME
, @@SERVERNAME
, @SESSION_USER
, @SessID OUTPUT
PRINT 'RC = ' + CONVERT(varchar, @RC)
PRINT 'New Session ID=' + CONVERT(varchar,@SessID)
*
* © Copyright 2005 Andrew Novick http://www.NovickSoftware.com
* You may use this code in any of your SQL Server databases
* including databases that you sell, so long as they contain
* other unrelated database objects. You may not publish this
* UDF either in print or electronically.
* Published as Coding in SQL Newsletter Vol 3 #3 1/19/05
http://www.NovickSoftware.com/coding-in-sql.htm
****************************************************************/
AS
DECLARE @myError int, @myRowCount
int
SET NOCOUNT ON
-- Create the AppSession row
INSERT INTO AppSession
(ApplicationID, SessionGUID, ProgramTypeCD,
ClientProcessID
, ClientComputerName, ClientProgramName
, ClientUSERID, ExecutedOnComputerName
, USERID, CreatedDT, LastUpDT)
VALUES (@ApplicationID,
newid(), @ProgramTypeCD, @ClientProcessID
, @ClientComputerName, @ClientProgramName
, @ClientUserID, @ExecutedOnComputer
, @UserID, GetDate(),
GetDate())
SELECT @myError = @@Error
, @SessID = SCOPE_IDENTITY()
RETURN @myError
GO
One has to supply quite a few parameters to usp_AppSession_Create and I found myself copying the script in the comment header several times. So for T-SQL stored procedures, I have another proc that fills in some of the parameters. usp_AppSession_SQLJOB takes only four parameters:
CREATE PROCEDURE
dbo.usp_AppSession_SQLJOB
@ApplicationID int = 1
-- Application ID
, @ClientProgramName varchar(128)
= NULL --
Program
-- that
is starting the session
, @ClientUserID varchar(20)
-- User ID of end user.
, @SessID int OUTPUT
-- New Session ID
/* Create an AppSession for a SQLJOB.
* Used by stored procedures that are run directly by SQL Server
* Agent.
*
* Test:
DECLARE @RC int, @SessID int
exec @RC = usp_AppSession_SQLJOB 1
, 'TEST_SCRIPT_Usp_AppSession_SQLJOB'
, 'TESTUSER', @SessID OUTPUT
PRINT 'RC = ' + coalesce(convert(varchar(12), @rc), '<null')
+ ' Sess=' + coalesce(convert(varchar(12), @sessID), '<null>')
select * from AppSession WHERE SessionID = @SessID
*
* © Copyright 2005 Andrew Novick http://www.NovickSoftware.com
* You may use this code in any of your SQL Server databases
* including databases that you sell, so long as they contain
* other unrelated database objects. You may not publish this
* UDF either in print or electronically.
* Published as Coding in SQL Newsletter Vol 3 #3 1/19/05
http://www.NovickSoftware.com/coding-in-sql.htm
****************************************************************/
AS
SET NOCOUNT ON
DECLARE @RC int
-- Return code
, @ProgName varchar(128)
, @Session_User varchar(20)
, @Host_Name varchar(32)
, @User_Name varchar(128)
SELECT @ProgName =
COALESCE(@ClientProgramName,
APP_NAME())
, @SEssion_User = SESSION_USER
, @Host_Name = host_name()
, @User_Name = User_Name()
EXEC @RC = usp_AppSession_Create @ApplicationID
, 'SQLJOB'
, @@SPID
-- ClientProcessID
, @HOST_NAME
-- ClientComputerName
, @ProgName
, @USER_NAME
, @@SERVERNAME
, @SESSION_USER
, @SessID
OUTPUT
RETURN @RC
GO
The session is used to organize trace messages and to differentiate messages that come from one instance of a program from those that come from other instances of the same program.
When I started writing this issue I thought that I’d put everything about sessions and traces and cleaning up after ones self in one issue. That’s not going to work so I’ve split the information into three issues:
Part I: Application Session Management
Part II: Writing Trace Information to a Table
Part III: Cleaning up the Session and Trace information
I’ll defer an example of using usp_AppSession_SQLJOB until next issue.
As always, if you have comments about the newsletter and in particular, the new direction that I’m planning on taking, please let me know at coding-in-sql@novicksoftware.com.
Transact-SQL User-Defined Functions has been published! Take a look at it now!
Do you have an interesting stored procedure, user-defined-function,
or other sample code? If you'd like to share them please send
them in an email to:
coding-in-sql@NovickSoftware.com
and they might be published in this newsletter. I try and
respond to every request that I get.
Thanks,
Andy
Andrew Novick
New Tips:Loading SQL Profiler trace (.trc) files with fn_trace_gettrace |
Upcoming
|
|
Copyright © 2003-2008 Novick Software, Inc. | Terms of Use | Privacy Policy | Nice Things People Say| |