SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger insert


Trigger insert

Author
Message
astrid 69000
astrid 69000
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2300 Visits: 337
Hi,

I have a table that keep track of the store procedures and it log errors. I want to add the name of the database to the table.

My table looks as follows


CREATE TABLE MetricsMart.dbo.ProcedureLog
(
LogDate SMALLDATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
DatabaseID INT,
DatabaseName nvarchar(200),
ObjectID INT,
ProcedureName NVARCHAR(400),
ErrorLine INT,
ErrorMessage NVARCHAR(MAX),
AdditionalInfo NVARCHAR(MAX)
);


I am trying to create a trigger that after each insert on the table, it will add the name of the db to the table, from sys.databases.

Taking in consideration I never created a trigger on my life, nothing is working Crying

This is my last half intent, as all the previous ones didn't work either.

Please help

CREATE TRIGGER InsertDBDetails_tr
ON MetricsMart
AFTER INSERT
AS
BEGIN
UPDATE ProcedureLog
SET DatabaseName = (SELECT NAME FROM SYS.DATABASES)
FROM INSERTED


Joe Torre
Joe Torre
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8356 Visits: 1475
Update the code in stored procedures to log this.
astrid 69000
astrid 69000
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2300 Visits: 337
that is what at did, but i still don't know how to do that in a trigger Crying
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)

Group: General Forum Members
Points: 122174 Visits: 8990
Wait - am I seeing correctly that your sproc actually creates the log record? If so, why isn't it simply putting the database info in the row it creates?

If it isn't, what is creating the initial error log row? Whatever it is should be populating ALL FIELDS! It is TERRIBLY inefficient to insert a row and then turn around an update it with data that you should have had on insert.

Also, look up db_id() and db_name() system functions.

Oh. and why are you storing both the database name and the database id? Same goes for procedure name and object id, right?? Wasted storage with zero benefit.

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
astrid 69000
astrid 69000
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2300 Visits: 337
thanks, I did added it in the code after I posted.
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85508 Visits: 9624
TheSQLGuru - Friday, January 26, 2018 2:08 PM
Wait - am I seeing correctly that your sproc actually creates the log record? If so, why isn't it simply putting the database info in the row it creates?

If it isn't, what is creating the initial error log row? Whatever it is should be populating ALL FIELDS! It is TERRIBLY inefficient to insert a row and then turn around an update it with data that you should have had on insert.

Also, look up db_id() and db_name() system functions.

Oh. and why are you storing both the database name and the database id? Same goes for procedure name and object id, right?? Wasted storage with zero benefit.

But the database_id and object_id can both change. Thus, it's really required to store the name to be accurate. And storing the existing id isn't that much space and so not necessarily a terrible idea, just for reference purposes.


SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)

Group: General Forum Members
Points: 122174 Visits: 8990
ScottPletcher - Monday, February 5, 2018 3:26 PM
TheSQLGuru - Friday, January 26, 2018 2:08 PM
Wait - am I seeing correctly that your sproc actually creates the log record? If so, why isn't it simply putting the database info in the row it creates?

If it isn't, what is creating the initial error log row? Whatever it is should be populating ALL FIELDS! It is TERRIBLY inefficient to insert a row and then turn around an update it with data that you should have had on insert.

Also, look up db_id() and db_name() system functions.

Oh. and why are you storing both the database name and the database id? Same goes for procedure name and object id, right?? Wasted storage with zero benefit.

But the database_id and object_id can both change. Thus, it's really required to store the name to be accurate. And storing the existing id isn't that much space and so not necessarily a terrible idea, just for reference purposes.

Name is all that is needed - agreed - and is obviously what was intended to keep.

Sorry, I completely disagree with storing the id's "just for reference purposes". 8 unnecessary bytes is just that. Full stop.

I note though that I am actually VER HAPPY that most people out there feel the same way as you do! It creates more performance tuning opportunities for me!! Cool


Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85508 Visits: 9624
TheSQLGuru - Tuesday, February 6, 2018 8:03 AM
ScottPletcher - Monday, February 5, 2018 3:26 PM
TheSQLGuru - Friday, January 26, 2018 2:08 PM
Wait - am I seeing correctly that your sproc actually creates the log record? If so, why isn't it simply putting the database info in the row it creates?

If it isn't, what is creating the initial error log row? Whatever it is should be populating ALL FIELDS! It is TERRIBLY inefficient to insert a row and then turn around an update it with data that you should have had on insert.

Also, look up db_id() and db_name() system functions.

Oh. and why are you storing both the database name and the database id? Same goes for procedure name and object id, right?? Wasted storage with zero benefit.

But the database_id and object_id can both change. Thus, it's really required to store the name to be accurate. And storing the existing id isn't that much space and so not necessarily a terrible idea, just for reference purposes.

Name is all that is needed - agreed - and is obviously what was intended to keep.

Sorry, I completely disagree with storing the id's "just for reference purposes". 8 unnecessary bytes is just that. Full stop.

I note though that I am actually VER HAPPY that most people out there feel the same way as you do! It creates more performance tuning opportunities for me!! Cool

Fascinating. You recommend a very inefficient method then say what a great tuner you are! Wow, rather :Crazy:.


SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)SSC Guru (122K reputation)

Group: General Forum Members
Points: 122174 Visits: 8990
ScottPletcher - Tuesday, February 6, 2018 10:03 AM

Fascinating. You recommend a very inefficient method then say what a great tuner you are! Wow, rather :Crazy:.

I am pretty tired today, so maybe I am missing something. But how is it that storing 8 bytes less (the two ids) as I suggested is very inefficient?


Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
ScottPletcher
ScottPletcher
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85508 Visits: 9624
TheSQLGuru - Tuesday, February 6, 2018 1:15 PM
ScottPletcher - Tuesday, February 6, 2018 10:03 AM

Fascinating. You recommend a very inefficient method then say what a great tuner you are! Wow, rather :Crazy:.

I am pretty tired today, so maybe I am missing something. But how is it that storing 8 bytes less (the two ids) as I suggested is very inefficient?

Storing the names is inefficient. Just create your own id that cannot change and use it. For a few rows, won't matter much. For lots of rows, could be a huge gain. I don't see a need to distinguish object type, so I don't, I just have a single names table that provides the id for any name.

Although even if I chose to store the name, I would also store the original id, since certain internal / trace / relationship tables use only id not the name. Compress the row and it'll be no more than 5 bytes the vast majority of the time.


SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search