To find long-running queries in SQL Server along with process ID, process name, login time, user, start time, and duration, you can use the following query:
SELECT
s.session_id AS 'Process ID',
s.program_name AS 'Process Name',
s.login_time AS 'Login Time',
s.login_name AS 'User',
r.start_time AS 'Start Time',
DATEDIFF(MINUTE, r.start_time, GETDATE()) AS 'Duration (Minutes)'
FROM
sys.dm_exec_sessions AS s
JOIN
sys.dm_exec_requests AS r
ON
s.session_id = r.session_id
WHERE
r.status IN ('running', 'runnable')
ORDER BY
r.start_time;
This query retrieves information about active sessions (sys.dm_exec_sessions) and their corresponding requests (sys.dm_exec_requests). It filters out requests that are currently running or runnable. The duration of the query is calculated by finding the difference in minutes between the start time of the request and the current time. Finally, the results are ordered by the start time of the request.
Using this query, you can identify long-running queries along with additional details such as process ID, process name, login time, user, start time, and duration.