473,698 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem when changing Views

I have 3 views, two of which depend on the other:

CREATE VIEW dbo.CustomerLis tQueryAccounts
AS
SELECT dbo.CustomerLis tQuery.*
FROM dbo.CustomerLis tQuery
WHERE (isProspect = 0)

CREATE VIEW dbo.CustomerLis tQueryProspects
AS
SELECT dbo.CustomerLis tQuery.*
FROM dbo.CustomerLis tQuery
WHERE (isProspect = 1)

which depend on:
CREATE VIEW dbo.CustomerLis tQuery
AS
SELECT TOP 100 PERCENT
dbo.Customers.* , CAST(dbo.Custom ers.CustomerID AS int) AS
CustomerIDAsNum ber,

dbo.NumberOfJob sPerCustomer.Co untOfJobID,
dbo.NumberOfQuo tesPerCustomer. CountOfQuoteID,

dbo.NumberOfCom plaintsPerCusto mer.CountOfComp laintID,
dbo.NumberOfNot esPerCustomer.C ountOfCustomerN oteID,

dbo.NumberOfEnq uiriesPerCustom er.CountOfEnqui ryID
FROM dbo.Customers

LEFT OUTER JOIN
dbo.NumberOfJob sPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfJob sPerCustomer.Cu stomerID
LEFT OUTER JOIN
dbo.NumberOfQuo tesPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfQuo tesPerCustomer. CustomerID
LEFT OUTER JOIN
dbo.NumberOfNot esPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfNot esPerCustomer.C ustomerID
LEFT OUTER JOIN
dbo.NumberOfCom plaintsPerCusto mer ON
dbo.Customers.C ustomerID = dbo.NumberOfCom plaintsPerCusto mer.CustomerID

LEFT OUTER JOIN
dbo.NumberOfEnq uiriesPerCustom er ON
dbo.Customers.C ustomerID = dbo.NumberOfEnq uiriesPerCustom er.CustomerID

ORDER BY dbo.Customers.A ccountName

These work well but I have an alternative version of this latter one
which has fewer columns. At present it is:
CREATE VIEW dbo.CustomerLis tQueryShorter
AS
SELECT dbo.Customers.* , CAST(dbo.Custom ers.CustomerID AS int) AS
CustomerIDAsNum ber
FROM dbo.Customers

I now wish to make this one CustomerListQue ry and so rename the
existing one CustomerListQue ryOriginal (just to get is out of the way)
and change its first line, using the View's properties (in Enterprise
Manager) to :
CREATE VIEW dbo.CustomerLis tQueryOriginal

I then rename CustomerListQue ryShorter to CustomerListQue ry and change
its first line to
CREATE VIEW dbo.CustomerLis tQuery

Now when I 'Return all rows' of CustomerListQue ryAccounts I get an
error message:
'dbo.CustomerLi stQueryAccounts ' has more column names specified than
columns defined.

If however, I go into the design and then select Run, I get the correct
output reflecting the new version of CustomerListQue ry. How do I get
the 'Return all rows' output to show the same.

It appears that the orginal version is still being used. How do I get
the system to replace this ?

Jul 5 '06 #1
6 1690
Jim Devenish (in************ ***@foobox.com) writes:
I have 3 views, two of which depend on the other:
CREATE VIEW dbo.CustomerLis tQuery
AS
SELECT TOP 100 PERCENT
...
ORDER BY dbo.Customers.A ccountName
Remove this TOP 100 PERCENT and ORDER BY nonsense. It serves no
purpose. Yes, you may feel that when you do a SELECT on the view
that you get back the rows in the same order as the ORDER BY clause,
but that is due to mere chance. Many people who had this sort of
views found that they no longer the result they expected when they
moved to SQL 2005.

There is only one way to get an ordered result from a query, and that
is to add ORDER BY to the query itself.
I then rename CustomerListQue ryShorter to CustomerListQue ry and change
its first line to
CREATE VIEW dbo.CustomerLis tQuery

Now when I 'Return all rows' of CustomerListQue ryAccounts I get an
error message:
'dbo.CustomerLi stQueryAccounts ' has more column names specified than
columns defined.

If however, I go into the design and then select Run, I get the correct
output reflecting the new version of CustomerListQue ry. How do I get
the 'Return all rows' output to show the same.
You should stop using SELECT *. SELECT * is great for ad-hoc queries,
but it does not belong in production code. One reason for that is what
you experienced. Had you listed the columns explicitly, you would still
have gotten an error, but at least the error would have been apparent.

If you insist on using SELECT *, you need to learn to use sp_refreshview,
because you will need it a lot.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 5 '06 #2

Erland Sommarskog wrote:
Remove this TOP 100 PERCENT and ORDER BY nonsense. It serves no
purpose. Yes, you may feel that when you do a SELECT on the view
that you get back the rows in the same order as the ORDER BY clause,
but that is due to mere chance. Many people who had this sort of
views found that they no longer the result they expected when they
moved to SQL 2005.

There is only one way to get an ordered result from a query, and that
is to add ORDER BY to the query itself.
Thank you for your helpful advice. I had not appreciated that using
Order By within a View was nonsense. I am in the process of converting
the back-end of my database from Access to SQLServer and so am new to
the latter. CustomerListQue ry had been an Access query used as the
RecordSource of a Form but in order to speed things up I made into a
View.

Are now suggesting that I remove Order By from the View and then make
the RecordSource into:
Select * From CustomerListQue ry Order By AccountName

I have just looked at BOL for 'Order By' and find that it says:
"The Order By clause is invalid in Views ... unless TOP is also
specified"
This implies that it can be used in this way but it does not say that
it fails to carry out the ordering.

Similarly when I look up the syntax of Create View I find, within the
select_statemen t that:
"A Create View statement cannot include Order By clause, unless there
is also a TOP clause in the select list of the Select statement"

It does not say that this is nonsense.

Jul 6 '06 #3

Erland Sommarskog wrote:
You should stop using SELECT *. SELECT * is great for ad-hoc queries,
but it does not belong in production code. One reason for that is what
you experienced. Had you listed the columns explicitly, you would still
have gotten an error, but at least the error would have been apparent.

If you insist on using SELECT *, you need to learn to use sp_refreshview,
because you will need it a lot.
Thank you for your warning about the use of Select * . I expect that
there has been extensive discussion elsewhere as to why 'it does not
belong in production code' but I was unaware of it. Perhaps you can
point me in the right direction.

However you say that I would have still got an error had I listed the
columns explicitly. So I return to my orginal question: why does the
new view produce the expected output from the Design View but not from
'Return all rows'?

How and where do I use sp_refreshview?

Jul 6 '06 #4
Stu
Hey Jim,

There are a number of issues with your approach, but the primary
reason that you're having the problems you are is that you're using
Enterprise Manager (known by many as Enterprise MANGLER) to edit the
script of your views. EM is a good tool for administration, but you
should be using Query Analzer for editing.

In Query Analyzer, you can right-click on the view and select EDIT, and
it will show you the script of the view, which you can then ALTER to
get to the correct format you need.

As far as the other issues, I think Erland tapped into them:

1. Don't use SELECT * in production code (or at least don't do so
wihout commenting). There was a recent discussion in another group
about it that you may want to read.
http://groups.google.com/group/micro...efe874da82b7ef
2. The TOP...ORDER BY embedded in a view is, at best, flaky. It's
also one of the behaviors that was changed in SQL 2005, so if you can
avoid using it, I'd recommend that you do so. The only way to ensure
an order to your results is to use ORDER BY in your final outer SELECT
statement (e.g., SELECT columnlist FROM dbo.CustomerLis tQueryAccounts
ORDER BY AccountName).

HTH,
Stu

Jim Devenish wrote:
I have 3 views, two of which depend on the other:

CREATE VIEW dbo.CustomerLis tQueryAccounts
AS
SELECT dbo.CustomerLis tQuery.*
FROM dbo.CustomerLis tQuery
WHERE (isProspect = 0)

CREATE VIEW dbo.CustomerLis tQueryProspects
AS
SELECT dbo.CustomerLis tQuery.*
FROM dbo.CustomerLis tQuery
WHERE (isProspect = 1)

which depend on:
CREATE VIEW dbo.CustomerLis tQuery
AS
SELECT TOP 100 PERCENT
dbo.Customers.* , CAST(dbo.Custom ers.CustomerID AS int) AS
CustomerIDAsNum ber,

dbo.NumberOfJob sPerCustomer.Co untOfJobID,
dbo.NumberOfQuo tesPerCustomer. CountOfQuoteID,

dbo.NumberOfCom plaintsPerCusto mer.CountOfComp laintID,
dbo.NumberOfNot esPerCustomer.C ountOfCustomerN oteID,

dbo.NumberOfEnq uiriesPerCustom er.CountOfEnqui ryID
FROM dbo.Customers

LEFT OUTER JOIN
dbo.NumberOfJob sPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfJob sPerCustomer.Cu stomerID
LEFT OUTER JOIN
dbo.NumberOfQuo tesPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfQuo tesPerCustomer. CustomerID
LEFT OUTER JOIN
dbo.NumberOfNot esPerCustomer ON
dbo.Customers.C ustomerID = dbo.NumberOfNot esPerCustomer.C ustomerID
LEFT OUTER JOIN
dbo.NumberOfCom plaintsPerCusto mer ON
dbo.Customers.C ustomerID = dbo.NumberOfCom plaintsPerCusto mer.CustomerID

LEFT OUTER JOIN
dbo.NumberOfEnq uiriesPerCustom er ON
dbo.Customers.C ustomerID = dbo.NumberOfEnq uiriesPerCustom er.CustomerID

ORDER BY dbo.Customers.A ccountName

These work well but I have an alternative version of this latter one
which has fewer columns. At present it is:
CREATE VIEW dbo.CustomerLis tQueryShorter
AS
SELECT dbo.Customers.* , CAST(dbo.Custom ers.CustomerID AS int) AS
CustomerIDAsNum ber
FROM dbo.Customers

I now wish to make this one CustomerListQue ry and so rename the
existing one CustomerListQue ryOriginal (just to get is out of the way)
and change its first line, using the View's properties (in Enterprise
Manager) to :
CREATE VIEW dbo.CustomerLis tQueryOriginal

I then rename CustomerListQue ryShorter to CustomerListQue ry and change
its first line to
CREATE VIEW dbo.CustomerLis tQuery

Now when I 'Return all rows' of CustomerListQue ryAccounts I get an
error message:
'dbo.CustomerLi stQueryAccounts ' has more column names specified than
columns defined.

If however, I go into the design and then select Run, I get the correct
output reflecting the new version of CustomerListQue ry. How do I get
the 'Return all rows' output to show the same.

It appears that the orginal version is still being used. How do I get
the system to replace this ?
Jul 6 '06 #5
Jim Devenish (in************ ***@foobox.com) writes:
Thank you for your warning about the use of Select * . I expect that
there has been extensive discussion elsewhere as to why 'it does not
belong in production code' but I was unaware of it. Perhaps you can
point me in the right direction.
There aree several reasons. One is tracability. Is the column xyz in use
somewhere? It's possible to find via sysdepends it is (although sysdepends
for various reasons isn't always reliable), but then you find that it
is a SELECT *, you cannot tell whether it is use at all. That is,
queries should list columns that are actually used. In any serious system
there are columns that are one point phased out - or could be phased out,
if you could verify that they are no longer in use.

If you add or drop columns, the SELECT * changes, but depending on context
not immediately, so there can be sources of confusion.
However you say that I would have still got an error had I listed the
columns explicitly. So I return to my orginal question: why does the
new view produce the expected output from the Design View but not from
'Return all rows'?
I use neither of the tools, but I guess that Design View resubmits the
view definition something Return all Rows have no reason to do. Using
Profiler would reveal what is going on.
How and where do I use sp_refreshview?
In Query Analyzer where you run other queries. As for how, well, did
you try Books Online?
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 6 '06 #6
Jim Devenish (in************ ***@foobox.com) writes:
Thank you for your helpful advice. I had not appreciated that using
Order By within a View was nonsense. I am in the process of converting
the back-end of my database from Access to SQLServer and so am new to
the latter. CustomerListQue ry had been an Access query used as the
RecordSource of a Form but in order to speed things up I made into a
View.

Are now suggesting that I remove Order By from the View and then make
the RecordSource into:
Select * From CustomerListQue ry Order By AccountName
Yes.
I have just looked at BOL for 'Order By' and find that it says:
"The Order By clause is invalid in Views ... unless TOP is also
specified"
This implies that it can be used in this way but it does not say that
it fails to carry out the ordering.
TOP 10 PERCENT ORDER BY is meaningful, because it picks the the top 10
percent according to the ORDER BY clause.

TOP 100 PERCENT ORDER BY is meaningless, because 100 percent is
everything, so it does not matter what you order by. You still get
everything.

That is, the purpose of ORDER BY in views in combination of TOP is to
determine which rows that are selected by the view.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8792
by: pysim | last post by:
Hi, I have a couple of general requests for pointers to python examples and design advice. I'm looking for examples of MVC-based GUI controls done in python (model-view-controller). Also, examples where something like a simulation model running in its own thread sends fast updates to a GUI in near real-time. The only examples I've seen, such as SimPy, wait until the model is finished running before outputting data.
20
2684
by: Neil | last post by:
I have an Access 2000 MDB file with a SQL 7 back end. I have a main table with 50,000 records; and I have a selections table with 50,000 records for each machine that uses the database (about 25-50). This allows each user to have their own set of selections. The selections table has three fields: ID (int), Sel (bit), MachName (varchar). ID and MachName comprise the primary key. I have a view that combines the main table and the entries...
8
2168
by: Dominique | last post by:
In order to become more familiar with the Model-View-Controller pattern, I have written a demo where each View is a plugin and the plugins are loaded at startup from the plugins directory. When the View's are loaded they add themselves to the DataModel View list... FDataModel.AddView(this); Where AddView looks like this... public void AddView( MvcView view ) {
2
1532
by: @cl | last post by:
hello ,everybody I'm a newer to xml, and I need to format and indent xml in a textpanel, as when input is <root> <node1> <node11/> </node1>
17
11708
by: radio1 | last post by:
Configuration: Access 2002 and SQL Server 2000 using a .ADP Project. I would VERY MUCH appreciate anyone's input into this problem I'm having. I have a form in Access that does not permit the user to add/change/delete any data, apparently as a result of adding a GROUP BY clause to a View used indirectly as the form's Record Source. I really don't believe that this restriction needs to be there, and I'm hoping that someone can suggest...
4
2839
by: antonyliu2002 | last post by:
My web form has a MultiView which has 4 Views. 4 LinkButtons on the web form activates each of the 4 views. The 4 views have been hiding and showing nicely depending upon which LinkButton is clicked. But, when I try to get the ActiveViewIndex when the button is clicked, I always get 0. Or if I do Multiview1.GetActiveView().ID;
3
1621
by: helraizer1 | last post by:
Hey folks, I have made an image-based shoutbox and now users can view older and newer message on the shoutbox depending on the $_GET - pagination - that works. However, since it's image based and can be linked in forums etc. on the Internet I only want it to save the gif image if $page == 1, so the newest shouts are saved onto the image. Yet the users can still view the other messages on site without it changing the saved gif image. What...
6
7045
by: Neil | last post by:
I had a strange situation with a view in SQL 7, that I could use some input on. I had a very simple view -- select a, b, c from table1 where x=y and z=q. Field a in table1 originally was varchar 70. A long time ago I changed it to varchar 95. I used this view as an ODBC linked table in an Access MDB. Recently, there was one row which has a value in field a that was more than 70 characters long. This caused an error when the view as...
0
1293
by: Tim | last post by:
Hi all, I'm using AJAX.NET and I've coded a page that changes the text of a button when a checkbox is clicked. The text changes from "Save" to "Keep Processing". The text change seems to work. However, when I then go to click on the button, I click once, and nothing happens. I click again, and get an error:
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.