Invalid object name 'sys.dm_exec_procedure_stats' error - sql

I am trying to obtain the maximum time consumed by a stored procedure in my DB. I obtained a sample query from here to obtain the same using sys.dm_exec_procedure_stats. The same is posted below. Whenever I try to execute this query I get the error as
Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.dm_exec_procedure_stats'.
Can you please let me know where I might probably be going wrong?
Below is the query used. No changes made.
SELECT TOP 10 d.object_id, d.database_id, OBJECT_NAME(object_id, database_id) 'proc name',
d.cached_time, d.last_execution_time, d.total_elapsed_time,
d.total_elapsed_time/d.execution_count AS [avg_elapsed_time],
d.last_elapsed_time, d.execution_count
FROM sys.dm_exec_procedure_stats AS d
ORDER BY [total_worker_time] DESC;
EDIT: Sorry for the blunder. Server is 2005.

Related

Why can't I use my server name (SQL Server)?

I'm about to study how to join tables from multiple, different databases. So the way I refer to a particular table is by following this format db_server_name.db_name.schema_name.table_name. So after searching around how to get the server name using this command :
SELECT ##SERVERNAME
I got the following server name:
LAPTOP-FV8FREL6\SQLEXPRESS
Also this confirms:
So I did this query :
select v.VendorID, v.VendorName
from LAPTOP-FV8FREL6\SQLEXPRESS.AP.dbo.Vendors v;
But it says
Msg 102, Level 15, State 1, Line 6 Incorrect syntax near '-'.
Do you know how to get this works?
Put [ ] around your server name.
select v.VendorID, v.VendorName
from [LAPTOP-FV8FREL6\SQLEXPRESS].AP.dbo.Vendors v;

Importing OLAP metadata in SQL Server via linked server results in out-of-range date

Currently, I am trying to extract metadata from an OLAP cube in SQL Server (via a linked server) using this simple query:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
select *
from $System.TMSCHEMA_COLUMNS
')
But in the result set, there is a column named RefreshedTime with values 31.12.1699 00:00:00.
Because of this value, the query gives this error message:
Msg 8114, Level 16, State 9, Line 1 Error converting data type (null)
to datetime.
The problem is that I need to run the query without specifying the columns in the SELECT statement.
Do you know a trick to avoid this error?
I know you wanted not to have to mention the columns explicitly, but in case nobody can suggest a way to have it handle the 1699-12-31 dates, then you can fallback to this:
select *
into [dbo].[columns_metadata]
from openquery([LINKED_SERVER], '
SELECT [ID]
,[TableID]
,[ExplicitName]
,[InferredName]
,[ExplicitDataType]
,[InferredDataType]
,[DataCategory]
,[Description]
,[IsHidden]
,[State]
,[IsUnique]
,[IsKey]
,[IsNullable]
,[Alignment]
,[TableDetailPosition]
,[IsDefaultLabel]
,[IsDefaultImage]
,[SummarizeBy]
,[ColumnStorageID]
,[Type]
,[SourceColumn]
,[ColumnOriginID]
,[Expression]
,[FormatString]
,[IsAvailableInMDX]
,[SortByColumnID]
,[AttributeHierarchyID]
,[ModifiedTime]
,[StructureModifiedTime]
,CStr([RefreshedTime]) as [RefreshedTime]
,[SystemFlags]
,[KeepUniqueRows]
,[DisplayOrdinal]
,[ErrorMessage]
,[SourceProviderType]
,[DisplayFolder]
from $System.TMSCHEMA_COLUMNS
')

Invalid procedure call error in MS Access Sum

I use the following SQL select query in MS Access to generate result on large input data:
SELECT "1.01" AS Item
,"Name" AS Test
,Count([Item]) AS [Count]
,Sum(IIf([Results] = "Invalid", 1, 0)) AS [# Invalid]
FROM [Test 1-01]
GROUP BY "1.01"
,"Name"
,"Yes";
I got an 'Invalid Procedure call' error. I traced down the part of the query that caused the error is
Sum(IIf([Results]="Invalid",1,0)) AS [# Invalid]
Does anyone have any idea of how to fix this error ?
#Sergery S. You're right. I found the problem was due to one damaged row data. It works fine again after I removed that row. Thahks again.

DB2 SQL How to get the last executed SQL-Statement with GET DIAGNOSTICS?

I want to call a procedure in RPG on IBM i with SQLSTATE and with a variable text.
getSQLMessage(SQLSTT: text)
The variable text should be the last executed sql statement before the procedure call.
Is there a opportunity to get it like this:
EXEC SQL GET DIAGNOSTICS CONDITION 1 :text = last executed sql statement
Or maybe someone knwos another solution for my problem?
Thanks a lot!
You can't use GET DIAGNOSTICS, but you can first get the JobLog
DSPJOBLOG OUTPUT(*OUTFILE) OUTFILE(QTEMP/ERR_LOG)
then get the last SQL Error:
Select Qmhmf,
Qmhmid,
Qmhmdt
From Qtemp.Err_Log
Where Qmhsev >= 20
And Substr(Qmhmid, 1, 3) In ('CPA' , 'CPD' , 'CPF' , 'SQL')
Order By Rrn(Err_log) Desc
Fetch First 1 Rows Only

SQL Server : merge using Join on Source Table fails to bind

I am writing a SQL Server Merge statement but can't seem to get the syntax correct. Would someone please take a look to see where I'm going wrong?
Any help you can give is most appreciated.
What I have is two tables that I'd like to merge (w_materialmarketprices2 and d_component). My source (d_component) table requires me to do a join to a tax table (d_tax).
Everything works fine except for when I try to add the additional tax table into my join. The reason I need the tax table is because it contains a tax rate which I don't have in my d_component table (although I do have the corresponding tax code).
My comparison criteria between w_materialmarketprices2 and d_component includes the tax rate in the calculation.
Here's my code:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
(SELECT
[comp_code], [comp_desc], [comp_o_un], [comp_type],
[comp_ccy], [comp_tx], [comp_net_price], [comp_per],
[comp_doc_date], [comp_last_update], [comp_latest],
D.[tax_rate] AS TaxRate
FROM
[DWH].[dbo].[d_component]) AS B
INNER JOIN
[DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] AND A.[mp_valid_date] = B.[comp_doc_date] AND B.[comp_net_price]>0 AND A.[mp_price_inc_vat] = ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3) AND A.[mp_budget_actual] = 'PO Actual' AND B.[comp_type] ='P100' AND (left(B.[comp_code],1)='S' OR left(B.[comp_code],1)='R')
WHEN NOT MATCHED BY TARGET
THEN
INSERT ([mp_budget_actual], [mp_comp_code], [mp_comp_desc], [mp_unit], [mp_unit_qty], [mp_qualified_supplier], [mp_ccy], [mp_price_inc_vat], [mp_valid_date], [mp_last_update], [mp_latest])
VALUES ('PO Actual', B.[comp_code], B.[comp_desc], B.[comp_o_un], 1, 'Y', B.[comp_ccy], ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3), B.[comp_doc_date], B.[comp_last_update], B.[comp_latest])
;
The error I'm getting is:
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ','.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'B'.
,D.[tax_rate] AS TaxRate shows up as underlined in red so I reckon the problem is something to do with that. I also get the message
The multi-part identifier "D.tax_rate" could not be bound
Thanks for your help in advance. Honkonger.
There is no reason to use a subquery in the USING clause, ie: don't put a SELECT in there:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
[DWH].[dbo].[d_component] AS B
INNER JOIN [DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] .......