How can I automatically update all other rows in the table after insert one new one in sql?

zapatilla

I have a table like this where I keep a list of appointments which increment based on the combination of A and B:

ID  A   B  Appointment  Count
-----------------------------
1  abc  0  2010-10-20   1
2  abc  0  2010-10-25   2
3  abc  0  2010-10-30   3
3  abc  1  2010-10-30   1
4  xyz  1  2010-08-18   1
5  xyz  1  2010-08-19   2
6  xyz  1  2010-08-20   3

And a function like this:

CREATE FUNCTION dbo.GenerateCount
(
   @id int,
   @A int,
   @B int,
   @appt_date date

)
RETURNS Int
AS
BEGIN

   RETURN
   (
       SELECT COUNT(*)
       FROM dbo.test_seq
       WHERE patient_id = @A
       AND B = @B
       AND id <= @id
       AND appt_date <= @appt_date
   )
END

With data inserted like this:

CREATE TABLE [dbo].[test_seq](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [A] [int] NOT NULL,
    [B] [int] NOT NULL,
    [appt_date] [date] NOT NULL,
    [count] AS dbo.GenerateCount(id, A, B, appt_date)
    )

When I insert a new entry in the table, it increments the count as expected. However if I insert a new entry with a date in the middle, say if I want to add:

ID  A   B  Appointment  Count
-----------------------------
1  abc  0  2010-10-21   

it has the correct count assigned, but the other rows don't get updated. How can I trigger a table update for all the other records after that date so they are corrected with the relevant count values?

I tried creating a trigger on insert/update/delete, but that only applies to the row being inserted and not the whole table.

Shnugo

If I get this correctly, the simple answer is: Don't!

A SQL-Server-table is not Excel...

You must decide

  • Do you want to set a value persistantly (in other words: a kind of key)?
  • Do you just want to number the rows for the moment?

Create a VIEW upon your table (according to the approach you find in your last question).

This will compute the correct numbers whenever you call that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i use SQL Select Insert to copy rows from one table to another

From Dev

With SQL Server, how can I update all rows but each one differently based on subquery

From Dev

SQL join including all rows from one table irrespective of how many are represented in the other table

From Dev

How to use SQL TRIGGER to insert rows from another table into a new one?

From Dev

If I want to insert many records in one record from other table, how can I do that?

From Dev

How can I update all rows beside the one I'm clicking in a ListView?

From Dev

How can I update all rows beside the one I'm clicking in a ListView?

From Dev

How to Update Two table Data one after other In codeigniter?

From Dev

How to Update Two table Data one after other In codeigniter?

From Dev

Insert in one table automatically inserts in other table first (PostgreSQL)

From Dev

Update all rows with one SQL query

From Dev

How can I remove one row from a table and insert it into another via a SQL statement?

From Dev

How can I remove one row from a table and insert it into another via a SQL statement?

From Dev

How can I merge rows and columns from two sql tables into one table

From Dev

How to insert into table based on other one?

From Dev

How to insert into table based on other one?

From Dev

How can I insert data one table and update some data in another table in asp.net mvc 5 & Entity Framework

From Java

Can we insert in one table and trigger to update the inserted row's one column by looking at other table's value for the id being inserted?

From Dev

SQL Change in one table automatically change the other table

From Dev

mysql - If I update one row, all the other rows (including updated row) return updated value

From Dev

How can I update multiple rows in a table with SQL query?

From Dev

How to update SQL Server table rows one by one using c#

From Dev

How to update SQL Server table rows one by one using c#

From Dev

How can I create 2 different lists, one has all evens, the other all odds in One statement?

From Dev

How I Can Merge Mutiple Rows into One in SQL Server

From Dev

How can I concat these two rows into one column in SQL

From Dev

Yii2 How can i set the db config in new Query without use all(), one() or other execution method?

From Dev

How can I collect all my types into one table in laravel?

From Dev

How to only insert one column into a SQL table?

Related Related

  1. 1

    How can i use SQL Select Insert to copy rows from one table to another

  2. 2

    With SQL Server, how can I update all rows but each one differently based on subquery

  3. 3

    SQL join including all rows from one table irrespective of how many are represented in the other table

  4. 4

    How to use SQL TRIGGER to insert rows from another table into a new one?

  5. 5

    If I want to insert many records in one record from other table, how can I do that?

  6. 6

    How can I update all rows beside the one I'm clicking in a ListView?

  7. 7

    How can I update all rows beside the one I'm clicking in a ListView?

  8. 8

    How to Update Two table Data one after other In codeigniter?

  9. 9

    How to Update Two table Data one after other In codeigniter?

  10. 10

    Insert in one table automatically inserts in other table first (PostgreSQL)

  11. 11

    Update all rows with one SQL query

  12. 12

    How can I remove one row from a table and insert it into another via a SQL statement?

  13. 13

    How can I remove one row from a table and insert it into another via a SQL statement?

  14. 14

    How can I merge rows and columns from two sql tables into one table

  15. 15

    How to insert into table based on other one?

  16. 16

    How to insert into table based on other one?

  17. 17

    How can I insert data one table and update some data in another table in asp.net mvc 5 & Entity Framework

  18. 18

    Can we insert in one table and trigger to update the inserted row's one column by looking at other table's value for the id being inserted?

  19. 19

    SQL Change in one table automatically change the other table

  20. 20

    mysql - If I update one row, all the other rows (including updated row) return updated value

  21. 21

    How can I update multiple rows in a table with SQL query?

  22. 22

    How to update SQL Server table rows one by one using c#

  23. 23

    How to update SQL Server table rows one by one using c#

  24. 24

    How can I create 2 different lists, one has all evens, the other all odds in One statement?

  25. 25

    How I Can Merge Mutiple Rows into One in SQL Server

  26. 26

    How can I concat these two rows into one column in SQL

  27. 27

    Yii2 How can i set the db config in new Query without use all(), one() or other execution method?

  28. 28

    How can I collect all my types into one table in laravel?

  29. 29

    How to only insert one column into a SQL table?

HotTag

Archive