Hello whenever I select in a new query from the system view sys.dm_tran_session_transactions I get always 0 rows
select * from sys.dm_tran_session_transactions
But people on some other answers use this table.
SQL Server 2005 : map Transaction_ID to ##SPID
Why have I empty this view?
The sys.dm_tran_session_transactions DMV will return sessions with open transactions so it seems there were no such sessions when you ran the query. Start a transaction to see the an example of data returned:
BEGIN TRAN;
SELECT * FROM sys.dm_tran_session_transactions;
ROLLBACK;
Related
Problem: To solve data consistency on one of most important tables in our system, all writes will be covered with explicit transactions (appropriate to what given stored procedure / business application is doing). However, since there are many sources of writes, it would be best if we had numerical measure of progress.
Ratio of explicit to autocommited transactions on that particular table would be one such measure. If after all the rework, there are still autocommit transactions in stats, we have proof that we missed some procedure or application (or manual process).
How can SQL server be queried for such information?
As mentioned by #lptr you can use sys.dm_tran_session_transactions to check if a user transaction was opened.
CREATE OR ALTER TRIGGER dbo.check_tran
ON dbo.YourTable
AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON;
IF (SELECT is_user_transaction FROM sys.dm_tran_session_transactions WHERE session_id = ##SPID) = 1
BEGIN
INSERT Audit_transactions (host_name, user_name, initial_batch)
SELECT HOST_NAME(), SUSER_SNAME(), CONCAT(b.parameters, ' ', b.event_info)
FROM sys.dm_exec_requests r
OUTER APPLY sys.dm_exec_input_buffer (##SPID, r.request_id) b
WHERE r.session_id = ##SPID
END;
sys.dm_tran_session_transactions requires VIEW SERVER STATE permissions, so if the user does not have that then you may need to sign the trigger with a certificate of a user that has that permission.
I have some mysterious problem where every day one table in DB (SQL Server 2016) is being recreated (I suppose dropped and created) with old data. I checked various options to try to find what process is doing this, however was unable to do that.
Scheduled Tasks - nothing
SQL Agent Jobs - nothing
How to trace what user/application/anythingelse is doing this ?
I tried launching SQL Profiler and starting manual trace, but after some time (half a day or so) it just stopped.
The default trace captures schema changes. Review the Schema Change History report or run the query below to retrieve the info in T-SQL. Note that the default trace rollover files are limited to 5 files of up to 20MB each so older events may have rolled off.
--select object created and deleted information from default trace
SELECT
trace_table.StartTime
, te.name
, trace_table.ObjectName
, trace_table.ApplicationName
, trace_table.LoginName
FROM (
SELECT REVERSE(SUBSTRING(REVERSE(path), CHARINDEX('\', REVERSE(path)) , 255)) + 'log.trc'
FROM sys.traces
WHERE
is_default = 1
) AS trace(path)
CROSS APPLY sys.fn_trace_gettable(trace.path, DEFAULT) AS trace_table
JOIN sys.trace_events AS te ON
te.trace_event_id = trace_table.EventClass
WHERE
EventSubClass = 0
AND name IN('Object:Created', 'Object:Deleted')
ORDER BY StartTime;
create a database trigger and log the create/drop table events:
create table dbo.traceTabledropcreate(EventDataXML xml, LogDatetime datetime default(getdate()));
go
create or alter trigger dbtrigger_traceTabledropcreate
on database
with execute as 'dbo'
for CREATE_TABLE, DROP_TABLE
as
begin
set nocount on;
--insert into dbo.traceTabledropcreate(EventDataXML)
--values (EVENTDATA());
declare #sessionxml xml =
(
select EVENTDATA(),
(
select *
from sys.dm_exec_sessions
where session_id = ##spid
for xml path('sessioninfo'), type
)
for xml path('')
);
insert into dbo.traceTabledropcreate(EventDataXML)
values (#sessionxml);
end
go
---..... and wait....
--..test
create table dbo.testtable(id int)
go
select *
from dbo.traceTabledropcreate
go
drop table dbo.testtable
go
select *
from dbo.traceTabledropcreate
go
I am using TOAD for SQL Server 6.8.2.9 (64 Bit) Freeware. Is it possible to see the results of all select statements in the result sets of a transaction? For example when I run
BEGIN TRANSACTION;
SELECT *
FROM FORM.QuestionStore
WHERE QuestionId = 890;
UPDATE FORM.QuestionStore
SET QuestionSubText = 'test'
WHERE QuestionId = 890;
SELECT *
FROM FORM.QuestionStore
WHERE QuestionId = 890;
ROLLBACK;
In my result sets tab I see the results of the second select statement (the one after the update) but I do not see the results of the first (the one before the update). Is there an option or setting to see both of them?
Why this code gives 1 in new query window of SSMS?
select ##ROWCOUNT
SSMS issues several queries after it creates a connection. You can see this using SQL Server Profiler
on my system,
select ##spid;
select SERVERPROPERTY('ProductLevel');
is the last query executed.
When you create a new connection, your SSMS window automatically sets a number of session variables for you.
Sample trace of a new session being created:
select ##spid;
select SERVERPROPERTY('ProductLevel');
As a test you can execute the following statement, and confirm that the result will always be 1.
select ##spid;
select SERVERPROPERTY('ProductLevel');
select ##ROWCOUNT
When I execute a sql statement like "Select ...", I can only see "...100%" completed...
I want to log the number of rows affected.
How can we do that?
run your SELECT from within a stored procedure, where you can log the rowcount into a table, or do anything else to record it...
CREATE PROCEDURE SSIS_TaskA
AS
DECLARE #Rows int
SELECT ... --your select goes here
SELECT #Rows=##ROWCOUNT
INSERT INTO YourLogTable
(RunDate,Message)
VALUES
(GETDATE(),'Selected '+CONVERT(varchar(10),ISNULL(#Rows,0))+' rows in SSIS_TaskA')
GO
When you use a SQL Task for a select most of the time you give as destination a DataSet Object, you can count the number of ligne from the DataSet
I believe you could leverage a t-sql output clause on your update or insert statement and capture that as an ssis variable....or just drop it into a sql table.
here is an example...its crappy, but it is an example
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25
OUTPUT INSERTED.EmployeeID,
DELETED.VacationHours,
INSERTED.VacationHours,
INSERTED.ModifiedDate
INTO #MyTableVar;
You could output ##ROWCOUNT anyplace you need it to be.
Here is output syntax
http://technet.microsoft.com/en-us/library/ms177564.aspx