Skip to content

Commit f3eab08

Browse files
authored
Merge pull request #11 from rahull0328/dev
added new answers
2 parents 37dc865 + f8dcd14 commit f3eab08

File tree

1 file changed

+151
-1
lines changed

1 file changed

+151
-1
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,157 @@ Text: SELECT * FROM orders WHERE amount > 1000;
18661866
</div>
18671867
18681868
#### 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.
1871+
1872+
**πŸ” Understanding @@ERROR and @@ROWCOUNT**
1873+
1874+
| Function | Description |
1875+
| ------------ | --------------------------------------------------------------------------------------------------------------- |
1876+
| `@@ERROR` | Returns the **error number** of the last executed SQL statement. If the statement was successful, it returns 0. |
1877+
| `@@ROWCOUNT` | Returns the **number of rows affected** by the last SQL statement. |
1878+
1879+
**❗ Important Note**
1880+
1881+
Both @@ERROR and @@ROWCOUNT must be captured right after the SQL statement you’re checking, otherwise their values will change.
1882+
1883+
**Example: Correct Way to Get Both @@ERROR and @@ROWCOUNT**
1884+
1885+
```sql
1886+
DECLARE @Error INT, @RowCount INT;
1887+
1888+
-- Execute a statement (example: update)
1889+
UPDATE Employees
1890+
SET Salary = Salary + 1000
1891+
WHERE Department = 'IT';
1892+
1893+
-- Immediately capture the values
1894+
SET @Error = @@ERROR;
1895+
SET @RowCount = @@ROWCOUNT;
1896+
1897+
-- Now you can use these values safely
1898+
IF @Error <> 0
1899+
BEGIN
1900+
PRINT 'An error occurred. Error code: ' + CAST(@Error AS VARCHAR);
1901+
END
1902+
ELSE
1903+
BEGIN
1904+
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.
2005+
2006+
### βš–οΈ Buffer Cache vs. Log Cache
2007+
2008+
| Feature | Buffer Cache | Log Cache |
2009+
| ------------ | ------------------------------ | ------------------------------------- |
2010+
| Stores | Data and index pages | Transaction log records |
2011+
| Purpose | Fast data retrieval and writes | Fast and reliable transaction logging |
2012+
| Persistence | Pages flushed asynchronously | Logs flushed on commit or flush event |
2013+
| Related file | Data file (.mdf, .ndf) | Log file (.ldf) |
2014+
2015+
**πŸ” Why are they important?**
2016+
2017+
- Buffer Cache improves query performance by reducing disk reads.
2018+
2019+
- Log Cache ensures transaction durability and speed, crucial for recovery and rollback.
18702020

18712021
<div align="right">
18722022
<b><a href="#table-of-contents">β†₯ back to top</a></b>

0 commit comments

Comments
Β (0)