SQL Server, VIEW has mixed up data










0














My team and I are using Microsoft SQL Server 2014 - Standard Edition (64-bit)



We have created some views as we usually do, they work(ed) normally while we were coding and testing.



Then suddenly, one of our QA noticed that the data in the application was mixed up, for example, description data was in the name field, name was where sex was supposed to be, etc.



We cheched data in the Tables and it was correct, then we checked the view, querying the view like this SELECT * FROM VIEW and realized that the view had the data mixed up.. The next logic step was to check the view queries, for our surprise all the queries were correct. so what was happening?



Well, that is the question, why the data in a view is corrupt or mixed up if the queries within the view are correct and they were working well for long time?



We just ALTERED the view, not modifying anything and that fixed the issue.



But, we need to know the cause of the data corruption, because we don't want to monitor and alter views all the time.



VIEW CODE AS REQUESTED



ALTER VIEW [dbo].[pvvClient] AS 
SELECT *
FROM Table
INNER JOIN Table 2 ON.....


The first thing that came to my mind was that the Table(s) has changed and that raised this behavior, do you think SCHEMABINDING can help to avoid this kind of issues










share|improve this question



















  • 2




    Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
    – Bridge
    Sep 12 '17 at 16:23










  • Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
    – Dan Bracuk
    Sep 12 '17 at 16:33










  • @DanBracuk no, it doesn't, it is even referenced in our EF model
    – Victor Hugo Terceros
    Sep 12 '17 at 16:35










  • Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
    – xQbert
    Sep 12 '17 at 16:55






  • 1




    Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
    – Sean Lange
    Sep 12 '17 at 18:48















0














My team and I are using Microsoft SQL Server 2014 - Standard Edition (64-bit)



We have created some views as we usually do, they work(ed) normally while we were coding and testing.



Then suddenly, one of our QA noticed that the data in the application was mixed up, for example, description data was in the name field, name was where sex was supposed to be, etc.



We cheched data in the Tables and it was correct, then we checked the view, querying the view like this SELECT * FROM VIEW and realized that the view had the data mixed up.. The next logic step was to check the view queries, for our surprise all the queries were correct. so what was happening?



Well, that is the question, why the data in a view is corrupt or mixed up if the queries within the view are correct and they were working well for long time?



We just ALTERED the view, not modifying anything and that fixed the issue.



But, we need to know the cause of the data corruption, because we don't want to monitor and alter views all the time.



VIEW CODE AS REQUESTED



ALTER VIEW [dbo].[pvvClient] AS 
SELECT *
FROM Table
INNER JOIN Table 2 ON.....


The first thing that came to my mind was that the Table(s) has changed and that raised this behavior, do you think SCHEMABINDING can help to avoid this kind of issues










share|improve this question



















  • 2




    Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
    – Bridge
    Sep 12 '17 at 16:23










  • Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
    – Dan Bracuk
    Sep 12 '17 at 16:33










  • @DanBracuk no, it doesn't, it is even referenced in our EF model
    – Victor Hugo Terceros
    Sep 12 '17 at 16:35










  • Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
    – xQbert
    Sep 12 '17 at 16:55






  • 1




    Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
    – Sean Lange
    Sep 12 '17 at 18:48













0












0








0







My team and I are using Microsoft SQL Server 2014 - Standard Edition (64-bit)



We have created some views as we usually do, they work(ed) normally while we were coding and testing.



Then suddenly, one of our QA noticed that the data in the application was mixed up, for example, description data was in the name field, name was where sex was supposed to be, etc.



We cheched data in the Tables and it was correct, then we checked the view, querying the view like this SELECT * FROM VIEW and realized that the view had the data mixed up.. The next logic step was to check the view queries, for our surprise all the queries were correct. so what was happening?



Well, that is the question, why the data in a view is corrupt or mixed up if the queries within the view are correct and they were working well for long time?



We just ALTERED the view, not modifying anything and that fixed the issue.



But, we need to know the cause of the data corruption, because we don't want to monitor and alter views all the time.



VIEW CODE AS REQUESTED



ALTER VIEW [dbo].[pvvClient] AS 
SELECT *
FROM Table
INNER JOIN Table 2 ON.....


The first thing that came to my mind was that the Table(s) has changed and that raised this behavior, do you think SCHEMABINDING can help to avoid this kind of issues










share|improve this question















My team and I are using Microsoft SQL Server 2014 - Standard Edition (64-bit)



We have created some views as we usually do, they work(ed) normally while we were coding and testing.



Then suddenly, one of our QA noticed that the data in the application was mixed up, for example, description data was in the name field, name was where sex was supposed to be, etc.



We cheched data in the Tables and it was correct, then we checked the view, querying the view like this SELECT * FROM VIEW and realized that the view had the data mixed up.. The next logic step was to check the view queries, for our surprise all the queries were correct. so what was happening?



Well, that is the question, why the data in a view is corrupt or mixed up if the queries within the view are correct and they were working well for long time?



We just ALTERED the view, not modifying anything and that fixed the issue.



But, we need to know the cause of the data corruption, because we don't want to monitor and alter views all the time.



VIEW CODE AS REQUESTED



ALTER VIEW [dbo].[pvvClient] AS 
SELECT *
FROM Table
INNER JOIN Table 2 ON.....


The first thing that came to my mind was that the Table(s) has changed and that raised this behavior, do you think SCHEMABINDING can help to avoid this kind of issues







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 12 '17 at 19:05

























asked Sep 12 '17 at 16:21









Victor Hugo Terceros

1,6932618




1,6932618







  • 2




    Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
    – Bridge
    Sep 12 '17 at 16:23










  • Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
    – Dan Bracuk
    Sep 12 '17 at 16:33










  • @DanBracuk no, it doesn't, it is even referenced in our EF model
    – Victor Hugo Terceros
    Sep 12 '17 at 16:35










  • Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
    – xQbert
    Sep 12 '17 at 16:55






  • 1




    Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
    – Sean Lange
    Sep 12 '17 at 18:48












  • 2




    Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
    – Bridge
    Sep 12 '17 at 16:23










  • Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
    – Dan Bracuk
    Sep 12 '17 at 16:33










  • @DanBracuk no, it doesn't, it is even referenced in our EF model
    – Victor Hugo Terceros
    Sep 12 '17 at 16:35










  • Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
    – xQbert
    Sep 12 '17 at 16:55






  • 1




    Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
    – Sean Lange
    Sep 12 '17 at 18:48







2




2




Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
– Bridge
Sep 12 '17 at 16:23




Does your view contain * at all? Please paste the content of the view. I bet you have missed some commas, and so some of the columns are being interpretted as aliases of other columns instead.
– Bridge
Sep 12 '17 at 16:23












Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
– Dan Bracuk
Sep 12 '17 at 16:33




Does table pvtConsumidorFinanciero contain any fields with the same name as the other fields/aliases in the select clause?
– Dan Bracuk
Sep 12 '17 at 16:33












@DanBracuk no, it doesn't, it is even referenced in our EF model
– Victor Hugo Terceros
Sep 12 '17 at 16:35




@DanBracuk no, it doesn't, it is even referenced in our EF model
– Victor Hugo Terceros
Sep 12 '17 at 16:35












Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
– xQbert
Sep 12 '17 at 16:55




Avoid using .* ask yourself, how does the database know what order to return * in? would it change if the tables are altered? could it vary by environment? Since * could be 5 columns now and 6 later what would be the impact downstream?
– xQbert
Sep 12 '17 at 16:55




1




1




Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
– Sean Lange
Sep 12 '17 at 18:48




Sure here is a great article that discusses this. sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/… It is certainly well known and discussed that you should avoid using select * unless it is in an exists clause.
– Sean Lange
Sep 12 '17 at 18:48












3 Answers
3






active

oldest

votes


















1














When you put * in the column list of a view and the underlying tables change your view will not automatically update to include the changed columns. In fact, if you delete a column you can get the data mixed up across columns. This has been discussed and documented many times. Aaron Bertrand has a great article covering this topic.



  • Bad habits to kick : using SELECT * / omitting the column list

Moral of the story, avoid using select * unless the select is inside an EXISTS.






share|improve this answer






























    2














    It doesn't make sense that simply altering the view without changing any of the code would fix it, but an important point is this part of your view...



    select pvtConsumidorFinanciero.*



    If this table definition changes... that is, if more columns are added or some are removed, the columns in this view would also change. That is why it is good practice to never select * in a view, especially when querying another view.



    Additionally, this table could have the same column names as other tables.



    What also could have happened is in your application, you are select * from view. Again, if a DBA changed the view, this could mess up your application, so i would avoid it an explicitly list the columns you want returned in the order you want them returned.






    share|improve this answer




























      0














      I think this is is also part of the answer:



      When you create views it is a good practice to use SCHEMABINDING, this way when you alter the table under the view, you are forced to review your view as well.






      share|improve this answer




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46181318%2fsql-server-view-has-mixed-up-data%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        When you put * in the column list of a view and the underlying tables change your view will not automatically update to include the changed columns. In fact, if you delete a column you can get the data mixed up across columns. This has been discussed and documented many times. Aaron Bertrand has a great article covering this topic.



        • Bad habits to kick : using SELECT * / omitting the column list

        Moral of the story, avoid using select * unless the select is inside an EXISTS.






        share|improve this answer



























          1














          When you put * in the column list of a view and the underlying tables change your view will not automatically update to include the changed columns. In fact, if you delete a column you can get the data mixed up across columns. This has been discussed and documented many times. Aaron Bertrand has a great article covering this topic.



          • Bad habits to kick : using SELECT * / omitting the column list

          Moral of the story, avoid using select * unless the select is inside an EXISTS.






          share|improve this answer

























            1












            1








            1






            When you put * in the column list of a view and the underlying tables change your view will not automatically update to include the changed columns. In fact, if you delete a column you can get the data mixed up across columns. This has been discussed and documented many times. Aaron Bertrand has a great article covering this topic.



            • Bad habits to kick : using SELECT * / omitting the column list

            Moral of the story, avoid using select * unless the select is inside an EXISTS.






            share|improve this answer














            When you put * in the column list of a view and the underlying tables change your view will not automatically update to include the changed columns. In fact, if you delete a column you can get the data mixed up across columns. This has been discussed and documented many times. Aaron Bertrand has a great article covering this topic.



            • Bad habits to kick : using SELECT * / omitting the column list

            Moral of the story, avoid using select * unless the select is inside an EXISTS.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 22:38









            Aaron Bertrand

            207k27361404




            207k27361404










            answered Sep 12 '17 at 19:08









            Sean Lange

            24.4k21735




            24.4k21735























                2














                It doesn't make sense that simply altering the view without changing any of the code would fix it, but an important point is this part of your view...



                select pvtConsumidorFinanciero.*



                If this table definition changes... that is, if more columns are added or some are removed, the columns in this view would also change. That is why it is good practice to never select * in a view, especially when querying another view.



                Additionally, this table could have the same column names as other tables.



                What also could have happened is in your application, you are select * from view. Again, if a DBA changed the view, this could mess up your application, so i would avoid it an explicitly list the columns you want returned in the order you want them returned.






                share|improve this answer

























                  2














                  It doesn't make sense that simply altering the view without changing any of the code would fix it, but an important point is this part of your view...



                  select pvtConsumidorFinanciero.*



                  If this table definition changes... that is, if more columns are added or some are removed, the columns in this view would also change. That is why it is good practice to never select * in a view, especially when querying another view.



                  Additionally, this table could have the same column names as other tables.



                  What also could have happened is in your application, you are select * from view. Again, if a DBA changed the view, this could mess up your application, so i would avoid it an explicitly list the columns you want returned in the order you want them returned.






                  share|improve this answer























                    2












                    2








                    2






                    It doesn't make sense that simply altering the view without changing any of the code would fix it, but an important point is this part of your view...



                    select pvtConsumidorFinanciero.*



                    If this table definition changes... that is, if more columns are added or some are removed, the columns in this view would also change. That is why it is good practice to never select * in a view, especially when querying another view.



                    Additionally, this table could have the same column names as other tables.



                    What also could have happened is in your application, you are select * from view. Again, if a DBA changed the view, this could mess up your application, so i would avoid it an explicitly list the columns you want returned in the order you want them returned.






                    share|improve this answer












                    It doesn't make sense that simply altering the view without changing any of the code would fix it, but an important point is this part of your view...



                    select pvtConsumidorFinanciero.*



                    If this table definition changes... that is, if more columns are added or some are removed, the columns in this view would also change. That is why it is good practice to never select * in a view, especially when querying another view.



                    Additionally, this table could have the same column names as other tables.



                    What also could have happened is in your application, you are select * from view. Again, if a DBA changed the view, this could mess up your application, so i would avoid it an explicitly list the columns you want returned in the order you want them returned.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 12 '17 at 16:33









                    scsimon

                    20.6k41536




                    20.6k41536





















                        0














                        I think this is is also part of the answer:



                        When you create views it is a good practice to use SCHEMABINDING, this way when you alter the table under the view, you are forced to review your view as well.






                        share|improve this answer

























                          0














                          I think this is is also part of the answer:



                          When you create views it is a good practice to use SCHEMABINDING, this way when you alter the table under the view, you are forced to review your view as well.






                          share|improve this answer























                            0












                            0








                            0






                            I think this is is also part of the answer:



                            When you create views it is a good practice to use SCHEMABINDING, this way when you alter the table under the view, you are forced to review your view as well.






                            share|improve this answer












                            I think this is is also part of the answer:



                            When you create views it is a good practice to use SCHEMABINDING, this way when you alter the table under the view, you are forced to review your view as well.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 12 '17 at 19:25









                            Victor Hugo Terceros

                            1,6932618




                            1,6932618



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid …


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid …


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46181318%2fsql-server-view-has-mixed-up-data%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                ReactJS Fetched API data displays live - need Data displayed static

                                Evgeni Malkin