I want to fetch the tables and stored procedures added in last 5 or 6 months to a particular database.
I have tried using the below command but it is not giving correct data.
select * from sys.objects
where type = 'U' or type = 'P'
and modify_date between '2012-09-01' and '2013-01-29'
Please suggest which command can give me this list.
If you are looking for objects ADDED, then why are you querying on modify_date? Shouldn't you be looking at create_date?
select * from sys.objects
where (type = 'U' or type = 'P')
and create_date between '2012-09-01' and '2013-01-29'
Also, you have not put your OR in parentheses, which means that you will get incorrect results anyway.
Raj
Related
I am running a query that returns the last execution time for a stored procedure:
SELECT
o.name,
ps.last_execution_time
FROM
sys.dm_exec_procedure_stats ps
INNER JOIN
sys.objects o ON ps.object_id = o.object_id
ORDER BY
ps.last_execution_time DESC
I am getting the correct results, but if I run the query again in around 30 seconds, I don't get any results.
Is there a setting or command I need to set or add to persist the results?
My goal is to find out what stored procedures ran in the past 3 days. I'm running this query against SQL Server 2019 Express.
I would suggest extended events for this. First, the session definition:
CREATE EVENT SESSION [ProcExecutions] ON SERVER
ADD EVENT sqlserver.module_end
ADD TARGET package0.event_file(
SET filename = N'ProcExecutions',
max_file_size = 10,
max_rollover_files = 5
)
WITH (
MAX_MEMORY = 4096 KB,
EVENT_RETENTION_MODE = ALLOW_MULTIPLE_EVENT_LOSS,
MAX_DISPATCH_LATENCY = 30 SECONDS,
MAX_EVENT_SIZE = 0 KB,
MEMORY_PARTITION_MODE = NONE,
TRACK_CAUSALITY = OFF,
STARTUP_STATE = ON
)
GO
ALTER EVENT SESSION [ProcExecutions] ON SERVER
STATE = START;
You may want to modify the session definition to suit your needs. Examples would be:
Filtering by a particular user (e.g. your application's login)
Grabbing just a sample (e.g. "one in a hundred executions")
Grab additional data (e.g. "what user called the proc?", "what was the whole statement (including parameters)?", etc)
Here's how to read the data:
IF object_id('tempdb.dbo.#events') IS NOT NULL
DROP TABLE #events;
select cast(event_data as xml) as [event]
into #events
from sys.fn_xe_file_target_read_file('ProcExecutions*.xel', null, null, null);
WITH XEData AS (
SELECT
[event].value('(event/#timestamp)[1]', 'datetime2') AS [timestamp],
db_name([event].value('(event/data[#name="source_database_id"])[1]', 'int')) AS [database],
[event].value('(event/data[#name="object_name"])[1]', 'sysname') AS [object_name],
[event].query('.') AS [event]
from #events
)
SELECT *
FROM XEData
ORDER BY [timestamp];
Again, this is very basic (returning only the timestamp, database, and procedure name). When I set about munging a new event session's data, I'll use that event column as a reference for what the XML looks like so I can write appropriate xpath expressions to pull the data that I need.
I have an application which stores all data in SQL Server 2008. This application creates, modifies, updates tables. The name of the database is XLPR_2001 and it contains nearly 500 tables.
I want to find out changes if I made through application where it affect the entire database (XLPR_2001). If I find it so I can directly made those changes directly in the database, I complete my work very fast.
I search it on web but not help me in my case.
As your Question, you are looking for Table Change Effect :
SELECT name [TableName],
Create_date [CreateDate],
modify_date [LastUpdate]
FROM sys.all_objects
WHERE type = 'U'
ORDER BY modify_date DESC;
From above SQL Command which would give you all Table_Name which are last effected by some activities (i.e. insert, update or delete).
Result :
#sarslash and #Yogesh in my case your code show very old result. I found below code on somewhere and it work perfectly.
select
object_name(object_id) as OBJ_NAME, *
from
sys.dm_db_index_usage_stats
where
database_id = db_id(db_name())
Order by
dm_db_index_usage_stats.last_user_update desc
You can find this info at modify_date column of sys.objects table
SELECT name, modify_date from sys.objects where type ='U' order by modify_date desc
I want to do find names of tables and stored procedures that were created
in past month
I searched in several places but I didn't get any proper solution.
To be more clear on requirement I need a query which can return me a list of tables which were created in past month in my database.
You can make use of the view sys.objects
SELECT
CASE type WHEN 'U' THEN 'Table' WHEN 'P' THEN 'SP' END AS type,
name,
create_date
FROM sys.objects
WHERE
type IN ('U', 'P') AND DATEDIFF(MONTH, create_date, GETDATE()) < 1
SELECT name
FROM sys.objects
WHERE type = 'P'
AND DATEDIFF(D,create_date, GETDATE()) < 7
May be this could work for u
I need to search SQL Server's metadata for all stored procedures that were changed between specific dates.
So far I have following query that looking for specific value in the SP.
SELECT DISTINCT sysobjects.name AS [Object Name]
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
and syscomments.text like '%Stock%'
In the example above I am looking for all SP that have word Stock in it.
But how can I search for all SP's that were changed between specific dates?
USE AdventureWorks;
GO
SELECT name, create_date, modify_date
FROM sys.objects
WHERE type = 'P'
AND name = 'uspUpdateEmployeeHireInfo'
GO
source: http://blog.sqlauthority.com/2007/08/10/sql-server-2005-find-stored-procedure-create-date-and-modified-date/
I want to check if there are any records in a table for a certain entry. I used COUNT(*) to check the number of records and got it to work. However, when the number of records for an entry is very high, my page loads slowly.
I guess COUNT(*) is causing the problem, but how do I check if the records exist without using it? I only want to check whether any records exist for the entry and then execute some code. Please help me find an alternative solution for this.
Thanks for any help.
There are several ways that may work. You can use exists, which lets the database optimise the method to get the answer:
if exists(select * from ...)
You can use top 1 so that the database can stop after finding the first match:
if (select count(*) from (select top 1 * from ...)) > 0
use select top 1 and check is there is an row
You can try selecting the first entry for given condition.
SELECT id FROM table WHERE <condition> LIMIT 1
I'm not sure if this will be quicker but you can try.
Other possible solution. How do you use count? COUNT(*)? If yes, then try using COUNT(id). As I remember this should be faster.
I would recommend testing to see if at least 1 record exists in the table, that meets your criteria then continue accordingly. For example:
IF EXISTS
(
SELECT TOP 1 Table_Name --Or Your ColumnName
FROM INFORMATION_SCHEMA.Tables -- Or your TableName
)
BEGIN
PRINT 'At least one record exists in table'
END
I found this on codeproject. It's quite handy.
-- Author,,Md. Marufuzzaman
SELECT SYS_OBJ.NAME AS "TABLE NAME"
, SYS_INDX.ROWCNT AS "ROW COUNT"
FROM SYSOBJECTS SYS_OBJ, SYSINDEXES SYS_INDX
WHERE SYS_INDX.ID = SYS_OBJ.ID
AND INDID IN(0,1) --This specifies 'user' databases only
AND XTYPE = 'U' --This omits the diagrams table of the database
--You may find other system tables will need to be ommitted,
AND SYS_OBJ.NAME <> 'SYSDIAGRAMS'
ORDER BY SYS_INDX.rowcnt DESC --I found it more useful to display
--The following line adds up all the rowcount results and places
--the final result into a separate column [below the first resulting table]
COMPUTE SUM(SYS_INDX.ROWCNT)
GO
you should use
select count(1) from
If you are saying (*) it will expand all the column's and then count