问题
Have a very odd situation. For various reasons we have a bunch of tables where this was done:
TableA was renamed to TableASource and a view was created called TableA TableB was renamed to TableBSource and a view was created called TableB
This all works fine and has done for a fair while. Yesterday I added a new field to TableASource, TableBSource (and the others). This was a field called 'createDate' a smallDateTime, no nulls with a default value of getDate(). You would think this should have zero impact on anything. Today first thing users are saying they can see these dates inplace of the intended data. E.G Lest says we had a page extracting 10 fields out of the view called TableA, instead of showing these 10 fields its now showing 9 correctly and randomly showing the value in the new createDate field that was added to TableASource (If it matters it placing it in the second field on the page)
I quickly dropped all these new createDate fields and the issue went away. How can this possibly happen? How can adding a field to TableASource effect the results of the TableA view?
Using SQLServer2008 r2
in response the the extra questions below. Here is the view called 'bayTrainCourses' which has been described above as TableA :
SELECT btc.course_id, btc.course_name, btc.course_status, btc.course_expiry, btc.course_startdate, btc.course_enddate, btc.course_type, btc.site_id, btc.channelid, btc.exam_type, btc.enroll_type, btc.last_updated, btc.compid, btc.TOC, btc.rdone, btc.autoqualify, btc.courseIntroId, btc.coursePurposeId, btc.courseBackgroundId,btc.courseObjectivesId, intro. [content] AS course_intro, purpose.[content] AS course_purpose, background.[content] AS course_background,
objectives.[content] AS course_objectives
FROM dbo.bayTrainCoursesSource AS btc
LEFT OUTER JOIN dbo.RTE AS intro ON btc.courseIntroId = intro.pk
LEFT OUTER JOIN dbo.RTE AS purpose ON btc.coursePurposeId = purpose.pk
LEFT OUTER JOIN dbo.RTE AS background ON btc.courseBackgroundId = background.pk
LEFT OUTER JOIN dbo.RTE AS objectives ON btc.courseObjectivesId = objectives.pk
The select is:
select * from baytraincourses....
EDITING - due to some trial an error. It turns out that the trial was actually the advice from JohnS below. Recreate the view.... I renamed the view to baytraincourses_broken. Then right click > script view as > create to > then I changed 'baytraincourses_broken' to 'baytraincourses' to recreate another version with the original name. Now the page works.
I am not a big fan of views. Its very rare I create them to be honest, I am working with code that I did not originally write. Do I really have to re-create views every time I add a new column? Surely not, how can I deal with this?
回答1:
After adding the new field to TableASource you need to recreate the view. (i.e. DROP VIEW TableA ... then CREATE VIEW TableA ...).
来源:https://stackoverflow.com/questions/28552763/adding-a-field-somehow-effects-a-views-results