You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+151-1Lines changed: 151 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1866,7 +1866,157 @@ Text: SELECT * FROM orders WHERE amount > 1000;
1866
1866
</div>
1867
1867
1868
1868
#### Q. How to get @@ERROR and @@ROWCOUNT at the same time?
1869
-
#### Q. Explain about buffer cash and log Cache in SQL Server?
1869
+
1870
+
In SQL Server, @@ERROR and @@ROWCOUNT are two system functions that provide information about the success and outcome of the last executed SQL statement. If you want to capture both at the same time, you have to store their values immediately after the statement you are evaluating, because executing another SQL statement will overwrite these values.
PRINT 'No error. Rows affected: ' + CAST(@RowCount AS VARCHAR);
1905
+
END
1906
+
```
1907
+
1908
+
**β οΈ What Happens If You Don't Store Immediately?**
1909
+
1910
+
```sql
1911
+
-- Bad Example
1912
+
UPDATE Employees
1913
+
SET Salary = Salary + 1000
1914
+
WHERE Department = 'IT';
1915
+
1916
+
-- Let's say you do something else now
1917
+
SELECT * FROM Employees;
1918
+
1919
+
-- Now you try to capture error and rowcount
1920
+
SET @Error = @@ERROR; -- captures for SELECT, not UPDATE
1921
+
SET @RowCount = @@ROWCOUNT;-- captures for SELECT, not UPDATE
1922
+
```
1923
+
1924
+
In this case, both @@ERROR and @@ROWCOUNT reflect the result of SELECT, not the UPDATE, which makes your error handling unreliable.
1925
+
1926
+
**π§ Summary**
1927
+
1928
+
- To get @@ERROR and @@ROWCOUNT together safely:
1929
+
1930
+
- Execute your SQL statement.
1931
+
1932
+
- Immediately assign @@ERROR and @@ROWCOUNT to local variables.
1933
+
1934
+
- Use those variables for further logic or error handling.
1935
+
1936
+
<div align="right">
1937
+
<b><a href="#table-of-contents">β₯ back to top</a></b>
1938
+
</div>
1939
+
1940
+
#### Q. Explain about buffer Cache and log Cache in SQL Server?
1941
+
1942
+
In SQL Server, Buffer Cache and Log Cache are essential components of the SQL Server Memory Architecture, designed to optimize performance by reducing direct disk I/O operations.
1943
+
1944
+
**π΅ 1. Buffer Cache (Buffer Pool)**
1945
+
1946
+
**β Definition:**
1947
+
1948
+
- The Buffer Cache (also called Buffer Pool) is the memory area where SQL Server stores pages of data read from database files (MDF/NDF). These are typically 8 KB pages.
1949
+
1950
+
**π Purpose:**
1951
+
1952
+
- To reduce physical disk I/O by keeping frequently accessed data in memory.
1953
+
- Acts like a temporary memory storage for data pages.
1954
+
1955
+
**π§± What is stored in the Buffer Cache?**
1956
+
1957
+
- Data Pages (from tables)
1958
+
1959
+
- Index Pages
1960
+
1961
+
- Catalog metadata
1962
+
1963
+
- Execution plans
1964
+
1965
+
- Work tables
1966
+
1967
+
**π Read/Write Behavior:**
1968
+
1969
+
- When a query is executed, SQL Server checks the Buffer Cache to see if the required data page is already in memory.
1970
+
1971
+
- If found β Read from cache (faster).
1972
+
1973
+
- If not β Read from disk and store in cache.
1974
+
1975
+
- Modified data pages are called dirty pages.
1976
+
1977
+
- These are written to disk asynchronously by the Checkpoint or Lazy Writer process.
1978
+
1979
+
**π΄ 2. Log Cache**
1980
+
1981
+
**β Definition:**
1982
+
1983
+
- The Log Cache is a memory area used to temporarily store transaction log records before they are written to the transaction log file (LDF).
1984
+
1985
+
**π Purpose:**
1986
+
- To improve performance of write operations by batching log writes.
1987
+
1988
+
- Ensures write-ahead logging (WAL) β the log record is written before the actual data page is written to disk.
1989
+
1990
+
**π Behavior:**
1991
+
1992
+
- When a transaction (like INSERT/UPDATE/DELETE) occurs:
1993
+
1994
+
- A log record is created and stored in the Log Cache.
1995
+
1996
+
- These records are flushed to the disk (LDF file) when:
1997
+
1998
+
- The transaction is committed (COMMIT).
1999
+
2000
+
- The log cache is full.
2001
+
2002
+
- A checkpoint occurs.
2003
+
2004
+
- SQL Server guarantees durability through this mechanism.
0 commit comments