Query – AumTechHub.Com http://aumtechhub.com Just share it!!! Tue, 23 Nov 2021 01:56:57 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.2 179596041 How to get timezone information? http://aumtechhub.com/how-to-get-timezone-information/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-get-timezone-information Thu, 04 Mar 2021 12:58:56 +0000 http://aumtechhub.com/?p=219 Using UTC time and using offset values can be one of the best ways to make sure you capture the correct local time based on region. In order to get the time information directly from SQL you can run the following query.

select * from sys.time_zone_info
order by name

]]>
219
How to rerun SQL query after delay? http://aumtechhub.com/how-to-rerun-sql-query-after-delay/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-rerun-sql-query-after-delay Thu, 21 Jan 2021 15:27:28 +0000 http://aumtechhub.com/?p=203 This sample show how to run the same query with delay. It will keep running the same query with 10 second delay and keep re-executing at least 5 times. Good example to when to use this is when you want to check the status on an on going backup.

Select L.NAME DBName,R.percent_complete as [Progress%]
FROM Master.dbo.sysdatabases L Left join sys.dm_exec_requests R on L.DBID=R.Database_ID
WHERE percent_complete > 0
raiserror('',0,1) with nowait
waitfor delay '00:00:10'
GO 15

]]>
203
Database Backup Percentage Complete Status http://aumtechhub.com/database-backup-percentage-complete-status/?utm_source=rss&utm_medium=rss&utm_campaign=database-backup-percentage-complete-status Sat, 29 Aug 2020 05:37:07 +0000 http://aumtechhub.com/?p=157 When performing backup with scripts its not easily possible to get the percentage complete of the running backup. This simple script will show status of current running backup on the server.

SELECT
L.NAME DBName,
R.percent_complete as [Progress%]
FROM Master.dbo.sysdatabases L Left join sys.dm_exec_requests R on L.DBID=R.Database_ID
WHERE R.Command LIKE '%BACKUP%'

Or you can change the where clause and use the following

SELECT
L.NAME DBName,
R.percent_complete as [Progress%]
FROM Master.dbo.sysdatabases L Left join sys.dm_exec_requests R on L.DBID=R.Database_ID
WHERE percent_complete > 0

If no backups are running then the query will return no data.

]]>
157
How to get list of new or modified database objects? http://aumtechhub.com/how-to-get-list-of-new-or-modified-database-objects/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-get-list-of-new-or-modified-database-objects Sat, 29 Aug 2020 03:28:44 +0000 http://aumtechhub.com/?p=142 This simple query tells us if there were any database objects created within the last 24 hours. You can modify the DATEADD parameters to get a larger time span.

select * from sys.objects where create_date > dateadd(D, -1, getdate())

This second query tells us if there were any database objects modified within the last 24 hours. You can modify the DATEADD parameters to get a larger time span.

select * from sys.objects where modify_date > dateadd(D, -1, getdate())

]]>
142
How to get total row count of all database tables? http://aumtechhub.com/how-to-get-total-row-count-of-all-database-tables/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-get-total-row-count-of-all-database-tables Thu, 27 Aug 2020 04:29:27 +0000 http://aumtechhub.com/?p=54 This script will create a temp table and store the name of the database table and total row counts for each table in the database.

CREATE TABLE #TableRowCounts
(
TableName varchar(150),
TotalRows int
)

EXEC sp_MSForEachTable @command1=’INSERT #TableRowCounts (TableName, TotalRows) SELECT ”?”, COUNT(*) FROM ?’

SELECT TableName, TotalRows FROM #TableRowCounts ORDER BY TableName, TotalRows DESC

DROP TABLE #TableRowCounts

More on sp_MSForEachTable

]]>
54
SSRS Report Run History http://aumtechhub.com/ssrs-report-run-history/?utm_source=rss&utm_medium=rss&utm_campaign=ssrs-report-run-history Thu, 27 Aug 2020 02:29:10 +0000 http://aumtechhub.com/?p=46 The following query shows report history for a report name (Employee Attendance Report) in the WHERE clause. This is a great way to check what parameters are used and how often the report is being called.

select
[InstanceName]
, [ReportPath]
, [UserName]
, [ExecutionId]
, [RequestType]
, [Format]
, [Parameters]
, [ReportAction]
, [TimeStart]
, [TimeEnd]
, [TimeDataRetrieval]
, [TimeProcessing]
, [TimeRendering]
, [Source]
, [Status]
, [ByteCount]
, [RowCount]
, [AdditionalInfo]
FROM ExecutionLog2
Where ReportPath like '% Employee Attendance Report %'
Order by TimeStart desc

]]>
46
How to get server disk space on SQL http://aumtechhub.com/how-to-get-server-disk-space-on-sql/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-get-server-disk-space-on-sql Wed, 26 Aug 2020 00:03:11 +0000 http://aumtechhub.com/?p=29 Run the following extended stored procedure and you will get the disk space in MBs on each drive on your server.

EXEC MASTER..xp_fixeddrives​

]]>
29
How to fix DBCC errors? http://aumtechhub.com/fix-dbcc-errors/?utm_source=rss&utm_medium=rss&utm_campaign=fix-dbcc-errors Wed, 26 Aug 2020 00:01:06 +0000 http://aumtechhub.com/?p=27 How to repair your database when you get DBCC errors.

Put the database in single user mode.

ALTER DATABASE YourDBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

Go;

Then run the following code:
DBCC CHECKDB(N'YourDBName', REPAIR_ALLOW_DATA_LOSS);

Then put the database back in multi-user mode.

ALTER DATABASE [YourDBName] SET MULTI_USER;

Go;

]]>
27
Rename Database By closing connections http://aumtechhub.com/rename-database-by-closing-connections/?utm_source=rss&utm_medium=rss&utm_campaign=rename-database-by-closing-connections Tue, 25 Aug 2020 02:49:39 +0000 http://aumtechhub.com/?p=19 When trying to rename database with open database connections its always a pain as the admin has to kill those connections. Instead of going that route one can use the following code.

USE [master]
GO
ALTER DATABASE NameOfYourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE NameOfYourDatabase
Modify Name = NewNameOfYourDatabase
GO
ALTER DATABASE NewNameOfYourDatabase SET MULTI_USER;

Note: Name of the database in the query above is case sensitive.

]]>
19
How to read registry entries for SQL? http://aumtechhub.com/how-to-read-registry-entries-for-sql/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-read-registry-entries-for-sql Tue, 25 Aug 2020 00:43:16 +0000 http://aumtechhub.com/?p=14 Following query shows all the registry values on the server.

select
[registry_key]
,[value_name]
,[value_data]
from [sys].[dm_server_registry]

]]>
14