Thursday, March 26, 2026

XACT_ABORT

SELECT CASE 
    WHEN ( ( ( @@OPTIONS & 512 ) = 512 ) ) THEN 'ON' 
    ELSE 'OFF' 
END AS XACT_ABORT_STATUS;

-- When XACT_ABORT is ON → any runtime error rolls back the entire transaction automatically.
-- When OFF → some errors (like PK violations) do not abort the transaction, and subsequent statements still run.

SET XACT_ABORT ON;

CREATE TABLE MyTable (
    Id INT PRIMARY KEY,
    Name VARCHAR(100)
);


SELECT * FROM MyTable;

BEGIN TRANSACTION;
INSERT INTO MyTable (Id, Name) VALUES (1, 'Alice');
INSERT INTO MyTable (Id, Name) VALUES (1, 'Bob');
-- INSERT INTO MyTable (Id, Name)
-- SELECT 1, 'Bob'
-- WHERE NOT EXISTS (
--     SELECT 1
--     FROM MyTable
--     WHERE Id = 1
-- );
SELECT * FROM MyTable;
commit;

SELECT * FROM MyTable;

Output is

Output:

303 ms
XACT_ABORT_STATUS
-----------------
ON               
Id          Name                                                                                                
----------- ----------------------------------------------------------------------------------------------------
Msg 2627, Level 14, State 1, Server 6f1d64a248a3, Line 23
Violation of PRIMARY KEY constraint 'PK__MyTable__3214EC07149F63D5'. Cannot insert duplicate key in object 'dbo.MyTable'. The duplicate key value is (1).

Eğer OFF yaparsak
Output:

433 ms
XACT_ABORT_STATUS
-----------------
ON               
Id          Name                                                                                                
----------- ----------------------------------------------------------------------------------------------------
Msg 2627, Level 14, State 1, Server de2db788b1d3, Line 23
Violation of PRIMARY KEY constraint 'PK__MyTable__3214EC078BA094A9'. Cannot insert duplicate key in object 'dbo.MyTable'. The duplicate key value is (1).
The statement has been terminated.
Id          Name                                                                                                
----------- ----------------------------------------------------------------------------------------------------
          1 Alice                                                                                               
Id          Name                                                                                                
----------- ----------------------------------------------------------------------------------------------------
          1 Alice   


Wednesday, March 25, 2026

DuplicateKeyException

Giriş

Elimizde legacy bir veri tabanı var. Primary Key (PK) alanların datetime + ms şeklinde tasarlanmış. Böylece satırların kolayca sıralanabileceği düşünülmüş. Ancak bir problem var. Yazma istekleri arttıkça DuplicateKeyException hataları alıyoruz. Yani aslında veri tabanı saati PK için iyi bir kaynak değil. Çünkü aynı anda gelen iki istek için aynı zaman değerini verebiliyor


Ancak PK olarak şu seçenekleri değerlendirebiliriz

  • - Sequence kullanılsaydı harika olurdu
  • - Snowflake Id kullanılsaydı yine olurdu  ancak o zamanlar Snowflake yoktu
  • - UUIDv4 kullanamıyoruz, çünkü sırayı kaybediyoruz
  • - UUIDv7 kullanılabilir, sırayı muhafaza ederiz
  • - PK'ya bir alan daha ilave edebilsek, rastgele bir şey örneğin threadId problemi azaltır, tam çözmez


Wednesday, March 11, 2026

Table Partitioning

Giriş
Data Loding yaparken kullanılabilecek yöntemler şöyle
1. bcp
2. BULK INSERT
3. SQL Server Integration Services (SSIS)

Açıklaması şöyle
Table partitioning is one of the most powerful strategies for improving load performance while maintaining data availability. At a financial services company processing billions of monthly transactions, implementing partition switching transformed their loading process. Instead of inserting directly into the main table and blocking queries, we loaded an identical staging table and instantly switched it into the partitioned main table: 
Örnek
Şöyle yaparız
-- Create staging table with same structure as target partition
SELECT * INTO Sales.Transactions_Staging
FROM Sales.Transactions
WHERE 1 = 0;

-- Load staging table (much faster than loading production table)
BULK INSERT Sales.Transactions_Staging
FROM '\\FileServer\Imports\NewTransactions.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');

-- Switch staging table into partition in an instant operation
ALTER TABLE Sales.Transactions
SWITCH PARTITION 12 TO Sales.Transactions_Staging;

Thursday, June 19, 2025

msdb Veri Tabanı - System Database

Giriş
Açıklaması şöyle
In SQL Server environments, system databases play crucial roles in ensuring smooth and reliable database operations. Among these, the msdb database is critical as it handles a variety of operational tasks, including job scheduling via SQL Server Agent, alert management, database mail configuration, and backup and restore history tracking. These functions are essential for automating routine maintenance, monitoring system health, and managing administrative workflows.
Açıklaması şöyle
msdb is a system database that stores metadata and configuration for:
  • SQL Server Agent jobs

  • Backup and restore history

  • Database mail

  • Service Broker

  • Log shipping

  • Maintenance plans

  • Alerts and operators

  • SSIS package storage (when using MSDB storage)

Sunday, April 6, 2025

Sütun Tipleri - REAL

Giriş
Açıklaması şöyle
- Equivalent to a 32-bit floating-point number (single precision)
- IEEE standard floating-point value (7-digit precision)
- Storage size: 4 bytes
- Synonym: FLOAT(24)

Sunday, February 16, 2025

ORDER BY + OFFSET + FETCH - Sayfalama İçindir

Örnek
SQL Server 2012 ve sonrasında şöyle yaparız
SELECT * FROM Employees ORDER BY EmployeeID ASC
OFFSET 10 ROWS         -- Skip the first 10 rows
FETCH NEXT 5 ROWS ONLY; -- Fetch the next 5 rows
Örnek
SQL Server 2008 ve öncesinde şöyle yapılır
SELECT TOP 5 *
FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY EmployeeID ASC) AS RowNum
    FROM Employees
) AS SubQuery
WHERE RowNum > 10;




Monday, February 10, 2025

DROP DATABASE

Örnek
Force drop için şöyle yaparız
-- Set the database to SINGLE_USER mode with ROLLBACK IMMEDIATE to kill all connections
ALTER DATABASE my_new_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

-- Drop the database
DROP DATABASE my_new_db;



XACT_ABORT

SELECT CASE      WHEN ( ( ( @@OPTIONS & 512 ) = 512 ) ) THEN 'ON'      ELSE 'OFF'  END AS XACT_ABORT_STATUS; -- When XAC...