Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Thursday, March 8, 2012

Blocking behavior of trigger and sp_send_dbmail

Hi Folks
I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
short: sp_send_dbmail inside of trigger results in blocked process and never
ends.
I wanted to code something simple the get notified if new rows were inserted
or updated in table.
create table t1 (col1 int, col2 int)
go
create table t2 (col1 int, col2 int)
go
create trigger tr_ins_t1 on t1 for insert, update
as
set nocount on
truncate table t2
insert t2
select * from inserted
exec msdb.dbo.sp_send_dbmail
@.recipients = 'gene_golub@.hotmail.com'
, @.subject = '!! sql1..t1 db : inserted new rows'
, @.query = 'select * from queries.dbo.t1'
go
insert t1 values(1,1)
go
side comment: inserted table was not recognized inside of query sql string
which would be executed by sp_send_dbmail so I had to use another table to
insert rows first and then to send them over.
When I insert simple row, my query hangs forewer.
What i see in sp_who: some process which is blocked by insert statement.
And this block does not resolve itself untill I go and kill process which is
bloced by insert stm.
I had no problem before using xp_sendmail inside of the triggers. Why it
become a problem with sp_send_dbmail?
Can anybody explain me what am I doing wrong?
thank you, Gene.
Hi Gene,
I have seen that behavior too. Would this work for you?
select * from queries.dbo.t1 with (nolock)
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Gene." wrote:

> Hi Folks
> I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
> short: sp_send_dbmail inside of trigger results in blocked process and never
> ends.
> I wanted to code something simple the get notified if new rows were inserted
> or updated in table.
> create table t1 (col1 int, col2 int)
> go
> create table t2 (col1 int, col2 int)
> go
> create trigger tr_ins_t1 on t1 for insert, update
> as
> set nocount on
> truncate table t2
> insert t2
> select * from inserted
> exec msdb.dbo.sp_send_dbmail
> @.recipients = 'gene_golub@.hotmail.com'
> , @.subject = '!! sql1..t1 db : inserted new rows'
> , @.query = 'select * from queries.dbo.t1'
> go
> insert t1 values(1,1)
> go
> side comment: inserted table was not recognized inside of query sql string
> which would be executed by sp_send_dbmail so I had to use another table to
> insert rows first and then to send them over.
> When I insert simple row, my query hangs forewer.
> What i see in sp_who: some process which is blocked by insert statement.
> And this block does not resolve itself untill I go and kill process which is
> bloced by insert stm.
> I had no problem before using xp_sendmail inside of the triggers. Why it
> become a problem with sp_send_dbmail?
> Can anybody explain me what am I doing wrong?
> thank you, Gene.
|||Hi Ben
Thank you for looking into this issue.
My example was simplification of real situation. Real table has about 1500
rows and wider.
nolock option does not help in that case.
Thank you, Gene.
"Ben Nevarez" wrote:
[vbcol=seagreen]
> Hi Gene,
> I have seen that behavior too. Would this work for you?
> select * from queries.dbo.t1 with (nolock)
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Gene." wrote:

Blocking behavior of trigger and sp_send_dbmail

Hi Folks
I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
short: sp_send_dbmail inside of trigger results in blocked process and never
ends.
I wanted to code something simple the get notified if new rows were inserted
or updated in table.
create table t1 (col1 int, col2 int)
go
create table t2 (col1 int, col2 int)
go
create trigger tr_ins_t1 on t1 for insert, update
as
set nocount on
truncate table t2
insert t2
select * from inserted
exec msdb.dbo.sp_send_dbmail
@.recipients = 'gene_golub@.hotmail.com'
, @.subject = '!! sql1..t1 db : inserted new rows'
, @.query = 'select * from queries.dbo.t1'
go
insert t1 values(1,1)
go
side comment: inserted table was not recognized inside of query sql string
which would be executed by sp_send_dbmail so I had to use another table to
insert rows first and then to send them over.
When I insert simple row, my query hangs forewer.
What i see in sp_who: some process which is blocked by insert statement.
And this block does not resolve itself untill I go and kill process which is
bloced by insert stm.
I had no problem before using xp_sendmail inside of the triggers. Why it
become a problem with sp_send_dbmail?
Can anybody explain me what am I doing wrong?
thank you, Gene.Hi Gene,
I have seen that behavior too. Would this work for you?
select * from queries.dbo.t1 with (nolock)
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Gene." wrote:

> Hi Folks
> I came accross very strange behavior of trigger utilizing sp_send_dbmail.
In
> short: sp_send_dbmail inside of trigger results in blocked process and nev
er
> ends.
> I wanted to code something simple the get notified if new rows were insert
ed
> or updated in table.
> create table t1 (col1 int, col2 int)
> go
> create table t2 (col1 int, col2 int)
> go
> create trigger tr_ins_t1 on t1 for insert, update
> as
> set nocount on
> truncate table t2
> insert t2
> select * from inserted
> exec msdb.dbo.sp_send_dbmail
> @.recipients = 'gene_golub@.hotmail.com'
> , @.subject = '!! sql1..t1 db : inserted new rows'
> , @.query = 'select * from queries.dbo.t1'
> go
> insert t1 values(1,1)
> go
> side comment: inserted table was not recognized inside of query sql string
> which would be executed by sp_send_dbmail so I had to use another table to
> insert rows first and then to send them over.
> When I insert simple row, my query hangs forewer.
> What i see in sp_who: some process which is blocked by insert statement.
> And this block does not resolve itself untill I go and kill process which
is
> bloced by insert stm.
> I had no problem before using xp_sendmail inside of the triggers. Why it
> become a problem with sp_send_dbmail?
> Can anybody explain me what am I doing wrong?
> thank you, Gene.|||Hi Ben
Thank you for looking into this issue.
My example was simplification of real situation. Real table has about 1500
rows and wider.
nolock option does not help in that case.
Thank you, Gene.
"Ben Nevarez" wrote:
[vbcol=seagreen]
> Hi Gene,
> I have seen that behavior too. Would this work for you?
> select * from queries.dbo.t1 with (nolock)
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Gene." wrote:
>

Blocking behavior of trigger and sp_send_dbmail

Hi Folks
I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
short: sp_send_dbmail inside of trigger results in blocked process and never
ends.
I wanted to code something simple the get notified if new rows were inserted
or updated in table.
create table t1 (col1 int, col2 int)
go
create table t2 (col1 int, col2 int)
go
create trigger tr_ins_t1 on t1 for insert, update
as
set nocount on
truncate table t2
insert t2
select * from inserted
exec msdb.dbo.sp_send_dbmail
@.recipients = 'gene_golub@.hotmail.com'
, @.subject = '!! sql1..t1 db : inserted new rows'
, @.query = 'select * from queries.dbo.t1'
go
insert t1 values(1,1)
go
side comment: inserted table was not recognized inside of query sql string
which would be executed by sp_send_dbmail so I had to use another table to
insert rows first and then to send them over.
When I insert simple row, my query hangs forewer.
What i see in sp_who: some process which is blocked by insert statement.
And this block does not resolve itself untill I go and kill process which is
bloced by insert stm.
I had no problem before using xp_sendmail inside of the triggers. Why it
become a problem with sp_send_dbmail?
Can anybody explain me what am I doing wrong?
thank you, Gene.Hi Gene,
I have seen that behavior too. Would this work for you?
select * from queries.dbo.t1 with (nolock)
Hope this helps,
Ben Nevarez
Senior Database Administrator
AIG SunAmerica
"Gene." wrote:
> Hi Folks
> I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
> short: sp_send_dbmail inside of trigger results in blocked process and never
> ends.
> I wanted to code something simple the get notified if new rows were inserted
> or updated in table.
> create table t1 (col1 int, col2 int)
> go
> create table t2 (col1 int, col2 int)
> go
> create trigger tr_ins_t1 on t1 for insert, update
> as
> set nocount on
> truncate table t2
> insert t2
> select * from inserted
> exec msdb.dbo.sp_send_dbmail
> @.recipients = 'gene_golub@.hotmail.com'
> , @.subject = '!! sql1..t1 db : inserted new rows'
> , @.query = 'select * from queries.dbo.t1'
> go
> insert t1 values(1,1)
> go
> side comment: inserted table was not recognized inside of query sql string
> which would be executed by sp_send_dbmail so I had to use another table to
> insert rows first and then to send them over.
> When I insert simple row, my query hangs forewer.
> What i see in sp_who: some process which is blocked by insert statement.
> And this block does not resolve itself untill I go and kill process which is
> bloced by insert stm.
> I had no problem before using xp_sendmail inside of the triggers. Why it
> become a problem with sp_send_dbmail?
> Can anybody explain me what am I doing wrong?
> thank you, Gene.|||Hi Ben
Thank you for looking into this issue.
My example was simplification of real situation. Real table has about 1500
rows and wider.
nolock option does not help in that case.
Thank you, Gene.
"Ben Nevarez" wrote:
> Hi Gene,
> I have seen that behavior too. Would this work for you?
> select * from queries.dbo.t1 with (nolock)
> Hope this helps,
> Ben Nevarez
> Senior Database Administrator
> AIG SunAmerica
>
> "Gene." wrote:
> > Hi Folks
> >
> > I came accross very strange behavior of trigger utilizing sp_send_dbmail. In
> > short: sp_send_dbmail inside of trigger results in blocked process and never
> > ends.
> >
> > I wanted to code something simple the get notified if new rows were inserted
> > or updated in table.
> >
> > create table t1 (col1 int, col2 int)
> > go
> > create table t2 (col1 int, col2 int)
> > go
> > create trigger tr_ins_t1 on t1 for insert, update
> > as
> > set nocount on
> > truncate table t2
> > insert t2
> > select * from inserted
> >
> > exec msdb.dbo.sp_send_dbmail
> > @.recipients = 'gene_golub@.hotmail.com'
> > , @.subject = '!! sql1..t1 db : inserted new rows'
> > , @.query = 'select * from queries.dbo.t1'
> > go
> > insert t1 values(1,1)
> > go
> >
> > side comment: inserted table was not recognized inside of query sql string
> > which would be executed by sp_send_dbmail so I had to use another table to
> > insert rows first and then to send them over.
> >
> > When I insert simple row, my query hangs forewer.
> > What i see in sp_who: some process which is blocked by insert statement.
> > And this block does not resolve itself untill I go and kill process which is
> > bloced by insert stm.
> >
> > I had no problem before using xp_sendmail inside of the triggers. Why it
> > become a problem with sp_send_dbmail?
> > Can anybody explain me what am I doing wrong?
> > thank you, Gene.

Wednesday, March 7, 2012

Block a truncate command

Is there any way to block a truncate command on a table via a trigger or
something else in MS SQL Server 2000?
Thanks,
--
JasonHere's a quote from Books Online:
"TRUNCATE TABLE permissions default to the table owner, members of the
symin fixed server role, and the db_owner and db_ddladmin fixed database
roles, and are not transferable."
Are you worried about the above users executing a TRUNCATE TABLE command?
Also note that this command cannot be run against a table that is referenced
by foreign key.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"JasonDWilson" <JasonDWilson@.discussions.microsoft.com> wrote in message
news:9E2D92A7-C397-4538-9574-1133F9B37B33@.microsoft.com...
> Is there any way to block a truncate command on a table via a trigger or
> something else in MS SQL Server 2000?
> Thanks,
> --
> Jason|||If you don't want the table truncated, then don't issue the truncte in the
first place.
"JasonDWilson" <JasonDWilson@.discussions.microsoft.com> wrote in message
news:9E2D92A7-C397-4538-9574-1133F9B37B33@.microsoft.com...
> Is there any way to block a truncate command on a table via a trigger or
> something else in MS SQL Server 2000?
> Thanks,
> --
> Jason|||No.
Try better user/permissions management.
Do you have people that have been given symin or dbo privileges and are
going to go in and issue unauthorized TRUNCATE commands? I think you need
to fix that problem instead of trying to patch the symptom...
"JasonDWilson" <JasonDWilson@.discussions.microsoft.com> wrote in message
news:9E2D92A7-C397-4538-9574-1133F9B37B33@.microsoft.com...
> Is there any way to block a truncate command on a table via a trigger or
> something else in MS SQL Server 2000?
> Thanks,
> --
> Jason|||I want them to be able to truncate some tables, but not others.
--
Jason
"Aaron Bertrand [SQL Server MVP]" wrote:

> No.
> Try better user/permissions management.
> Do you have people that have been given symin or dbo privileges and are
> going to go in and issue unauthorized TRUNCATE commands? I think you need
> to fix that problem instead of trying to patch the symptom...
>
>
> "JasonDWilson" <JasonDWilson@.discussions.microsoft.com> wrote in message
> news:9E2D92A7-C397-4538-9574-1133F9B37B33@.microsoft.com...
>
>|||But only the table owner can truncate anyhow. Are you saying that these user
s act as dbo, the object
owner, db_owner or symin in your system?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"JasonDWilson" <JasonDWilson@.discussions.microsoft.com> wrote in message
news:76E0D204-6026-4BDB-8B54-F384FF67556A@.microsoft.com...
>I want them to be able to truncate some tables, but not others.
> --
> Jason
>
> "Aaron Bertrand [SQL Server MVP]" wrote:
>

Friday, February 10, 2012

Bit more specific, Can one pass an NVARCHAR to IF UPDATED()?

Question is pretty much in the thread title.

I'm still plunking away on a history trigger that requires the evaluation of whether or not each column has been updated. If it has,t hen I'll be tossing a record into a history table.

Basically I need something to function along the same logic as this:

IF UPDATED(@.s)
BEGIN
--some code
END

Any thoughts you folks may have are greatly appreciated.About the closest I can get is the Columns_Updated() and maybe some fancy dancing with syscolumns.

-PatP|||About the closest I can get is the Columns_Updated() and maybe some fancy dancing with syscolumns.

-PatP

At least it's something to go on. I'll take a look at it tomorrow.

Much obliged good sir.