Hi All,
Here I have a view like this:
create view vw_Lcustkeycode as
select *, convert(bit,0) as IsArchived from dbo.Lcustkeycode
union all
select *, convert(bit,1) as IsArchived from
DataEntryArchive.dbo.aLcustkeycode
I created an INSTEAD OF UPDATE trigger like:
CREATE TRIGGER tr_update_2cols on vw_Lcustkeycode INSTEAD OF UPDATE
AS
BEGIN
if update(UserName) and update(DateModified) begin
update d
set d.UserName=i.UserName, d.DateModified=i.DateModified
from dbo.Lcustkeycode d
inner join inserted i on d.LCustKeycode_id=i.LCustKeycode_id
where i.IsArchived=0
update d
set d.UserName=i.UserName, d.DateModified=i.DateModified
from DataEntryArchive.dbo.aLcustkeycode d
inner join inserted i on d.LCustKeycode_id=i.LCustKeycode_id
where i.IsArchived=1
end
END
When I run following update in Query Analyzer (notice the lcustkeycode_id is
primary key so only 1 row should be affected):
update vw_LCustKeycode
set username='jamma', datemodified=getdate()
where lcustkeycode_id=111060167
It said:
(2 row(s) affected)
(1 row(s) affected)
(8 row(s) affected)
(0 row(s) affected)
(8 row(s) affected)
I check the data and they are correct and really only 1 row was updated, but
why it said so many rows were affected? I find there are no other trigers
sitting there except my instead of trigger.
Can anyone here explain this strange behaviour?
Thanks,
JamesJames Ma wrote:
> Hi All,
> Here I have a view like this:
> create view vw_Lcustkeycode as
> select *, convert(bit,0) as IsArchived from dbo.Lcustkeycode
> union all
> select *, convert(bit,1) as IsArchived from
> DataEntryArchive.dbo.aLcustkeycode
> I created an INSTEAD OF UPDATE trigger like:
> CREATE TRIGGER tr_update_2cols on vw_Lcustkeycode INSTEAD OF UPDATE
> AS
> BEGIN
> if update(UserName) and update(DateModified) begin
> update d
> set d.UserName=i.UserName, d.DateModified=i.DateModified
> from dbo.Lcustkeycode d
> inner join inserted i on d.LCustKeycode_id=i.LCustKeycode_id
> where i.IsArchived=0
> update d
> set d.UserName=i.UserName, d.DateModified=i.DateModified
> from DataEntryArchive.dbo.aLcustkeycode d
> inner join inserted i on d.LCustKeycode_id=i.LCustKeycode_id
> where i.IsArchived=1
> end
> END
> When I run following update in Query Analyzer (notice the
> lcustkeycode_id is primary key so only 1 row should be affected):
> update vw_LCustKeycode
> set username='jamma', datemodified=getdate()
> where lcustkeycode_id=111060167
> It said:
> (2 row(s) affected)
> (1 row(s) affected)
> (8 row(s) affected)
> (0 row(s) affected)
> (8 row(s) affected)
> I check the data and they are correct and really only 1 row was
> updated, but why it said so many rows were affected? I find there are
> no other trigers sitting there except my instead of trigger.
> Can anyone here explain this strange behaviour?
> Thanks,
> James
Look in Profiler and see what it's doing (look at SP:StmtCompleted
events in addition to SQL:StmtCompleted).
David Gugick
Quest Software
www.imceda.com
www.quest.com|||Thanks for your quick reply. Just now I closed Query Analyzer and entered it
again, then the results become:
(1 row(s) affected)
(0 row(s) affected)
(1 row(s) affected)
Seems fine now. I can't explain what happened just now. Even when I set
nocount off, it retuned.
(2 row(s) affected)
(8 row(s) affected)
But now everything is fine.
"David Gugick" wrote:
> James Ma wrote:
> Look in Profiler and see what it's doing (look at SP:StmtCompleted
> events in addition to SQL:StmtCompleted).
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com
>|||Hello, James
The extra "n row(s) affected" are probably a side effect of the "Show
execution plan" in Query Analyzer. Retry your query with this option on
and off to see if that's the problem.
Razvan
Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts
Sunday, February 26, 2012
Fun of INSTEAD OF UPDATE trigger
Labels:
allselect,
asselect,
bit,
convert,
database,
dbo,
fun,
instead,
isarchived,
lcustkeycodeunion,
microsoft,
mysql,
oracle,
server,
sql,
thiscreate,
trigger,
update,
view,
vw_lcustkeycode
Friday, February 24, 2012
full-text search of SP/trigger source?
Is there any Microsoft utility that will search through the source of SPs,
triggers, and functions, looking for a string? (Something beyond the
dependencies listing).
Thanks
TimoThere isn't anything built-in that I'm aware of. You can either script the
objects to files using Enterprise Manager and then search the files or run a
query like the one below to search syscomments. Note that the query method
isn't 100% accurate since a string may be split between 2 rows.
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE text LIKE '%MyString%'
Hope this helps.
Dan Guzman
SQL Server MVP
"Timo" <timo@.noneofyer.biz> wrote in message
news:usSnZrvEFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Is there any Microsoft utility that will search through the source of SPs,
> triggers, and functions, looking for a string? (Something beyond the
> dependencies listing).
> Thanks
> Timo
>|||Timo
Vyas has written a great script
DROP PROCEDURE sp_FindObject
GO
CREATE PROCEDURE sp_FindObject
@.SearchString varchar (255)
AS
SET nocount ON
DECLARE @.Name varchar(255)
DECLARE @.Text nvarchar(4000)
CREATE TABLE #Objs
( ObjName varchar (255))
DECLARE Obj CURSOR
FOR SELECT [NAME],[TEXT] FROM sysobjects so, syscomments sc WHERE (so.xtype
='TR' or .....) AND so.id = sc.id
OPEN Obj
FETCH Next FROM Obj INTO @.Name,@.Text
WHILE @.@.FETCH_STATUS=0
BEGIN
IF PATINDEX(@.SearchString,@.Text) <> 0
INSERT INTO #Objs VALUES (@.Name)
FETCH Next FROM Obj INTO @.Name,@.Text
END
CLOSE Obj
DEALLOCATE Obj
SELECT objname FROM #Objs GROUP BY objname
DROP TABLE #Objs
go
EXEC sp_FindObject '%HOST_ID()%'
"Timo" <timo@.noneofyer.biz> wrote in message
news:usSnZrvEFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Is there any Microsoft utility that will search through the source of SPs,
> triggers, and functions, looking for a string? (Something beyond the
> dependencies listing).
> Thanks
> Timo
>|||Dan, Uri
Thank you both for the help!
Timo
triggers, and functions, looking for a string? (Something beyond the
dependencies listing).
Thanks
TimoThere isn't anything built-in that I'm aware of. You can either script the
objects to files using Enterprise Manager and then search the files or run a
query like the one below to search syscomments. Note that the query method
isn't 100% accurate since a string may be split between 2 rows.
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE text LIKE '%MyString%'
Hope this helps.
Dan Guzman
SQL Server MVP
"Timo" <timo@.noneofyer.biz> wrote in message
news:usSnZrvEFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Is there any Microsoft utility that will search through the source of SPs,
> triggers, and functions, looking for a string? (Something beyond the
> dependencies listing).
> Thanks
> Timo
>|||Timo
Vyas has written a great script
DROP PROCEDURE sp_FindObject
GO
CREATE PROCEDURE sp_FindObject
@.SearchString varchar (255)
AS
SET nocount ON
DECLARE @.Name varchar(255)
DECLARE @.Text nvarchar(4000)
CREATE TABLE #Objs
( ObjName varchar (255))
DECLARE Obj CURSOR
FOR SELECT [NAME],[TEXT] FROM sysobjects so, syscomments sc WHERE (so.xtype
='TR' or .....) AND so.id = sc.id
OPEN Obj
FETCH Next FROM Obj INTO @.Name,@.Text
WHILE @.@.FETCH_STATUS=0
BEGIN
IF PATINDEX(@.SearchString,@.Text) <> 0
INSERT INTO #Objs VALUES (@.Name)
FETCH Next FROM Obj INTO @.Name,@.Text
END
CLOSE Obj
DEALLOCATE Obj
SELECT objname FROM #Objs GROUP BY objname
DROP TABLE #Objs
go
EXEC sp_FindObject '%HOST_ID()%'
"Timo" <timo@.noneofyer.biz> wrote in message
news:usSnZrvEFHA.3936@.TK2MSFTNGP10.phx.gbl...
> Is there any Microsoft utility that will search through the source of SPs,
> triggers, and functions, looking for a string? (Something beyond the
> dependencies listing).
> Thanks
> Timo
>|||Dan, Uri
Thank you both for the help!
Timo
Subscribe to:
Posts (Atom)