Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:10:55+00:00 2026-05-31T11:10:55+00:00

I have two database tables, Team ( ID, NAME, CITY, BOSS, TOTALPLAYER ) and

  • 0

I have two database tables, Team (ID, NAME, CITY, BOSS, TOTALPLAYER) and
Player (ID, NAME, TEAMID, AGE), the relationship between the two tables is one to many, one team can have many players.

I want to know is there a way to define a TOTALPLAYER column in the Team table as computed?

For example, if there are 10 players’ TEAMID is 1, then the row in Team table which ID is 1 has the TOTALPLAYER column with a value of 10. If I add a player, the TOTALPLAYER column’s value goes up to 11, I needn’t to explicitly assign value to it, let it generated by the database. Anyone know how to realize it?

Thx in advance.

BTW, the database is SQL Server 2008 R2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:10:57+00:00Added an answer on May 31, 2026 at 11:10 am

    Yes, you can do that – you need a function to count the players for the team, and use that in the computed column:

    CREATE FUNCTION dbo.CountPlayers (@TeamID INT)
    RETURNS INT 
    AS BEGIN
        DECLARE @PlayerCount INT
    
        SELECT @PlayerCount = COUNT(*) FROM dbo.Player WHERE TeamID = @TeamID
    
        RETURN @PlayerCount
    END
    

    and then define your computed column:

    ALTER TABLE dbo.Team
    ADD TotalPlayers AS dbo.CountPlayers(ID) 
    

    Now if you select, that function is being called every time, for each team being selected. The value is not persisted in the Team table – it’s calculated on the fly each time you select from the Team table.

    Since it’s value isn’t persisted, the question really is: does it need to be a computed column on the table, or could you just use the stored function to compute the number of players, if needed?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in the database Team:Id(int PK), Name(nvarchar) and Player:Id(int PK), Name(nvarchar),
I have two database tables with a one-to-many relationship. Is it possible to select
I have two related tables in my database: Page and Tag. One Page can
I have two database tables, one for Users of a web site, containing the
I have two database tables that have a many to many relationship: People and
I have two table in my database. One called person and one called team.
I have two MySQL database tables: one containing a list of championships and another
I have two database tables. One table stores data for a commitment that a
I have two database tables, one in MYSQL and one in MSSQL. Both have
I have two tables Team_DATA and Driver_PROFILE_DATA in an SQL database. For every driver_profile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.