Wednesday, July 27, 2016

T-SQL transaction control in SP

A very basic usages of transactions in order to ensure that only one transaction is active at a time.

use master
go

declare @trancount INT = @@TRANCOUNT
, @i INT = NULL

begin try
print 'start'
print @trancount

IF @trancount = 0
begin
BEGIN TRANSACTION
print 'trans'
print @trancount
end

print 'working'
print @trancount

-- Uncoment this line to create exception
-- set @i = 1/0

IF @trancount = 0
BEGIN
COMMIT
print 'commit'
print @trancount
END

END TRY
BEGIN CATCH
print 'rolling'
print @trancount
print ERROR_MESSAGE()

IF @trancount = 0
BEGIN
ROLLBACK
print 'roll'
print @trancount
END
END CATCH

Friday, March 04, 2016

One of the best C# tips

I have found this when looking around in the net and I was so amazed I had to post it here.
By experience, most of these tips I already use but there are still a couple that I never seem to memorize. Is it because I don't use it every day or just laziness I really do not know.

http://www.codeproject.com/Articles/8210/Top-tips-and-tricks-to-jazz-up-your-C-business

Hope it helps you code better.

Thursday, February 18, 2016

T-SQL Server tips

-- Validar os contadores de performance
--select *
--from sys.dm_os_performance_counters
--where counter_name like '%Buffer cache hit ratio%'


---- Overview rápido sobre o estado dos processos na BD
--exec sp_who2


---- Se aparecer clean nos três primeiros está tudo bem
-- exec sp_server_diagnostics


--- Verificar se temos transacções pendentes
---- Por cada BD
-- dbcc opentran

---- Para todas as BDs
--exec sp_MSforeachdb 'use ? dbcc opentran'