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

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!

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

SQL Server 2012 vs SQL Server 2008 Where clause processing

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.

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

Convert SQL computer to ROLL UP sql server 2012 since its no longer support compute

I have db like this:
For meaning:
LOP: class
SINHVIEN: student
KHOA: department
MONHOC: subject
DIEMTHI: mark
I want to do the query to list ALL the CLASS and the number of CLASS belong to each DEPARTMENT
SELECT khoa.makhoa,tenkhoa,malop,tenlop FROM khoa,lop
WHERE khoa.makhoa=lop.makhoa
ORDER BY khoa.makhoa
COMPUTE COUNT(malop) BY khoa.makhoa
and the result look like
But, the SQL 2012 NO LONGER support COMPUTE, they said it's can be done with ROLL UP but I can't do with its syntax, please help me
Update 1:
RADAR's result, with more help
you can replace COMPUTE with ROLLUP and GROUP BY
SELECT khoa.makhoa,tenkhoa,malop,tenlop , COUNT(malop) FROM khoa,lop
WHERE khoa.makhoa=lop.makhoa
GROUP BY khoa.makhoa,tenkhoa,malop,tenlop
WITH ROLLUP

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