Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.
-
Ioannis Demetriades #1
Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database;
Hi,
I am testing a trigger that and I am getting this error message saying
"Another user has modified the contents of this table or view; the database
row you are modifying no longer exists in the database"
What I am trying to do through the trigger is not to allow the creation of a
new session for which there exists another session with the same cashierid
and closingtime is NULL.
Thanks
Ioannis
CREATE TABLE [dbo].[Sessions] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[UserName] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[OpeningTime] [datetime] NOT NULL ,
[OpeningBalance] [decimal](12, 2) NOT NULL ,
[ClosingBalance] [decimal](12, 2) NULL ,
[CalculatedBalance] [decimal](12, 2) NOT NULL ,
[ClosingTime] [datetime] NULL ,
[CashierId] [int] NULL
) ON [PRIMARY]
CREATE TRIGGER SessionINSERT ON [dbo].[Sessions]
FOR INSERT, UPDATE
AS
BEGIN
Declare @Count Integer
Declare @ErrorMsg VarChar(100)
Declare MyCursor CURSOR For Select id,UserName,OpeningBalance,CashierId
From Inserted
Declare @Id Int
Declare @UserName Char(20)
Declare @OpeningBalance Decimal(12,2)
Declare @cId Int
Open MyCursor
Fetch Next From MyCursor
Into @id,@UserName,@OpeningBalance,@cId
WHILE @@Fetch_Status <> -1
BEGIN
Print @id
Print @UserName
Print @OpeningBalance
Print @cId
Fetch Next From MyCursor
Into @id,@UserName,@OpeningBalance,@cId
END
Close MyCursor
Deallocate MyCursor
/*
Select @Count = count(*) From Inserted
Set @ErrorMsg = 'Count=' + convert(varchar(100),@Count)
Raiserror (@errormsg,16,1)
*/
If UPDATE(CashierId)
BEGIN
Declare @CashierId Int
Declare @OpeningTime DateTime
Select @CashierId = s.CashierId, @UserName = s.UserName, @OpeningTime =
s.OpeningTime
From Sessions s, Inserted I
Where s.CashierId = i.CashierId
And s.ClosingTime Is NULL
And i.ClosingTime Is NULL
And s.Id <> i.Id
If @CashierId is not null
BEGIN
ROLLBACK TRANSACTION
Declare @CashierName Char(20)
Select @CashierName=Description
From Cashiers
Where id = @CashierId
Set @ErrorMsg = 'Cashier ''' + RTrim(@CashierName) + ''' is currently
open by ' + RTrim(@UserName) + ' [' + Convert(varchar(20),@OpeningTime) +
']'
RAISERROR (@ErrorMsg,16, 1)
END
END
END
Ioannis Demetriades Guest
-
User Database Table
I am trying to follow the Matrix guided tour for creating a Users Database table. After creating the table, directions indicate to highlite the... -
#24932 [Fbk->NoF]: pg_lo_read_all returns a modified large object from database
ID: 24932 Updated by: sniper@php.net Reported By: leitgebj at barada dot canisius dot edu -Status: Feedback... -
#24932 [NEW]: pg_lo_read_all returns a modified large object from database
From: leitgebj at barada dot canisius dot edu Operating system: Mac OS X Darwin Kernel 6.3 PHP version: 4.3.2 PHP Bug Type: ... -
Modifying a Database query
I would like to know if there is a way to track modifications to an invoice. I am using Order entry Database format. At any time, the orders/order... -
Passing database info to page allow user input then pass into another database
Hi I really am going crazy! I'm using VBScript, ASP, and SQL My page reminds me of a shopping cart but looking at shopping cart examples has not... -
Mandar Naik[MSFT] #2
Re: Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database;
Hi,
My understanding of your requirement is as follows:-
if there is the same cashierID in the table without a ClosingTime, then the
new INSERT should be rolled back.
Please check teh following code. I have tested it on a sample table and it
works fine. Do get back if you need any clarifications on the same.
CREATE TRIGGER SessionCheck ON dbo.Sessions
FOR INSERT
AS
BEGIN
DECLARE @sessionID int
DECLARE @count int
SELECT @sessionID = cashierID FROM INSERTED
SELECT @count = count(*) FROM Sessions
WHERE cashierID = @sessionID and
ClosingTime IS NULL
IF @count > 1
BEGIN
PRINT 'SESSION ALREADY EXISTS'
ROLLBACK TRANSACTION
END
END
--
Regards,
Mandar Naik
This posting is provided AS IS with no warranties, and confers no rights.
"Ioannis Demetriades" <idemetriades@yahoo.co.uk> wrote in message
news:ekzYF8vPDHA.2852@tk2msftngp13.phx.gbl...database> Hi,
>
> I am testing a trigger that and I am getting this error message saying
>
> "Another user has modified the contents of this table or view; thea> row you are modifying no longer exists in the database"
>
> What I am trying to do through the trigger is not to allow the creation of> new session for which there exists another session with the same cashierid
> and closingtime is NULL.
>
> Thanks
> Ioannis
>
>
> CREATE TABLE [dbo].[Sessions] (
> [id] [int] IDENTITY (1, 1) NOT NULL ,
> [UserName] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [OpeningTime] [datetime] NOT NULL ,
> [OpeningBalance] [decimal](12, 2) NOT NULL ,
> [ClosingBalance] [decimal](12, 2) NULL ,
> [CalculatedBalance] [decimal](12, 2) NOT NULL ,
> [ClosingTime] [datetime] NULL ,
> [CashierId] [int] NULL
> ) ON [PRIMARY]
>
>
>
> CREATE TRIGGER SessionINSERT ON [dbo].[Sessions]
> FOR INSERT, UPDATE
> AS
>
> BEGIN
> Declare @Count Integer
> Declare @ErrorMsg VarChar(100)
> Declare MyCursor CURSOR For Select id,UserName,OpeningBalance,CashierId
> From Inserted
>
> Declare @Id Int
> Declare @UserName Char(20)
> Declare @OpeningBalance Decimal(12,2)
> Declare @cId Int
>
>
> Open MyCursor
> Fetch Next From MyCursor
> Into @id,@UserName,@OpeningBalance,@cId
>
> WHILE @@Fetch_Status <> -1
> BEGIN
> Print @id
> Print @UserName
> Print @OpeningBalance
> Print @cId
>
> Fetch Next From MyCursor
> Into @id,@UserName,@OpeningBalance,@cId
> END
> Close MyCursor
> Deallocate MyCursor
>
> /*
> Select @Count = count(*) From Inserted
> Set @ErrorMsg = 'Count=' + convert(varchar(100),@Count)
>
> Raiserror (@errormsg,16,1)
> */
>
> If UPDATE(CashierId)
> BEGIN
> Declare @CashierId Int
> Declare @OpeningTime DateTime
>
>
> Select @CashierId = s.CashierId, @UserName = s.UserName, @OpeningTime =
> s.OpeningTime
> From Sessions s, Inserted I
> Where s.CashierId = i.CashierId
> And s.ClosingTime Is NULL
> And i.ClosingTime Is NULL
> And s.Id <> i.Id
>
>
> If @CashierId is not null
> BEGIN
> ROLLBACK TRANSACTION
>
> Declare @CashierName Char(20)
>
> Select @CashierName=Description
> From Cashiers
> Where id = @CashierId
>
>
> Set @ErrorMsg = 'Cashier ''' + RTrim(@CashierName) + ''' is currently
> open by ' + RTrim(@UserName) + ' [' + Convert(varchar(20),@OpeningTime) +
> ']'
>
> RAISERROR (@ErrorMsg,16, 1)
> END
> END
> END
>
>
>
>
>
>
>
>
>
>
>
>
Mandar Naik[MSFT] Guest
-
Ioannis Demetriades #3
Re: Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database;
Hi Mandar,
Your trigger works fine. However I still have to take care of any updates on
"Sessions.CashiersId". Do you know what caused this error message in my
original trigger?
Thanks
"Mandar Naik[MSFT]" <mandarn@online.microsoft.com> wrote in message
news:%234PFeQwPDHA.4024@tk2msftngp13.phx.gbl...the> Hi,
>
> My understanding of your requirement is as follows:-
> if there is the same cashierID in the table without a ClosingTime, thenof> new INSERT should be rolled back.
>
> Please check teh following code. I have tested it on a sample table and it
> works fine. Do get back if you need any clarifications on the same.
>
> CREATE TRIGGER SessionCheck ON dbo.Sessions
> FOR INSERT
> AS
> BEGIN
> DECLARE @sessionID int
> DECLARE @count int
> SELECT @sessionID = cashierID FROM INSERTED
> SELECT @count = count(*) FROM Sessions
> WHERE cashierID = @sessionID and
> ClosingTime IS NULL
> IF @count > 1
> BEGIN
> PRINT 'SESSION ALREADY EXISTS'
> ROLLBACK TRANSACTION
> END
> END
>
> --
> Regards,
> Mandar Naik
>
>
> This posting is provided AS IS with no warranties, and confers no rights.
> "Ioannis Demetriades" <idemetriades@yahoo.co.uk> wrote in message
> news:ekzYF8vPDHA.2852@tk2msftngp13.phx.gbl...> database> > Hi,
> >
> > I am testing a trigger that and I am getting this error message saying
> >
> > "Another user has modified the contents of this table or view; the> > row you are modifying no longer exists in the database"
> >
> > What I am trying to do through the trigger is not to allow the creationcashierid> a> > new session for which there exists another session with the same=> > and closingtime is NULL.
> >
> > Thanks
> > Ioannis
> >
> >
> > CREATE TABLE [dbo].[Sessions] (
> > [id] [int] IDENTITY (1, 1) NOT NULL ,
> > [UserName] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> > [OpeningTime] [datetime] NOT NULL ,
> > [OpeningBalance] [decimal](12, 2) NOT NULL ,
> > [ClosingBalance] [decimal](12, 2) NULL ,
> > [CalculatedBalance] [decimal](12, 2) NOT NULL ,
> > [ClosingTime] [datetime] NULL ,
> > [CashierId] [int] NULL
> > ) ON [PRIMARY]
> >
> >
> >
> > CREATE TRIGGER SessionINSERT ON [dbo].[Sessions]
> > FOR INSERT, UPDATE
> > AS
> >
> > BEGIN
> > Declare @Count Integer
> > Declare @ErrorMsg VarChar(100)
> > Declare MyCursor CURSOR For Select id,UserName,OpeningBalance,CashierId
> > From Inserted
> >
> > Declare @Id Int
> > Declare @UserName Char(20)
> > Declare @OpeningBalance Decimal(12,2)
> > Declare @cId Int
> >
> >
> > Open MyCursor
> > Fetch Next From MyCursor
> > Into @id,@UserName,@OpeningBalance,@cId
> >
> > WHILE @@Fetch_Status <> -1
> > BEGIN
> > Print @id
> > Print @UserName
> > Print @OpeningBalance
> > Print @cId
> >
> > Fetch Next From MyCursor
> > Into @id,@UserName,@OpeningBalance,@cId
> > END
> > Close MyCursor
> > Deallocate MyCursor
> >
> > /*
> > Select @Count = count(*) From Inserted
> > Set @ErrorMsg = 'Count=' + convert(varchar(100),@Count)
> >
> > Raiserror (@errormsg,16,1)
> > */
> >
> > If UPDATE(CashierId)
> > BEGIN
> > Declare @CashierId Int
> > Declare @OpeningTime DateTime
> >
> >
> > Select @CashierId = s.CashierId, @UserName = s.UserName, @OpeningTimecurrently> > s.OpeningTime
> > From Sessions s, Inserted I
> > Where s.CashierId = i.CashierId
> > And s.ClosingTime Is NULL
> > And i.ClosingTime Is NULL
> > And s.Id <> i.Id
> >
> >
> > If @CashierId is not null
> > BEGIN
> > ROLLBACK TRANSACTION
> >
> > Declare @CashierName Char(20)
> >
> > Select @CashierName=Description
> > From Cashiers
> > Where id = @CashierId
> >
> >
> > Set @ErrorMsg = 'Cashier ''' + RTrim(@CashierName) + ''' is+> > open by ' + RTrim(@UserName) + ' [' + Convert(varchar(20),@OpeningTime)>> > ']'
> >
> > RAISERROR (@ErrorMsg,16, 1)
> > END
> > END
> > END
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
Ioannis Demetriades Guest
-
Mandar Naik[MSFT] #4
Re: Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database;
Hi,
Regarding the update check, you will need a seperate trigger FOR UPDATE
where you can use the similar logic based on the updated column.
I am adding the code here. It works in my scenario. Please revert if you
have any queries.
CREATE TRIGGER SessionCheckUpd ON dbo.Sessions
FOR UPDATE
AS
BEGIN
DECLARE @sessionID int
DECLARE @count int
If UPDATE(SessionID)
BEGIN
SELECT @sessionID = CashiersId FROM INSERTED
SELECT @count = count(*) FROM Sessions
WHERE CashiersId= @sessionID and ClosingTime iS NULL
IF @count > 1
BEGIN
PRINT 'SESSION ALREADY EXISTS'
ROLLBACK TRANSACTION
END
END
END
--
Regards,
Mandar Naik
This posting is provided AS IS with no warranties, and confers no rights.
"Ioannis Demetriades" <idemetriades@yahoo.co.uk> wrote in message
news:eV##aZwPDHA.3236@TK2MSFTNGP10.phx.gbl...on> Hi Mandar,
>
> Your trigger works fine. However I still have to take care of any updatesit> "Sessions.CashiersId". Do you know what caused this error message in my
> original trigger?
>
> Thanks
>
>
> "Mandar Naik[MSFT]" <mandarn@online.microsoft.com> wrote in message
> news:%234PFeQwPDHA.4024@tk2msftngp13.phx.gbl...> the> > Hi,
> >
> > My understanding of your requirement is as follows:-
> > if there is the same cashierID in the table without a ClosingTime, then> > new INSERT should be rolled back.
> >
> > Please check teh following code. I have tested it on a sample table andrights.> > works fine. Do get back if you need any clarifications on the same.
> >
> > CREATE TRIGGER SessionCheck ON dbo.Sessions
> > FOR INSERT
> > AS
> > BEGIN
> > DECLARE @sessionID int
> > DECLARE @count int
> > SELECT @sessionID = cashierID FROM INSERTED
> > SELECT @count = count(*) FROM Sessions
> > WHERE cashierID = @sessionID and
> > ClosingTime IS NULL
> > IF @count > 1
> > BEGIN
> > PRINT 'SESSION ALREADY EXISTS'
> > ROLLBACK TRANSACTION
> > END
> > END
> >
> > --
> > Regards,
> > Mandar Naik
> >
> >
> > This posting is provided AS IS with no warranties, and confers nocreation> > "Ioannis Demetriades" <idemetriades@yahoo.co.uk> wrote in message
> > news:ekzYF8vPDHA.2852@tk2msftngp13.phx.gbl...> > database> > > Hi,
> > >
> > > I am testing a trigger that and I am getting this error message saying
> > >
> > > "Another user has modified the contents of this table or view; the> > > row you are modifying no longer exists in the database"
> > >
> > > What I am trying to do through the trigger is not to allow the,> of> cashierid> > a> > > new session for which there exists another session with the same> > > and closingtime is NULL.
> > >
> > > Thanks
> > > Ioannis
> > >
> > >
> > > CREATE TABLE [dbo].[Sessions] (
> > > [id] [int] IDENTITY (1, 1) NOT NULL ,
> > > [UserName] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULLid,UserName,OpeningBalance,CashierId> > > [OpeningTime] [datetime] NOT NULL ,
> > > [OpeningBalance] [decimal](12, 2) NOT NULL ,
> > > [ClosingBalance] [decimal](12, 2) NULL ,
> > > [CalculatedBalance] [decimal](12, 2) NOT NULL ,
> > > [ClosingTime] [datetime] NULL ,
> > > [CashierId] [int] NULL
> > > ) ON [PRIMARY]
> > >
> > >
> > >
> > > CREATE TRIGGER SessionINSERT ON [dbo].[Sessions]
> > > FOR INSERT, UPDATE
> > > AS
> > >
> > > BEGIN
> > > Declare @Count Integer
> > > Declare @ErrorMsg VarChar(100)
> > > Declare MyCursor CURSOR For Select@OpeningTime> > > From Inserted
> > >
> > > Declare @Id Int
> > > Declare @UserName Char(20)
> > > Declare @OpeningBalance Decimal(12,2)
> > > Declare @cId Int
> > >
> > >
> > > Open MyCursor
> > > Fetch Next From MyCursor
> > > Into @id,@UserName,@OpeningBalance,@cId
> > >
> > > WHILE @@Fetch_Status <> -1
> > > BEGIN
> > > Print @id
> > > Print @UserName
> > > Print @OpeningBalance
> > > Print @cId
> > >
> > > Fetch Next From MyCursor
> > > Into @id,@UserName,@OpeningBalance,@cId
> > > END
> > > Close MyCursor
> > > Deallocate MyCursor
> > >
> > > /*
> > > Select @Count = count(*) From Inserted
> > > Set @ErrorMsg = 'Count=' + convert(varchar(100),@Count)
> > >
> > > Raiserror (@errormsg,16,1)
> > > */
> > >
> > > If UPDATE(CashierId)
> > > BEGIN
> > > Declare @CashierId Int
> > > Declare @OpeningTime DateTime
> > >
> > >
> > > Select @CashierId = s.CashierId, @UserName = s.UserName,Convert(varchar(20),@OpeningTime)> => currently> > > s.OpeningTime
> > > From Sessions s, Inserted I
> > > Where s.CashierId = i.CashierId
> > > And s.ClosingTime Is NULL
> > > And i.ClosingTime Is NULL
> > > And s.Id <> i.Id
> > >
> > >
> > > If @CashierId is not null
> > > BEGIN
> > > ROLLBACK TRANSACTION
> > >
> > > Declare @CashierName Char(20)
> > >
> > > Select @CashierName=Description
> > > From Cashiers
> > > Where id = @CashierId
> > >
> > >
> > > Set @ErrorMsg = 'Cashier ''' + RTrim(@CashierName) + ''' is> > > open by ' + RTrim(@UserName) + ' [' +> +>> >> > > ']'
> > >
> > > RAISERROR (@ErrorMsg,16, 1)
> > > END
> > > END
> > > END
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> >
>
Mandar Naik[MSFT] Guest
-
Ioannis Demetriades #5
Re: Another user has modified the contents of this table or view; the database row you are modifying no longer exists in the database;
Hi Mandar,
Thanks for your advice. I took your code and made some minor modifications
and I noticed that when i run INSERT and UPDATE sql statements the triggers
behave as expected. It's only when I try to modify the data through the
Enterprise manager that I get this message. My question is whether i am
doing something fundamentally wrong with the triggers or is it something
that has to do with the cursor that is used to fetch the data in the
enterprise manager? Where can i get some info about this error message?
Thanks
--- CODE -------------
CREATE TABLE [dbo].[Sessions] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[UserName] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[OpeningTime] [datetime] NOT NULL ,
[OpeningBalance] [decimal](12, 2) NOT NULL ,
[ClosingBalance] [decimal](12, 2) NULL ,
[CalculatedBalance] [decimal](12, 2) NOT NULL ,
[ClosingTime] [datetime] NULL ,
[CashierId] [int] NULL
) ON [PRIMARY]
drop trigger sessionINS
go
CREATE TRIGGER SessionINS ON dbo.Sessions
FOR INSERT
AS
BEGIN
/*
Print 'INSERT Trigger'
*/
DECLARE @iCashierId int
DECLARE @count int
Declare @iClosingTime DateTime
SELECT @iCashierId = cashierID, @iClosingTime = ClosingTime
FROM INSERTED
If @iClosingTime Is not NULL
Return
SELECT @count = count(*) FROM Sessions
WHERE cashierID = @iCashierId and
ClosingTime IS NULL
IF @count > 1
BEGIN
Raiserror ('SESSION ALREADY EXISTS (INSERT)',16,1)
ROLLBACK TRANSACTION
END
END
CREATE TRIGGER SessionUPD ON dbo.Sessions
FOR UPDATE
AS
BEGIN
DECLARE @iCashierId int
DECLARE @Count int
Declare @iClosingTime DateTime
/*
Print 'UPDATE Trigger'
*/
If UPDATE(CashierId)
BEGIN
/*
Print 'UPDATE Trigger: Updating CashierId'
*/
SELECT @iClosingTime = ClosingTime, @iCashierId = CashierId
FROM INSERTED
If @iClosingTime Is NOT NULL
Return
SELECT @count = count(*)
FROM Sessions
WHERE CashierId= @iCashierId
And ClosingTime Is NULL
IF @count > 1
BEGIN
Raiserror ('SESSION ALREADY EXISTS (UPDATE)',16,1)
ROLLBACK TRANSACTION
END
END
END
Ioannis Demetriades Guest



Reply With Quote

