sql server 2008 - Update Across Tables -


i need set values in database , can't work. (i'm using ssms 2008 r2)

the tables involved dbo.scales , dbo.posscore.

dbo.scales contains 4 columns:

  1. grade (varchar)
  2. upper bound (int)
  3. lower bound (int)
  4. award (varchar)

dbo.posscore has 2 columns:

  1. possible score (int)
  2. grade (varchar)

i need fill dbo.posscore table appropriate grade according bounds given in dbo.scales table.

it best if referential i.e. if change boundaries table adjusts accordingly.

how should go this?

you have 2 options if want values in posscore change when alter values in scales: either create trigger on scales (re-)creates posscore table whenever needed (or updates it), or use view changes dynamically. unless amount of data needs change prohibitively large , recalculating posscore values takes long view should best option.

one way create view use suitable table number range covers upper , lower bounds. fortunately sql server has system table can used purpose (provided range falls in span 0-2047.

a query create view below:

create view posscore select s.grade, number "possible score" master..spt_values v join scales s on v.number <= s.upperbound , v.number >= s.lowerbound , v.type = 'p' 

sql fiddle demo


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -