Error "DBCC execution completed" when running data connection from Excel - sql

I get this error when I try to run this data connection from Excel 2010
Connection string:
Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Data Source=sql-
server;Use Procedure for Prepare=1;Auto Translate=True;Packet
Size=4096;Workstation ID=PV-SAMSUNG;Use Encryption for Data=False;Tag with
column collation when possible=False;Initial Catalog=BVR_AUTOMAX
Command text:
EXECUTE sp_executesql N'
BEGIN
DBCC TRACEON(8765);
SELECT *
FROM OPENQUERY(SugarCRM, ''
select ticker_symbol,count(a.id) as pocet,sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as bvr, count(a.id) - sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as delta
from crm.accounts a inner join crm.users u on a.assigned_user_id=u.id
inner join crm.accounts_cstm ac on a.id=ac.id_c
where a.deleted=0
group by ticker_symbol
having delta>0 and bvr>0
order by delta desc;
'' );
END';
When I run this code in MS SQL Server Mngt Studio it works fine.
Thanks for your help
Petr

I found a solution, quite simple one. Just change this line of code to the following (add the WITH NO_INFOMSGS argument)
DBCC TRACEON(8765) WITH NO_INFOMSGS ;

Related

Importing SQL Server 2008 stored procedure into Excel 2016 I get an error

I am importing a stored procedure from SQL Server 2008 into Excel 2016, and get an error when I execute it in Excel:
Error converting data type nvarchar to date
This is the stored procedure [LA_VOTER].[Temp].[dmv_import]:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Arash B
-- Create date: 6/5/2018
-- Description: test for creating dynamic 'Data Entry Signature Verification' worksheeet
-- =============================================
ALTER PROCEDURE [Temp].[dmv_import]
#date DATE = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE #date_work DATE
IF #date IS NULL
SET #date_work = GETDATE()
ELSE
SET #date_work = #date
/*count of processed*/
SELECT
vi.import_code as 'DMV Category',
SUM(CASE WHEN CONVERT(date, vi.createdate, 101) = #date_work THEN 1 ELSE 0 END) AS 'received',
SUM(CASE WHEN CONVERT(date, vi.processed_date, 101) = #date_work THEN 1 ELSE 0 END) AS 'processed'
FROM
DIMSNet.dbo.voter_import vi
WHERE
vi.import_type IN ('DMV')
AND import_code < 13
GROUP BY
import_code
END
I access it in Excel through Data – From Other Sources shown below.
I select from SQL and enter “slavote-dr1” for server to connect to; “DIMSNet” the selected table and any random table as instructions indicated.
Once I reach the “Import Data” pop-up window I select the Cell in the excel worksheet I want to drop the table onto and click on the “properties” button.
Chose the “Definition” tab and changed command type to “SQL”.
Entered the executable in the “Command Text box”. Since the database use to connect I inserted the full path for the stored procedure on the command line.
Execute "LA_VOTER"."Temp"."dmv_import" ","
I click “OK” and return to the “Import Data” window. I click “Ok” to import the table.
I get the error stated above.
I looked at the script and could not see where this error was coming from. Is there anything that you can suggest?
PS. Added 6/12/18 ----- The SP runs fine in SQL. It is only when importing into Excel that the issue occurs.
The failure is undoubtedly in the stored procedure. It is almost certainly in these lines:
sum(case when convert(date, vi.createdate, 101) = #date_work then 1 else 0 end) as received,
sum(case when convert(date, vi.processed_date, 101) = #date_work then 1 else 0 end) as processed
I would suggest that you find the problems in the data and fix them. You can possibly fix the problem by doing:
sum(case when isdate(vi.createdate) = 0 then 0
when convert(date, vi.createdate, 101) = #date_work then 1
else 0
end) as received,
sum(case when isdate(vi.processed_date) = 0 then 0
when convert(date, vi.processed_date, 101) = #date_work
then 1
else 0
end) as processed
OK used a different method and got it working.
Used the "From Microsoft Query" and my Execute script was:
{CALL LA_VOTE.tmp.dmv_import(?)} and it imported the data I needed into the fields I wanted.
http://codebyjoshua.blogspot.com/2012/01/get-data-from-sql-server-stored.html

Running SQL Server Stored Procedure from Excel Issue

I am trying to create an Excel spreadsheet that will run a run some SQL code which I have done in the past for simple select statements but now with some more advance code I keep getting an error when I try and run the SQL Server stored procedure from Excel even though it runs fine in SQL Server Management Studio.
The code of the stored procedure is:
truncate table LAS_RPT_IST;
insert into LAS_RPT_IST (IST_Ref_no, IST_Inv_no, LDC_ACCT_ID, IST_Commodity, IST_Tax, IST_STRS, START_DT, END_DT)
(Select
th.user1_tx, th.user4_tx, ldc.ldc_acct_id,
sum(case when th.trans_sub_ty_cd = 'ISTC' then th.trans_am end) as ISTC,
sum(case when th.trans_sub_ty_cd = 'TAX' then th.trans_am end) as TAX,
sum(case when th.trans_sub_ty_cd = 'STRS' then th.trans_am end) as STRS,
th.generic1_dt, th.generic2_dt
from
transaction_history th, ldc_Account ldc
where
th.trans_creation_dt > = Convert(varchar(8),DateAdd(d, -1, Convert(datetime, N'20140930', 101)), 112)
and th.user10_tx = 'IST'
and th.ldc_acct_id = ldc.ldc_acct_id
group by
th.user1_tx, th.user4_tx, ldc.ldc_acct_id, th.generic1_dt, th.generic2_dt );
But when I put this into Excel and try and run it I get this error
I can run select statements against the DB from Excel with no issue.
Can anyone tell me what I am missing?
You'll have to run the TRUNCATE and INSERT operations in seperate commands.

Why SQL Server complains about non existing column, if the actual SQL is not executed?

I have the following SQL:
IF EXISTS (SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl'))
BEGIN
UPDATE DefaultBaseUrl SET AuthenticationTypeId = at.AuthenticationTypeId
FROM DefaultBaseUrl dbu
JOIN (
SELECT AuthenticationTypeId, CASE CodeName WHEN 'NATIVE' THEN 0 ELSE 1 END RequireNamespaceClaim
FROM AuthenticationType
) at ON dbu.RequireNamespaceClaim = at.RequireNamespaceClaim
END
Running it prints:
Msg 207, Level 16, State 1, Line 8
Invalid column name 'RequireNamespaceClaim'.
However, running
SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl')
reveals that no such column indeed exists.
So, the IF EXISTS statement is FALSE, hence the body of the if-statement does not run. However, the error is still printed.
What is going on?
How can I fix it?
SQL Server compiles the entire query and checks it for validity.
At that time the column doesn't exist.
There is a command to run an SQL from a string which is not compiled.
Have a look at (I think that's the right one):
http://technet.microsoft.com/en-us/library/ms175170(v=sql.105).aspx

SQL Job Failing but not the stored procedure

EDIT 4/18:
I want to thank everyone who has answered so far. As a test I have set up a new job which has just one step in it,
EXECUTE p_CallLog_GetAbandonedCallsForCallList
This procedure itself runs just fine and reports no errors or warnings but will not run when executed as part of a job. The error I receive when running the job is:
Executed as user: NT AUTHORITY\SYSTEM. OLE DB provider 'SQLOLEDB' reported an error. [SQLSTATE 42000] (Error 7399) [SQLSTATE 01000] (Error 7312) OLE DB error trace
[OLE/DB Provider 'SQLOLEDB' IDBInitialize::Initialize returned 0x80004005: ].
[SQLSTATE 01000] (Error 7300). The step failed.
I have tried changing the run as user selection and every other selection I try results in an error of:
Executed as user: Db2WebCal. Remote access not allowed for Windows NT user activated
by SETUSER. [SQLSTATE 42000] (Error 7410). The step failed.
All of the users I have tried do have a local login setup on the linked server.
The p_CallLog_GetAbandonedCallsForCallList procedure is as follows:
CREATE PROCEDURE [dbo].[p_CallLog_GetAbandonedCallsForCallList]
AS
BEGIN
DECLARE #SrvrName varchar(255)
DECLARE #HoursOld int --only get abandoned call that are fresher than #HoursOld
SET #HoursOld = 2 /*was normal default */
SET #SrvrName = CAST(ServerProperty('MachineName') as varchar(255))
CREATE TABLE #tmpAbandonedCalls
(
[ID] INT NULL,
StartTime DateTime NULL,
CallerIDNumber varchar(255) NULL,
CallerIDCount INT NULL,
Holdtime INT NULL,
DIDNumber varchar(20) NULL,
CustomData varchar(255) NULL,
FromFirstName varchar(100) NULL,
FromLastName varchar(100) NULL,
CallType int NULL
)
IF #SrvrName <> 'ROME' BEGIN
INSERT INTO #tmpAbandonedCalls
SELECT
cl.[ID],
cl.StartTime,
cl.CallerIDNumber,
LastAbandonedCallID.cnt as CallerIDCount,
cl.HoldTime,
right(cl.DIDNumber,10) as DIDNumber,
REPLACE(right(left(cl.CustomData,charindex(';',cl.CustomData) - 1), len(left(cl.CustomData,charindex(';',cl.CustomData) - 1)) - charindex('=',cl.CustomData)) , ' NAME','') as CustomData,
cl.FromFirstName,
cl.FromLastName,
2 AS CallType -- T_L_CallType obctAbandoned
FROM
[StrataCS.Perceptionist.local].TVDB.dbo.CallLog CL LEFT OUTER JOIN
/*=============================================
This derived table lists the CallerID's and the most
recent Call Log ID for candidate calls
=============================================*/
(
SELECT
CallerIDNumber, Max(ID) as ID, count(*) as cnt
FROM
[StrataCS.Perceptionist.local].TVDB.dbo.CallLog CL
WHERE
--Last n days
--StartTime >= DATEADD(dd,-#DaysBack,CAST(CONVERT(VARCHAR(10),GETDATE(),112) as DATETIME))
--Get calls only from within the last two hours.
StartTime >= DATEADD(hh,-2,GETDATE())
AND
LEFT(CustomData,11) = 'CompanyName'
AND
CHARINDEX('Db2ID', CustomData) > 0
AND
CallerIDNumber <> ''
AND
Len(CallerIDNumber) = 10
AND
(cl.HoldTime > 0) --0 holdtime is generally an automated call that we will not want to call again
GROUP BY
CallerIDNumber
) as LastAbandonedCallID
ON
CL.CallerIDNumber = LastAbandonedCallID.CallerIDNumber
WHERE
--Last n days
--StartTime >= DATEADD(dd,-#DaysBack,CAST(CONVERT(VARCHAR(10),GETDATE(),112) as DATETIME))
--Get calls only from within the last two hours.
cl.StartTime >= DATEADD(hh,(-1 * #HoursOld),GETDATE())
AND
--determine abandoned calls
CASE
WHEN CL.Result IN (0, 3, 11) THEN 0
WHEN CL.Result IN (1, 2) THEN 1
WHEN CL.Result IN (4, 9) THEN 2
WHEN CL.Result = 5 THEN 3
WHEN CL.Result = 6 THEN 4
WHEN CL.Result = 8 THEN 5
WHEN CL.Result = 10 THEN 6
WHEN CL.Result = 12 THEN 7
WHEN CL.Result = 13 THEN 8
WHEN CL.Result = 14 THEN 9
ELSE -CL.Result
END = 0
--Calls which have hit the call queue will have both a CompanyName and Db2ID in custom data.
AND
LEFT(CustomData,11) = 'CompanyName'
AND
CHARINDEX('Db2ID', CustomData) > 0
AND
--omit calls with no caller id -- or from IGC 6143847400
(
CL.CallerIDNumber <> ''
--OR CL.CallerIDNumber = '6143847400'
)
AND
--make sure the caller id has 10 digits
Len(CL.CallerIDNumber) = 10
AND
--The abandoned call must be the most recent call from this caller id
--CL.ID >= isnull(LastAbandonedCallID.ID,-#DaysBack)
CL.ID >= isnull(LastAbandonedCallID.ID,-1)
AND
(CL.HoldTime > 0) --0 holdtime is generally an automated call that we will not want to call again
ORDER BY
--sort by call time, most recent first.
StartTime DESC
-- Company has opted out of the Abandoned Callback program
DELETE #tmpAbandonedCalls
FROM
#tmpAbandonedCalls tmp
INNER JOIN dbo.T_CompanyPhoneSetup cps
on tmp.DIDNumber = cps.DID
INNER JOIN T_CompanyAbandonedCallbackOptOut aco
ON cps.CompanyID = aco.CompanyID
AND
aco.OptOutIsActive = 1
--Delete calls that have had a terminating outcome or have been returned within the last 20 minutes
DELETE #tmpAbandonedCalls
FROM
#tmpAbandonedCalls
INNER JOIN
(
SELECT
c.CallLogID
FROM
T_Call c (nolock)
INNER JOIN #tmpAbandonedCalls tmp
on tmp.ID = c.CallLogID
LEFT OUTER JOIN T_L_Need n
ON c.NeedID = n.NeedID
LEFT OUTER JOIN T_L_Outcome o
ON c.OutcomeID = o.OutcomeID
LEFT OUTER JOIN dbo.T_L_CallCampaignDetailStatus ccds
ON o.CCDetailStatusID = ccds.CCDetailStatusID
LEFT OUTER JOIN dbo.T_Company co
ON c.CompanyID = co.CompanyID
LEFT OUTER JOIN dbo.T_L_ProductLine pl
ON co.ProductLineID = pl.ProductLineID
LEFT OUTER JOIN T_CompanyAbandonedCallbackOptOut aco
ON (c.CompanyID = aco.CompanyID) and (aco.OptOutIsActive = 1)
GROUP BY
c.CallLogID
HAVING
--Calls that have an outcome that include at least one terminating outcome
(SUM(CAST(isnull(ccds.IsTerminal,0) as INT)) > 0)
OR
(
--Calls that have been returned less than 20 minutes ago
(SUM(CAST(isnull(ccds.IsTerminal,0) as INT)) = 0)
AND
GETDATE() <= DATEADD(mi,20,Max(c.EnteredOn))
)
OR
(
--Calls for Perceptionist Lite product line
(MAX(co.ProductLineID) = 2) -- Perceptionist Lite
)
) LastCall
ON
#tmpAbandonedCalls.[ID] = LastCall.CallLogID
END
INSERT INTO T_OutboundCallList
(TrackingID, Company, CompanyDID, Phone, CallType)
SELECT
#tmpAbandonedCalls.[ID],
#tmpAbandonedCalls.CustomData,
#tmpAbandonedCalls.DIDNumber,
#tmpAbandonedCalls.CallerIDNumber,
#tmpAbandonedCalls.CallType
FROM
#tmpAbandonedCalls
ORDER BY
StartTime
END
GO
ORIGINAL:
I have the following stored procedure that is used to fill a table with values.
PROCEDURE [dbo].[p_OutboundCallList_Create]
AS
BEGIN
TRUNCATE TABLE T_OutboundCallList
EXECUTE p_LeadVendor_GetCallsForCallList
EXECUTE p_CallCampaign_GetCallsForCallList
EXECUTE p_CallLog_GetAbandonedCallsForCallList
EXECUTE p_NoSaleFollowUp_GetCallsForCallList
END
Running this works fine and the table is filled. After creating a job and adding the following step:
EXEC p_OutboundCallList_Create
The job fails with the following error message:
Executed as user: NT AUTHORITY\SYSTEM. Warning: Null value is eliminated
by an aggregate or other SET operation. [SQLSTATE 01003] (Message 8153)
Warning: Null value is eliminated by an aggregate or other SET operation.
[SQLSTATE 01003] (Message 8153) OLE DB provider 'SQLOLEDB' reported an error.
[SQLSTATE 42000] (Error 7399) [SQLSTATE 01000] (Error 7312) OLE DB error trace
[OLE/DB Provider 'SQLOLEDB' IDBInitialize::Initialize returned 0x80004005: ].
[SQLSTATE 01000] (Error 7300). The step failed.
If I comment out the line
EXECUTE p_CallLog_GetAbandonedCallsForCallList
..the job runs fine. This stored procedure (p_CallLog_GetAbandonedCallsForCallList) does rely on a linked server and runs fine by itself and also runs fine when I run p_OutboundCallList_Create. It only fails when I run it as part of a job. I have tried running as a different user (sa, benderle, etc.) and always get the same result (failed).
"Null value is eliminated by an aggregate or other SET operation" is a SQL Server warning, which means you won't see it in the output if you run your query and this warning is raised (switch to the "Messages" tab in SQL Server Management Studio to see this).
Although it's a warning, presumably any warnings in a SQL job cause the job to error out. Fix the p_CallLog_GetAbandonedCallsForCallList procedure so it doesn't raise that warning and you job will work as expected.
I woulf be interested in seeing the code in p_CallLog_GetAbandonedCallsForCallList, as it may explain more as to why this is happening for the user NT AUTHORITY\SYSTEM.
The message occurs when you perform an aggregation (e.g. sum(), max(), count() on a data set that has a null in it).
To fix this, you may need to put an ISNULL() around the field in question, or use an INNER JOIN instead of a LEFT or RIGHT JOIN.
To disable warning that throw error in Sql Server Agent tasks.
The tasks that are launched from the SQL SERVER AGENT (scheduled), give an error, if there is a warning of the style:
"Null value is eliminated by an aggregate or other SET operation"
This message can be disabled by SET ANSI_WARNING OFF
But in some cases, there are sql statements that require this value to be on to avoid errors like:
Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This ensures consistent query semantics. Enable these options and then reissue your query
Therefore, there is no other solution than to prevent SQL SERVER AGENT from considering it an error.
This can be achieved by modifying the values ​​in the REGISTRY (REGEDIT)
Modify the entries, depending on your version of SQL SERVER:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\SQLServerAgent
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.SQL2012\SQLServerAgent
Add as 'no error' the message id you want to disable: 7405

How to see query history in SQL Server Management Studio

Is the query history stored in some log files? If yes, can you tell me how to find their location? If not, can you give me any advice on how to see it?
[Since this question will likely be closed as a duplicate.]
If SQL Server hasn't been restarted (and the plan hasn't been evicted, etc.), you may be able to find the query in the plan cache.
SELECT t.[text]
FROM sys.dm_exec_cached_plans AS p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%';
If you lost the file because Management Studio crashed, you might be able to find recovery files here:
C:\Users\<you>\Documents\SQL Server Management Studio\Backup Files\
Otherwise you'll need to use something else going forward to help you save your query history, like SSMS Tools Pack as mentioned in Ed Harper's answer - though it isn't free in SQL Server 2012+. Or you can set up some lightweight tracing filtered on your login or host name (but please use a server-side trace, not Profiler, for this).
As #Nenad-Zivkovic commented, it might be helpful to join on sys.dm_exec_query_stats and order by last_execution_time:
SELECT t.[text], s.last_execution_time
FROM sys.dm_exec_cached_plans AS p
INNER JOIN sys.dm_exec_query_stats AS s
ON p.plan_handle = s.plan_handle
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%'
ORDER BY s.last_execution_time DESC;
Late one but hopefully useful since it adds more details…
There is no way to see queries executed in SSMS by default. There are several options though.
Reading transaction log – this is not an easy thing to do because its in proprietary format. However if you need to see queries that were executed historically (except SELECT) this is the only way.
You can use third party tools for this such as ApexSQL Log and SQL Log Rescue (free but SQL 2000 only). Check out this thread for more details here SQL Server Transaction Log Explorer/Analyzer
SQL Server profiler – best suited if you just want to start auditing and you are not interested in what happened earlier. Make sure you use filters to select only transactions you need. Otherwise you’ll end up with ton of data very quickly.
SQL Server trace - best suited if you want to capture all or most commands and keep them in trace file that can be parsed later.
Triggers – best suited if you want to capture DML (except select) and store these somewhere in the database
Query history can be viewed using the system views:
sys.dm_exec_query_stats
sys.dm_exec_sql_text
sys.dm_exec_query_plan
For example, using the following query:
select top(100)
creation_time,
last_execution_time,
execution_count,
total_worker_time/1000 as CPU,
convert(money, (total_worker_time))/(execution_count*1000)as [AvgCPUTime],
qs.total_elapsed_time/1000 as TotDuration,
convert(money, (qs.total_elapsed_time))/(execution_count*1000)as [AvgDur],
total_logical_reads as [Reads],
total_logical_writes as [Writes],
total_logical_reads+total_logical_writes as [AggIO],
convert(money, (total_logical_reads+total_logical_writes)/(execution_count + 0.0)) as [AvgIO],
[sql_handle],
plan_handle,
statement_start_offset,
statement_end_offset,
plan_generation_num,
total_physical_reads,
convert(money, total_physical_reads/(execution_count + 0.0)) as [AvgIOPhysicalReads],
convert(money, total_logical_reads/(execution_count + 0.0)) as [AvgIOLogicalReads],
convert(money, total_logical_writes/(execution_count + 0.0)) as [AvgIOLogicalWrites],
query_hash,
query_plan_hash,
total_rows,
convert(money, total_rows/(execution_count + 0.0)) as [AvgRows],
total_dop,
convert(money, total_dop/(execution_count + 0.0)) as [AvgDop],
total_grant_kb,
convert(money, total_grant_kb/(execution_count + 0.0)) as [AvgGrantKb],
total_used_grant_kb,
convert(money, total_used_grant_kb/(execution_count + 0.0)) as [AvgUsedGrantKb],
total_ideal_grant_kb,
convert(money, total_ideal_grant_kb/(execution_count + 0.0)) as [AvgIdealGrantKb],
total_reserved_threads,
convert(money, total_reserved_threads/(execution_count + 0.0)) as [AvgReservedThreads],
total_used_threads,
convert(money, total_used_threads/(execution_count + 0.0)) as [AvgUsedThreads],
case
when sql_handle IS NULL then ' '
else(substring(st.text,(qs.statement_start_offset+2)/2,(
case
when qs.statement_end_offset =-1 then len(convert(nvarchar(MAX),st.text))*2
else qs.statement_end_offset
end - qs.statement_start_offset)/2 ))
end as query_text,
db_name(st.dbid) as database_name,
object_schema_name(st.objectid, st.dbid)+'.'+object_name(st.objectid, st.dbid) as [object_name],
sp.[query_plan]
from sys.dm_exec_query_stats as qs with(readuncommitted)
cross apply sys.dm_exec_sql_text(qs.[sql_handle]) as st
cross apply sys.dm_exec_query_plan(qs.[plan_handle]) as sp
WHERE st.[text] LIKE '%query%'
Current running queries can be seen using the following script:
select ES.[session_id]
,ER.[blocking_session_id]
,ER.[request_id]
,ER.[start_time]
,DateDiff(second, ER.[start_time], GetDate()) as [date_diffSec]
, COALESCE(
CAST(NULLIF(ER.[total_elapsed_time] / 1000, 0) as BIGINT)
,CASE WHEN (ES.[status] <> 'running' and isnull(ER.[status], '') <> 'running')
THEN DATEDIFF(ss,0,getdate() - nullif(ES.[last_request_end_time], '1900-01-01T00:00:00.000'))
END
) as [total_time, sec]
, CAST(NULLIF((CAST(ER.[total_elapsed_time] as BIGINT) - CAST(ER.[wait_time] AS BIGINT)) / 1000, 0 ) as bigint) as [work_time, sec]
, CASE WHEN (ER.[status] <> 'running' AND ISNULL(ER.[status],'') <> 'running')
THEN DATEDIFF(ss,0,getdate() - nullif(ES.[last_request_end_time], '1900-01-01T00:00:00.000'))
END as [sleep_time, sec] --Время сна в сек
, NULLIF( CAST((ER.[logical_reads] + ER.[writes]) * 8 / 1024 as numeric(38,2)), 0) as [IO, MB]
, CASE ER.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'ReadUncommited'
WHEN 2 THEN 'ReadCommited'
WHEN 3 THEN 'Repetable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot'
END as [transaction_isolation_level_desc]
,ER.[status]
,ES.[status] as [status_session]
,ER.[command]
,ER.[percent_complete]
,DB_Name(coalesce(ER.[database_id], ES.[database_id])) as [DBName]
, SUBSTRING(
(select top(1) [text] from sys.dm_exec_sql_text(ER.[sql_handle]))
, ER.[statement_start_offset]/2+1
, (
CASE WHEN ((ER.[statement_start_offset]<0) OR (ER.[statement_end_offset]<0))
THEN DATALENGTH ((select top(1) [text] from sys.dm_exec_sql_text(ER.[sql_handle])))
ELSE ER.[statement_end_offset]
END
- ER.[statement_start_offset]
)/2 +1
) as [CURRENT_REQUEST]
,(select top(1) [text] from sys.dm_exec_sql_text(ER.[sql_handle])) as [TSQL]
,(select top(1) [objectid] from sys.dm_exec_sql_text(ER.[sql_handle])) as [objectid]
,(select top(1) [query_plan] from sys.dm_exec_query_plan(ER.[plan_handle])) as [QueryPlan]
,NULL as [event_info]--(select top(1) [event_info] from sys.dm_exec_input_buffer(ES.[session_id], ER.[request_id])) as [event_info]
,ER.[wait_type]
,ES.[login_time]
,ES.[host_name]
,ES.[program_name]
,cast(ER.[wait_time]/1000 as decimal(18,3)) as [wait_timeSec]
,ER.[wait_time]
,ER.[last_wait_type]
,ER.[wait_resource]
,ER.[open_transaction_count]
,ER.[open_resultset_count]
,ER.[transaction_id]
,ER.[context_info]
,ER.[estimated_completion_time]
,ER.[cpu_time]
,ER.[total_elapsed_time]
,ER.[scheduler_id]
,ER.[task_address]
,ER.[reads]
,ER.[writes]
,ER.[logical_reads]
,ER.[text_size]
,ER.[language]
,ER.[date_format]
,ER.[date_first]
,ER.[quoted_identifier]
,ER.[arithabort]
,ER.[ansi_null_dflt_on]
,ER.[ansi_defaults]
,ER.[ansi_warnings]
,ER.[ansi_padding]
,ER.[ansi_nulls]
,ER.[concat_null_yields_null]
,ER.[transaction_isolation_level]
,ER.[lock_timeout]
,ER.[deadlock_priority]
,ER.[row_count]
,ER.[prev_error]
,ER.[nest_level]
,ER.[granted_query_memory]
,ER.[executing_managed_code]
,ER.[group_id]
,ER.[query_hash]
,ER.[query_plan_hash]
,EC.[most_recent_session_id]
,EC.[connect_time]
,EC.[net_transport]
,EC.[protocol_type]
,EC.[protocol_version]
,EC.[endpoint_id]
,EC.[encrypt_option]
,EC.[auth_scheme]
,EC.[node_affinity]
,EC.[num_reads]
,EC.[num_writes]
,EC.[last_read]
,EC.[last_write]
,EC.[net_packet_size]
,EC.[client_net_address]
,EC.[client_tcp_port]
,EC.[local_net_address]
,EC.[local_tcp_port]
,EC.[parent_connection_id]
,EC.[most_recent_sql_handle]
,ES.[host_process_id]
,ES.[client_version]
,ES.[client_interface_name]
,ES.[security_id]
,ES.[login_name]
,ES.[nt_domain]
,ES.[nt_user_name]
,ES.[memory_usage]
,ES.[total_scheduled_time]
,ES.[last_request_start_time]
,ES.[last_request_end_time]
,ES.[is_user_process]
,ES.[original_security_id]
,ES.[original_login_name]
,ES.[last_successful_logon]
,ES.[last_unsuccessful_logon]
,ES.[unsuccessful_logons]
,ES.[authenticating_database_id]
,ER.[sql_handle]
,ER.[statement_start_offset]
,ER.[statement_end_offset]
,ER.[plan_handle]
,NULL as [dop]--ER.[dop]
,coalesce(ER.[database_id], ES.[database_id]) as [database_id]
,ER.[user_id]
,ER.[connection_id]
from sys.dm_exec_requests ER with(readuncommitted)
right join sys.dm_exec_sessions ES with(readuncommitted)
on ES.session_id = ER.session_id
left join sys.dm_exec_connections EC with(readuncommitted)
on EC.session_id = ES.session_id
where ER.[status] in ('suspended', 'running', 'runnable')
or exists (select top(1) 1 from sys.dm_exec_requests as ER0 where ER0.[blocking_session_id]=ES.[session_id])
This request displays all active requests and all those requests that explicitly block active requests.
All these and other useful scripts are implemented as representations in the SRV database, which is distributed freely.
For example, the first script came from the view [inf].[vBigQuery], and the second came from view [inf].[vRequests].
There are also various third-party solutions for query history.
I use Query Manager from Dbeaver:
and Query Execution History from SQL Tools, which is embedded in SSMS:
You can Monitor SQL queries by SQL Profiler if you need it
The system doesn't record queries in that way. If you know you want to do that ahead of time though, you can use SQL Profiler to record what is coming in and track queries during the time Profiler is running.
As others have noted, you can use SQL Profiler, but you can also leverage it's functionality through sp_trace_* system stored procedures. For example, this SQL snippet will (on 2000 at least; I think it's the same for SQL 2008 but you'll have to double-check) catch RPC:Completed and SQL:BatchCompleted events for all queries that take over 10 seconds to run, and save the output to a tracefile that you can open up in SQL profiler at a later date:
DECLARE #TraceID INT
DECLARE #ON BIT
DECLARE #RetVal INT
SET #ON = 1
exec #RetVal = sp_trace_create #TraceID OUTPUT, 2, N'Y:\TraceFile.trc'
print 'This trace is Trace ID = ' + CAST(#TraceID AS NVARCHAR)
print 'Return value = ' + CAST(#RetVal AS NVARCHAR)
-- 10 = RPC:Completed
exec sp_trace_setevent #TraceID, 10, 1, #ON -- Textdata
exec sp_trace_setevent #TraceID, 10, 3, #ON -- DatabaseID
exec sp_trace_setevent #TraceID, 10, 12, #ON -- SPID
exec sp_trace_setevent #TraceID, 10, 13, #ON -- Duration
exec sp_trace_setevent #TraceID, 10, 14, #ON -- StartTime
exec sp_trace_setevent #TraceID, 10, 15, #ON -- EndTime
-- 12 = SQL:BatchCompleted
exec sp_trace_setevent #TraceID, 12, 1, #ON -- Textdata
exec sp_trace_setevent #TraceID, 12, 3, #ON -- DatabaseID
exec sp_trace_setevent #TraceID, 12, 12, #ON -- SPID
exec sp_trace_setevent #TraceID, 12, 13, #ON -- Duration
exec sp_trace_setevent #TraceID, 12, 14, #ON -- StartTime
exec sp_trace_setevent #TraceID, 12, 15, #ON -- EndTime
-- Filter for duration [column 13] greater than [operation 2] 10 seconds (= 10,000ms)
declare #duration bigint
set #duration = 10000
exec sp_trace_setfilter #TraceID, 13, 0, 2, #duration
You can find the ID for each trace-event, columns, etc from Books Online; just search for the sp_trace_create, sp_trace_setevent and sp_trace_setfiler sprocs. You can then control the trace as follows:
exec sp_trace_setstatus 15, 0 -- Stop the trace
exec sp_trace_setstatus 15, 1 -- Start the trace
exec sp_trace_setstatus 15, 2 -- Close the trace file and delete the trace settings
...where '15' is the trace ID (as reported by sp_trace_create, which the first script kicks out, above).
You can check to see what traces are running with:
select * from ::fn_trace_getinfo(default)
The only thing I will say in caution -- I do not know how much load this will put on your system; it will add some, but how big that "some" is probably depends how busy your server is.
I give my credit to all answer before me here.
This script show how to find top 'slowest' 20 queries that affect your SQL
select top 20 dest.text, deqs.execution_count, deqs.total_elapsed_time, deqs.total_worker_time,
(deqs.total_elapsed_time / deqs.execution_count) as 'avg_elapse_time',
(deqs.total_worker_time / deqs.execution_count) as 'avg_worker_time'
from sys.dm_exec_query_stats as deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) as dest
where deqs. last_execution_time >= '2021-09-27 16:00' /* YOUR DATE AND TIME HERE*/
order by 'avg_elapse_time' desc
SELECT deqs.last_execution_time AS [Time], dest.text AS [Query], dest.*
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE dest.dbid = DB_ID('msdb')
ORDER BY deqs.last_execution_time DESC
This should show you the time and date of when a query was ran
I use the below query for tracing application activity on a SQL server that does not have trace profiler enabled.
The method uses Query Store (SQL Server 2016+) instead of the DMV's. This gives better ability to look into historical data, as well as faster lookups.
It is very efficient to capture short-running queries that can't be captured by sp_who/sp_whoisactive.
/* Adjust script to your needs.
Run full script (F5) -> Interact with UI -> Run full script again (F5)
Output will contain the queries completed in that timeframe.
*/
/* Requires Query Store to be enabled:
ALTER DATABASE <db> SET QUERY_STORE = ON
ALTER DATABASE <db> SET QUERY_STORE (OPERATION_MODE = READ_WRITE, MAX_STORAGE_SIZE_MB = 100000)
*/
USE <db> /* Select your DB */
IF OBJECT_ID('tempdb..#lastendtime') IS NULL
SELECT GETUTCDATE() AS dt INTO #lastendtime
ELSE IF NOT EXISTS (SELECT * FROM #lastendtime)
INSERT INTO #lastendtime VALUES (GETUTCDATE())
;WITH T AS (
SELECT
DB_NAME() AS DBName
, s.name + '.' + o.name AS ObjectName
, qt.query_sql_text
, rs.runtime_stats_id
, p.query_id
, p.plan_id
, CAST(p.last_execution_time AS DATETIME) AS last_execution_time
, CASE WHEN p.last_execution_time > #lastendtime.dt THEN 'X' ELSE '' END AS New
, CAST(rs.last_duration / 1.0e6 AS DECIMAL(9,3)) last_duration_s
, rs.count_executions
, rs.last_rowcount
, rs.last_logical_io_reads
, rs.last_physical_io_reads
, q.query_parameterization_type_desc
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY plan_id, runtime_stats_id ORDER BY runtime_stats_id DESC) AS recent_stats_in_current_priod
FROM sys.query_store_runtime_stats
) AS rs
INNER JOIN sys.query_store_runtime_stats_interval AS rsi ON rsi.runtime_stats_interval_id = rs.runtime_stats_interval_id
INNER JOIN sys.query_store_plan AS p ON p.plan_id = rs.plan_id
INNER JOIN sys.query_store_query AS q ON q.query_id = p.query_id
INNER JOIN sys.query_store_query_text AS qt ON qt.query_text_id = q.query_text_id
LEFT OUTER JOIN sys.objects AS o ON o.object_id = q.object_id
LEFT OUTER JOIN sys.schemas AS s ON s.schema_id = o.schema_id
CROSS APPLY #lastendtime
WHERE rsi.start_time <= GETUTCDATE() AND GETUTCDATE() < rsi.end_time
AND recent_stats_in_current_priod = 1
/* Adjust your filters: */
-- AND (s.name IN ('<myschema>') OR s.name IS NULL)
UNION
SELECT NULL,NULL,NULL,NULL,NULL,NULL,dt,NULL,NULL,NULL,NULL,NULL,NULL, NULL
FROM #lastendtime
)
SELECT * FROM T
WHERE T.query_sql_text IS NULL OR T.query_sql_text NOT LIKE '%#lastendtime%' -- do not show myself
ORDER BY last_execution_time DESC
TRUNCATE TABLE #lastendtime
INSERT INTO #lastendtime VALUES (GETUTCDATE())
If you want an history for the queries you executed through SMSS.
You may want to try SSMSPlus.
https://github.com/akarzazi/SSMSPlus
This feature does not exists out of the box in SSMS.
You'll need SSMS 18 or newer.
Disclaimer : I'm the author.
The SQL query below can show simple query logs:
SELECT last_execution_time, text
FROM sys.dm_exec_query_stats stats
CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle)
ORDER BY last_execution_time
This is how it looks like below:
And, the SQL query below can show simple transaction query logs:
SELECT Operation, [Begin Time], [End Time]
FROM fn_dblog(NULL,NULL)
This is how it looks like below:
In addition, but I don't know the SQL query to show both simple query logs and simple transaction query logs together.
you can use "Automatically generate script on every save", if you are using management studio.
This is not certainly logging.
Check if useful for you.. ;)
If the queries you are interested in are dynamic queries that fail intermittently, you could log the SQL and the datetime and user in a table at the time the dynamic statement is created. It would be done on a case-by case basis though as it requires specific programming to happen and it takes a littel extra processing time, so do it only for those few queries you are most concerned about. But having a log of the specific statements executed can really help when you are trying to find out why it fails once a month only. Dynamic queries are hard to thoroughly test and sometimes you get one specific input value that just won't work and doing this logging at the time the SQL is created is often the best way to see what specifically wasn in the sql that was built.
A slightly out-of-the-box method would be to script up a solution in AutoHotKey. I use this, and it's not perfect, but works and is free. Essentially, this script assigns a hotkey to CTRL+SHIFT+R which will copy the selected SQL in SSMS (CTRL+C), save off a datestamp SQL file, and then execute the highlighted query (F5). If you aren't used to AHK scripts, the leading semicolon is a comment.
;CTRL+SHIFT+R to run a query that is first saved off
^+r::
;Copy
Send, ^c
; Set variables
EnvGet, HomeDir, USERPROFILE
FormatTime, DateString,,yyyyMMdd
FormatTime, TimeString,,hhmmss
; Make a spot to save the clipboard
FileCreateDir %HomeDir%\Documents\sqlhist\%DateString%
FileAppend, %Clipboard%, %HomeDir%\Documents\sqlhist\%DateString%\%TimeString%.sql
; execute the query
Send, {f5}
Return
The biggest limitations are that this script won't work if you click "Execute" rather than use the keyboard shortcut, and this script won't save off the whole file - just the selected text. But, you could always modify the script to execute the query, and then select all (CTRL+A) before the copy/save.
Using a modern editor with "find in files" features will let you search your SQL history. You could even get fancy and scrape your files into a SQLite3 database to query your queries.