use of host_name( ) in triggers

From: Edward Bogaard (e.bogaard@citee.nl)
Date: 05/28/02


From: "Edward Bogaard" <e.bogaard@citee.nl>
Date: Tue, 28 May 2002 04:18:17 -0700


I wasn't able to reproduce your problem using SQL7, it
really inserts the hostname from the trigger. Below is
what I tried. The BOL suggest using HOST_NAME() as a
default value.

Hope this helps,
Edward
---------------------
CREATE TABLE Test
   (TEST_ID INT PRIMARY KEY,
    [USER] NCHAR(60) NOT NULL,
    [TABLE] NCHAR(60) NOT NULL,
    Workstation NCHAR(30) NOT NULL,
    [DATE] datetime not null
)
GO

CREATE TABLE TEST2
(
        TEST2_ID INT PRIMARY KEY,
        TEST2_VALUE NCHAR(30) NULL
)
GO

DROP TRIGGER ti_test

CREATE TRIGGER ti_test on TEST2 for insert AS
BEGIN
        DECLARE @ID INT
        SELECT @ID = isnull(MAX(TEST_ID), 0) + 1 FROM Test
        INSERT INTO Test (TEST_ID, [USER], [TABLE],
Workstation, [DATE])
        VALUES(@ID, SYSTEM_USER, 'TEST2', HOST_NAME(),
getdate())
END
GO

INSERT INTO TEST2 VALUES (1, 'TEST')
GO

select * from test
GO



Relevant Pages

  • Re: Trigger is not working when I test
    ... I also can't help wonderining ... why on earth you are using a trigger for something that could be very simply ... CONSTRAINTS in BOL. ... >> the rows that may be changed by the user action. ...
    (microsoft.public.sqlserver.programming)
  • Re: try catch ok, what about finally?
    ... -- This one raises an error in the trigger, ... CREATE TABLE test1 ... CREATE TABLE test2 NOT NULL PRIMARY KEY) ...
    (microsoft.public.sqlserver)
  • Re: INSERT Trigger
    ... aber in einer (fehlerhaften) Anwendung statt. ... >trigger möglich, siehe BOL. ...
    (microsoft.public.de.sqlserver)
  • use of host_name( ) in triggers
    ... Create Trigger x_check_modifier ... x_login_name nchar not null, ... >I wasn't able to reproduce your problem using SQL7, ... >CREATE TRIGGER ti_test on TEST2 for insert AS ...
    (microsoft.public.sqlserver.security)
  • Re: Copy identity key in new record in related table.
    ... If you read the BOL on @@IDENTITY you will see it is prone to problems if ... something else (like a trigger) inserts another record after you insert one. ... Scott ... > I will read about the the other two options, in the BOL ...
    (microsoft.public.sqlserver.programming)