SQL Server 2012 vs SQL Server 2008 Where clause processing - sql-server-2012

We just recently converted to SQL Server 2012 (yes, our company is very behind!) from 2008. In processing some very basic queries, I am finding that the results are different and it appears to be related to the order of the items in the where clause.
For example this query:
select
source, count(*), sum(book_value)
from
repository.CURRENT_MONTH_14_1
where
NET_WAC = 0 or gross_wac = 0
and As_Of_Date = '2/29/2016'
and Argus_Entity = 'TEST'
group by
source
The results in SQL Server 2008, would give me only items that satisfy both the as_of_date and the argus_entity requirement and have a net_wac or gross_wac = 0.
In SQL Server 2012, I am getting all items that have a net_wac or gross_wac = 0 even if they are in other argus_entity or as_of_dates.
Is there a certain order required now? Does SQL Server 2012 no longer satisfy all where requirements before giving results?
FYI, I do not have access to the server or any backend information. I'm lucky they allow us to query the information.
Thanks for any help you can provide.

Related

SQL Query conversion from 6.5 to >= SQL Server 2008

Currently i am working on the migration project. Found the below query in a procedure. I am able to get the size of database from sys.master_files table. In WHERE condition segmap is used. I am not able to find the simillar column in sys.master_files. Please help me on this
SELECT sum(size) * 2
FROM master..sysusages U
WHERE U.segmap = 3
AND U.dbid = db_id(#db_name)
SYBASE and SQLSERVER used to share same code base.So as from SYBASE docs..below is definition of segmap
The values of master..sysusages.segmap mean the following:
3: Data stored on this segment
4: Log stored on this segment
7: Since 7=4+3, both log and data stored on this segment
So the equivalent would be type='0' which means get only data space

Simple SQL Server query hanging after a certain index. What's going on?

We are using Microsoft SQL Server 2008 R2 (SP3) - 10.50.6220.0 (X64). I have this simple table in SQL Server which according to its storage property has only 2910 records.
I've come across what is perhaps the weirdest performance issue in my experience thus far and have no way to tell what's happening: any query "beyond" row 2100 in the default order simply hangs indefinitely. This is happening in ONLY one of our environments, nowhere else.
So, what I am saying is that if I query:
SELECT TOP 2100 *
FROM [Rapids].[everest].[UserData]
→ 2100 rows and query finishes execution in no time.
While
SELECT TOP 2101 *
FROM [Rapids].[everest].[UserData]
→ query hangs indefinitely.
As I said, we are on SQL Server 2008 R2, so I can't use OFFSET/FETCH NEXT, but if I do my offset manually, I still get the same behaviour.
Let's say that the penultimate record of the first 2100 returned in the successful query has Id = 2324, if I query:
SELECT TOP 1 *
FROM [Rapids].[everest].[UserData]
WHERE Id > 2324
→ 1 row, corresponding to the last one of the first 2100.
And the following hangs:
SELECT TOP 2 *
FROM [Rapids].[everest].[UserData]
WHERE Id > 2324
→ query hangs indefinitely.
I'm really puzzled, and as I said, this is happening in only 1 environment, which leads me to believe this table is "corrupted" in some way.

isDescendantOf method can not be found in SQL Server 2012 script - Why?

I'm new to SQL server 2012, and was doing well until I ran this code, taken from a Microsoft book. I've hunted for a solution and got no where.
Any advice you can give is greatly appreciated.
DECLARE #AmyNodeId hierarchyid
SELECT #AmyNodeId = NodeId
FROM Employee
WHERE EmployeeId = 46
SELECT NodeId.ToString() AS NodeIdPath, *
FROM Employee
WHERE Employee.NodeId.isDescendantOf(#AmyNodeId) = 1
ORDER BY NodeLevel, NodeId
Gives this message:
Could not find method 'isDescendantOf' for type 'Microsoft.SqlServer.Types.SqlHierarchyId' in assembly 'Microsoft.SqlServer.Types'
Humph!
Harvey
This is because the method is actually case sensitive! check your case
This fixed itself after I restarted the SQL Server 2014 Management studio.
Bugger!

How to Find the Database Views Which are not executed or Accessed for more than 6 Months in SQL Server 2005

I would like to clean up my database by identified & removing the views & stored procedures which were not in use or not accessed for a longer period (May be for last 6 months or 1 year) in SQL Server 2005.
Please help.
You can't do this 100% unless you're running a trace on your system 24/7 and keeping the data or using the auditing mechanisms of 2008.
All the data will be lost when you restart system, else you can find out the last used time for a specific object as queried below
select
DB_NAME(us.[database_id]) as [db],
OBJECT_NAME(us.[object_id],us.[database_id]) as [object],
MAX(us.[last_user_lookup]) as [last_user_lookup],
MAX(us.[last_user_scan]) as [last_user_scan],
MAX(us.[last_user_seek]) as [last_user_seek]
from sys.dm_db_index_usage_stats us
where us.[database_id] = DB_ID()
AND us.[object_id] = OBJECT_ID('tblname')
group by us.[database_id], us.[object_id];
Based on #Koushick's answer, I resolved it by using this ..
SELECT DB_NAME(us.[database_id]) AS [db],
OBJECT_NAME(us.[object_id], us.[database_id]) AS [object],
MAX(us.[last_user_lookup]) AS [last_user_lookup],
MAX(us.[last_user_scan]) AS [last_user_scan],
MAX(us.[last_user_seek]) AS [last_user_seek]
FROM sys.dm_db_index_usage_stats AS us
WHERE DB_NAME(us.[database_id]) = 'your database name'
AND OBJECT_NAME(us.[object_id], us.[database_id]) = 'your object'
GROUP BY us.[database_id], us.[object_id];
You can then quickly sort and play with the dates to see when objects were last used, as opposed to when they were last modified.
To find out the views/stored procedures older than particular date you can use following query
SELECT [name],create_date,modify_date
FROM sys.views (or sys.procedures)
WHERE modify_date<= 'date_older_than_you_want'
To find out unused views you can use following query:
SELECT [name],create_date,modify_date
FROM
sys.views
where create_date=modify_date

literal group works in Access 2010 but not SQl Server 2012

Hi the following query works in access 2010 but not SQL Server 2012. The error I am getting says 'Each GROUP BY expression must contain at least one column that is not an outer reference'. From my research it is because the query groups by a literal.
How can I achieve the same result in SQL Server
SELECT
1 AS ID, Sum([Meds].TotalMeds) AS TotalMeds,
Sum([Meds].Presc) AS Presc
INTO Stats_Meds
FROM
[Meds], Rounds
WHERE
((([Meds].Round)<[Rounds].[Round]))
GROUP BY
1;
thanks
Why do you even need a group by? Why not just use:
SELECT Sum(Meds.TotalMeds) AS TotalMeds, Sum(Meds.Presc) AS Presc
INTO Stats_Meds
FROM Meds, Rounds
WHERE Meds.Round < Rounds.Round
I am guessing you need the Id to inset into the Stats table. See if this query works for you.
DISCLAIMER(for down-voters): I have not tested this query on performance or actually run the query just a suggestion that might work for OP.
SELECT ID, SUM (WithId.TotalMeds), SUM(WithId.Presc)
INTO Stats_Meds
FROM
(SELECT 1 AS ID, Meds.TotalMeds, Rounds.Presc
FROM Meds, Rounds WHERE Meds.Round < Rounds.Round) WithId GROUP BY ID
OR you could just do -
SELECT 1, Sum(Meds.TotalMeds) AS TotalMeds, Sum(Meds.Presc) AS Presc
INTO Stats_Meds
FROM Meds, Rounds
WHERE Meds.Round < Rounds.Round