Can I use a computed column in a table to fetch data from another table columns

hilbiazhar

I have two tables : a) BookFine and b) IssuedBook.

In BookFine, I have the columns RegNo and LateFee where I am storing just the Late Fees of delayed Books against the Registration Number of Student.

In IssuedBook I have the columns RegNo, BookTitle, IssueDate and ReturnDate.

I want to achieve this thing involving both tables :

Whenever a student delays a book after the ReturnDate ( Like if ReturnDate < Current Date ) then the LateFee Column in BookFine table should be incremented by 1 where BookFine.RegNo = IssuedBook.RegNo.

I have successfully done this through SQL Job Schedules but I wonder if this is the way it should be done or is there any other good way of doing it. Note that I am using SQL Server 2008 r2.

Thanks.

efreed

The syntax you're looking for is:

UPDATE BookFine 
   SET LateFee = LateFee +1
 WHERE RegNo IN (SELECT RegNo FROM IssuedBook WHERE ReturnDate < CurrentDate)

But you need to handle cases like if there are no records for this student in BookFine, this could be done by first running a prep statement to initialize a zero record. (Putting both of these in a transaction would prevent stray 0 records from being made)

INSERT INTO BookFine (RegNo, LateFee)
SELECT RegNo, 0
  FROM IssuedBook
 WHERE ReturnDate < CurrentDate
   AND RegNo NOT IN (SELECT RegNo FROM BookFine)

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 create Computed Column which gets data from another table

From Dev

SQL computed column for sum of data in another table

From Dev

How to create a computed column that references another column from another table?

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

How can I add columns of data from one table to another from a lookup ID number in CSV/Excel?

From Dev

data.table difference set of columns from another column

From Dev

Procedure to insert data from one column into two columns in another table

From Dev

I need to merge data from 2 columns to another column. And then merge all rows of that added column into one cell in another table

From Dev

How can I calculate dates from another column and another table?

From Dev

How can I calculate dates from another column and another table?

From Dev

Add column to table with data from another table

From Dev

Why I can't directly use the column from another table behind not in function?

From Dev

How can I parse data from one column in a table and put the result into another with SQL Server?

From Dev

How to fetch all ids of a column by matching with multiple columns of another table?

From Dev

Use Spring Data JPA to Find By Column from 1 table and order by column from another table

From Dev

While loop showing only one record when i use nested while loop for fetch data from another table

From Java

How can I add column from one table to another in Clickhouse?

From Dev

Fetch data from Table 1 column's count in Table 2

From Dev

computed column using another table in sql

From Dev

Fetch rows in MySQL which are not present in both columns from another table

From Dev

Fetch rows in MySQL which are not present in both columns from another table

From Dev

Insert data from column of a table and store to column another table

From Dev

Use WHERE clause on a column from another table

From Dev

I want to fetch only single row data from the table with multiple columns

From Dev

I want fetch data from second table if not present in first table

From Dev

MySQL insert into column data from another table

From Dev

Oracle Temporary table columns and data from another table's rows

From Dev

In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

From Dev

In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

Related Related

  1. 1

    How can i create Computed Column which gets data from another table

  2. 2

    SQL computed column for sum of data in another table

  3. 3

    How to create a computed column that references another column from another table?

  4. 4

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  5. 5

    How can I add columns of data from one table to another from a lookup ID number in CSV/Excel?

  6. 6

    data.table difference set of columns from another column

  7. 7

    Procedure to insert data from one column into two columns in another table

  8. 8

    I need to merge data from 2 columns to another column. And then merge all rows of that added column into one cell in another table

  9. 9

    How can I calculate dates from another column and another table?

  10. 10

    How can I calculate dates from another column and another table?

  11. 11

    Add column to table with data from another table

  12. 12

    Why I can't directly use the column from another table behind not in function?

  13. 13

    How can I parse data from one column in a table and put the result into another with SQL Server?

  14. 14

    How to fetch all ids of a column by matching with multiple columns of another table?

  15. 15

    Use Spring Data JPA to Find By Column from 1 table and order by column from another table

  16. 16

    While loop showing only one record when i use nested while loop for fetch data from another table

  17. 17

    How can I add column from one table to another in Clickhouse?

  18. 18

    Fetch data from Table 1 column's count in Table 2

  19. 19

    computed column using another table in sql

  20. 20

    Fetch rows in MySQL which are not present in both columns from another table

  21. 21

    Fetch rows in MySQL which are not present in both columns from another table

  22. 22

    Insert data from column of a table and store to column another table

  23. 23

    Use WHERE clause on a column from another table

  24. 24

    I want to fetch only single row data from the table with multiple columns

  25. 25

    I want fetch data from second table if not present in first table

  26. 26

    MySQL insert into column data from another table

  27. 27

    Oracle Temporary table columns and data from another table's rows

  28. 28

    In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

  29. 29

    In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

HotTag

Archive