How to discover the last time SQL Server was restarted? - sql-server

What command could be used to discover the last datetime SQL Server was restarted?

SELECT MIN ([login_time]) FROM sysprocesses;
Source:
"What is the current uptime of SQL Server?"

If you talking about Microsoft's SQL Server 2005 or above, MSDN's docs about sys.databases says:
create_date
Date the database was created or renamed. For tempdb, this value changes every time the server restarts.
So, this should be what you need:
SELECT create_date FROM sys.databases WHERE NAME='tempdb'

Related

SQL Server returns incorrect XML value for parent node after installation of CU14

We recently updated SQL Server from:
Microsoft SQL Server 2017 (RTM-CU9-GDR) (KB4293805) - 14.0.3035.2 (X64)
to:
Microsoft SQL Server 2017 (RTM-CU14-GDR) (KB4494352) - 14.0.3103.1 (X64)
In one of the extraction jobs we are using SQL like:
SELECT
XMLNode_Root.R.value('./#ID', 'INTEGER') AS [SellerID],
CAST(XMLNode_Root.R.value('../#ID', 'VARCHAR(25)') AS BIGINT) AS [ApplicationID]
FROM Stage.LoanAppl_XML AS AD
CROSS APPLY AD.ApplicationXMLPackage.nodes('//MessageResponse/body/Application/Seller') AS XMLNode_Root(R)
OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE'));
Recently, on random occasions this select started to return NULL values for ApplicationID column even the ID is defined in the parent.
What is interesting:
1. If I do any change to SQL Select, like change case or add spaces it returns correct values.
2. Running dbcc freeproccache solves the problem for some time.
I have execution plan saved before and after clearing cache, but can't post it now.
I wonder if anybody had similar issue and was able to find a solution.

SQL Server Management Studio Express not showing recently added data

I recently installed and have been using Microsoft SQL Server Management Studio Express 9.00.4035.00 to manage a remote database on my hosts' SQL Server 2005.
Until now I had been using EMS SQL Manager 2011 Lite and that was working fine.
2 new rows where inserted into one of my tables this morning; one by a customer signing up for a service and the other as a test signup by me.
When I run a typical select query:
[Select top 20 * From tblNotary Order By ID Desc]
I don't see the the 2 most recent rows. But when I run the same query from EMS SQL Manager Lite I see the records.
I also verified when connecting using MS Access 2010 I see the 2 new rows in the table. I have checked and double-checked the connection settings and they match EMS.
Is there a setting or something obvious I am missing? Why can't I see the most recent record insertions? I am on a Windows 7 desktop machine.
The most likely reason is you are connecting to a different database than you expected. You can select ##servername to verify both queries are running against the same server.
If the records are stil being inserted as part of an open transaction and have not been committed, they are called "phantom" rows. You will not see phantom rows if your query runs at transaction isolation level read committed or higher. It may be that EMS SQL Manager Lite is running at read uncommitted, in which case it will include phantom rows in the query.
Sometimes connection strings are a tricky thing. Run this to double check:
select ##servername servername, name, crdate
from sys.sysdatabases
where name = db_name()
if you have an issue running that, just do a simpler version:
select ##servername, db_name(), ##version
Select top 20 * From tblNotary Order By ID Desc

Check database access (SQL Server 2005 / 2008)

What is the best way to check database access from remote machine.
Here is the situation:
SQL Server 2005 (or 2008) is running on MACHINE1 (Windows 2003 or 2008). There is a database called ENTERPRISEDB.
Another server, called MACHINE2 (Windows 2003 or 2008). It has ODBC connection to MACHINE1.
Now, what is the best and simplest SQL query to check if MACHINE2 can connect and access the ENTERPRISEDB in MACHINE1?
Thanks.
If your ODBC connection is to database ENTERPRISEDB then you could run
SELECT top 1 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'
which will return "1" if the database exists or nothing if it doesn't.
If you need a boolean/int return then try
SELECT case when COUNT(*) >0 then 1 else 0 end
FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='ENTERPRISEDB'
which will return "1" if it exists, "0" if it doesn't.
I always use
Select ##servername, ##SERVICENAME
Returns Server Name, Instance Name to check basic server connection. To check database connectivity itself is correct you can't beat sys.objects.
select DB_NAME(), * from sys.objects
Returns a table with current database name as the first column and a list of object names as the general data list.

On SQL Server 2008, how to find out when was any database offline/online?

I have to include one report in my application showing offline/online activity of few databases on SQL Server 2008.
Could you please suggest how can I collect teh same information from sql server?
SELECT DATABASEPROPERTYEX('YOURDATABASE', 'Status')
DatabaseStatus_DATABASEPROPERTYEX
GO
SELECT state_desc DatabaseStatus_sysDatabase
FROM sys.databases
WHERE name = 'YOURDATABASE'
GO
This will tell you the status of the database.
In order to find out when your database was taken OFFLINE, you can use the SQL that I posted before, or the easiest way is to check the Event Viewer and it will tell you when the Database was taken OFFLINE. I have just tested this on my local machine and SQL Server writes out an Information message to the Application log.
You can also use below query to check database status.
SELECT Name, state_desc FROM sys.databases

User access log to SQL Server

I need to get a log of user access to our SQL Server so I can track average and peak concurrency usage. Is there a hidden table or something I'm missing that has this information for me? To my knowledge the application I'm looking at does not track this at the application level.
I'm currently working on SQL Server 2000, but will moving to SQL Server 2005 shortly, so solutions for both are greatly appreciated.
In SQL Server 2005, go to tree view on the left and select Server (name of the actual server) > Management > Activity Monitor. Hope this helps.
on 2000 you can use sp_who2 or the dbo.sysprocesses system table
on 2005 take a look at the sys.dm_exec_sessions DMV
Below is an example
SELECT COUNT(*) AS StatusCount,CASE status
WHEN 'Running' THEN 'Running - Currently running one or more requests'
WHEN 'Sleeping ' THEN 'Sleeping - Currently running no requests'
ELSE 'Dormant – Session is in prelogin state' END status
FROM sys.dm_exec_sessions
GROUP BY status

Resources