Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

View not working since moving database from SQL 2000 to SQL 2008 Expand / Collapse
Author
Message
Posted Friday, January 11, 2013 2:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 3, 2013 11:50 AM
Points: 21, Visits: 70
Hi All

I moved a simple database from SQL Server 2000 to SQL server 2008. It has a couple of Tables and a couple of views.

I have a view using the following statement.

SELECT DISTINCT 
TOP (100) PERCENT PERSONID AS EmpID, CONVERT(numeric, PERSONNUM) AS ClockNo, FIRSTNM + ' ' + LASTNM AS EmpName, EMPLOYMENTSTATUS,
HOMELABORLEVELNAME3
FROM KronosWFC.dbo.VP_EMPLOYEE
WHERE (EMPLOYMENTSTATUS = 'Active')
ORDER BY ClockNo

The above works perfectly, however if I modify it slightly like below.

SELECT DISTINCT 
TOP (100) PERCENT PERSONID AS EmpID, CONVERT(numeric, PERSONNUM) AS ClockNo, FIRSTNM + ' ' + LASTNM AS EmpName, EMPLOYMENTSTATUS,
HOMELABORLEVELNAME3
FROM KronosWFC.dbo.VP_EMPLOYEE
WHERE (EMPLOYMENTSTATUS = 'Active') AND (CONVERT(numeric, PERSONNUM) = 722)
ORDER BY ClockNo

I get an SQL error "Error converting data type nvarchar to numeric"

Now the above view worked perfectly in SQL 2000 but not in SQL 2008, I just cant figure out what it is. At first I thought maybe it was because one of the rows could not be converted to a numer but I would have assumed the first view wouldnt work either. Also I have checked all the rows and they are all numbers.

Any help would be appreciated.

Regards

Douglas Bell
Post #1405834
Posted Friday, January 11, 2013 2:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:52 AM
Points: 5,131, Visits: 4,918
That is a funny one, as the convert is the same in the select and the where, so both should fail.

Would it be possible to post the DDL (CREATE TABLE script) form VP_EMPLOYEE and also some dummy data, to see if I can repro the error.




Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1405842
Posted Friday, January 11, 2013 5:26 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:11 PM
Points: 15,517, Visits: 27,893
It's probably because the SELECT criteria only converts the information returned, but the WHERE clause, since it's a function on a column which means a complete scan of the table (and a very bad idea), it must resolve the values. So you may not see the error from the SELECT, but you're guaranteed to always see it in the WHERE.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1405927
Posted Friday, January 11, 2013 5:29 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
Sorry to possibly add to your troubles but you may have more than one problem with your View.

The "Select Top 100 Percent" method was commonly used in previous versions of sql server to retrieve ordered
results from Views. I don't believe this works in newer versions of Sql Server as the optimizer seems to just ignore the request to Sort.
You can easily test it by viewing the plan of a call to your View.
If the order of rows is important, you may need to order the rows in an outer query as you retrieve them from the View.
Post #1405932
Posted Friday, January 11, 2013 6:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:52 PM
Points: 7,079, Visits: 12,569
What is the datatype of PERSONNUM?

You can change this in the WHERE clause:

WHERE     (EMPLOYMENTSTATUS = 'Active') AND (CONVERT(numeric, PERSONNUM) = 722)

to this, where DATATYPE is the datatype of PERSONNUM, e.g. VARCHAR(20) or NVARCHAR(50), which will make that part a SARG:

WHERE     (EMPLOYMENTSTATUS = 'Active') AND (PERSONNUM = CAST('722' AS DATATYPE))



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1405965
Posted Monday, January 14, 2013 12:02 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, July 18, 2014 2:05 PM
Points: 3,943, Visits: 2,957
Grant is right - you need to check your data types.

Also, you cannot have an ORDER BY clause in a view. SQL 2000 tolerated it but the later versions do not.
If you need one, you have to include the TOP n clause in the SELECT statement.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1406893
Posted Monday, January 14, 2013 12:18 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:52 PM
Points: 7,079, Visits: 12,569
Ed Wagner (1/14/2013)
If you need one, you have to include the TOP n clause in the SELECT statement.

...or TOP (n) PERCENT


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1406898
Posted Monday, January 14, 2013 12:24 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:32 PM
Points: 13,067, Visits: 11,903
Ed Wagner (1/14/2013)
Grant is right - you need to check your data types.

Also, you cannot have an ORDER BY clause in a view. SQL 2000 tolerated it but the later versions do not.
If you need one, you have to include the TOP n clause in the SELECT statement.


It will only guarantee which rows and NOT guarantee the output order. If you want an order to the result the ONLY way to ensure that is to have an order by on the query, not the view.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1406904
Posted Tuesday, January 15, 2013 3:21 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 3, 2013 11:50 AM
Points: 21, Visits: 70
Hi All

Sorry for the late reply, first of me getting a chance to look at this particular database.

PERSONNUM is NVARCHAR(15)

I have taken away the order by clause as to be honest its not really needed as the application front end does the sorting anyway.

I have tried multiple things converting both ClockNo and 722 to numeric, converting the 722 to NVARCHAR(15) I still get the error that it cannot convert.

Regards

Douglas Bell

Post #1407128
Posted Tuesday, January 15, 2013 6:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:52 PM
Points: 7,079, Visits: 12,569
What does this query yield?

SELECT DISTINCT
PERSONID AS EmpID,
CONVERT(NUMERIC, 722) AS ClockNo, -- we know the ClockNo is 722 per the WHERE-clause
FIRSTNM + ' ' + LASTNM AS EmpName,
EMPLOYMENTSTATUS,
HOMELABORLEVELNAME3
FROM KronosWFC.dbo.VP_EMPLOYEE
WHERE EMPLOYMENTSTATUS = 'Active'
AND PERSONNUM = CAST('722' AS NVARCHAR(15));



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1407242
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse