SQL Server - APP_NAME() empty when deleting
Asked By Jon Davis
03-May-07 05:53 PM
I was trying to set up auditing triggers on a table and was using APP_NAME()
to get the name of the application that performed the action. APP_NAME()
always returns "SQL Server Management Studio - Query" when I'm testing in
that environment and executing INSERTs or UPDATEs. But for some unknown
reason, it returns blank when executing DELETE.
I haven't explicitly set the application name but I was expecting it to
still show "SQL Server Management Studio" here, especially since DELETEs are
as worthy of auditing as anything else. Is this by design? Any idea why?
Jon
NOT NULL
(1)
SQL Server
(1)
CREATE TABLE
(1)
PRIMARY KEY
(1)
CONSTRAINT
(1)
DROP TABLE
(1)
UNION
(1)
Nvarchar
(1)
Dan Guzman replied...

I can't seem to repro using the script below. What SQL Server version and
service pack are you using?
CREATE TABLE dbo.MyTable
(
MyColumn int NOT NULL
CONSTRAINT PK_MyTable PRIMARY KEY
)
GO
CREATE TABLE dbo.MyTableAudit
(
AuditID int IDENTITY NOT NULL
CONSTRAINT PK_MyTableAudit PRIMARY KEY,
MyColumn int NOT NULL,
TableAction varchar(20) NOT NULL,
ActionDateTime datetime NOT NULL,
Application nvarchar(128) NULL,
UserName nvarchar(128) NULL,
)
GO
CREATE TRIGGER TR_MyTable_Insert
ON dbo.MyTable FOR INSERT
AS
INSERT INTO dbo.MyTableAudit
SELECT MyColumn, 'Insert', GETDATE(), APP_NAME(), SUSER_SNAME()
FROM inserted
GO
CREATE TRIGGER TR_MyTable_Update
ON dbo.MyTable FOR UPDATE
AS
INSERT INTO dbo.MyTableAudit
SELECT MyColumn, 'Update (before)', GETDATE(), APP_NAME(), SUSER_SNAME()
FROM deleted
UNION ALL
SELECT MyColumn, 'Update (after)', GETDATE(), APP_NAME(), SUSER_SNAME()
FROM inserted
GO
CREATE TRIGGER TR_MyTable_Delete
ON dbo.MyTable FOR DELETE
AS
INSERT INTO dbo.MyTableAudit
SELECT MyColumn, 'Delete', GETDATE(), APP_NAME(), SUSER_SNAME()
FROM deleted
GO
INSERT INTO dbo.MyTable VALUES(1)
GO
UPDATE dbo.MyTable
SET MyColumn = 2
GO
DELETE FROM dbo.MyTable
GO
SELECT * FROM dbo.MyTableAudit
GO
DROP TABLE dbo.MyTable, dbo.MyTableAudit
GO
--
Hope this helps.
Dan Guzman
SQL Server MVP
SQL Server Scripting Extended Properties I am running on SQL Server 2000 SP4. I am attempting to generate SQL Script and include extended properties. When doing so, the extended properties for one table (i.e. Column Description) are not generated. Extended properties for all other tables script just fine. I've even gone so far as to script the table without extended properties, import it into a new database, manually add column descriptions, and generate joy. Any ideas? Do you get an error? Can you post the DDL for the table? Are you using any reserved words for any of the object names? - - Andrew J. Kelly
SQL Server Database Engine Tuning Advisor I am trying to use the Database Engine Tuning Advisor. I Engine Tuning Advisor. When I start the Database Engine Tuning Advisor for either version of SQL I get the same message when I click Connect using either the sa account or to open a new connection. Additional information: Could not find stored procedure 'msdb. . sp_DTA_help_session'. (Microsoft SQL Server.Error:2812) I have checked and the stored procedure does not exist in msdb for description, I understand that we failed to use Database Engine Tuning Advisor to connect to SQL server database. If I have misunderstood, please let me know. Based on the current situation, it doesn't resolve the issue, the issue might be related to the version of the SQL Server. are you using RTM or SQL 2005 sp2 version? Besides, You may experience problems
SQL Server Long lasting schema stability (Sch-S) locks This problem is occuring on SQL 2000 Users of my web site are complaining that the application is hanging at one for many minutes. After looking through BOL, it seems like these locks indicate that the SQL is compiling, but what would cause it to take so long to compile? This is the stored proc is based on an indexed column. Here is the DDL, if needed: CREATE TABLE [dbo].[DN1] ( [Record_Number] [int] NOT NULL , [Inactive_flag] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Deleted_flag] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [NAME] [varchar] (80) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [PROCEDURE_CODE] [varchar] (8
SQL Server Performance degrades after using NOLOCK Hi, Can someone help explain the following please. . . I have SELECT 'No'; This query runs pretty much instantaneously. Here is the IO and TIME stats: Table 'table1'. Scan count 1, logical reads 28, physical reads 3, read- ahead reads 492, lob logical reads 0, lob physical reads 0, lob read- ahead reads 0. Table 'tblData'. Scan count 0, logical reads 2, physical reads 0, read- ahead reads 0, lob reads 0, lob physical reads 0, lob read- ahead reads 0. (1 row(s) affected) SQL Server Execution Times: CPU time = 0 ms, elapsed time = 26 ms. (1 row(s) affected) SQL Server Execution Times: CPU time = 0 ms, elapsed time = 1 ms. Just out of curiosity, I
SQL Server Problem with DROP CONSTRAINT When I issue the following T-SQL 2008: ALTER TABLE ALIS.mc.LOCApp DROP CONSTRAINT LOCApp_Created_df I get the following errors: Msg 1781, Level 16, State 1, Line 1 Column a DEFAULT bound to it. Msg 1750, Level 16, State 0, Line 1 Could not create constraint. See previous errors. I can see the constraint exists in Object Explorer. If I attempt