<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>học sql - HaiNH</title>
	<atom:link href="https://hainh.dev/tag/hoc-sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://hainh.dev</link>
	<description></description>
	<lastBuildDate>Fri, 14 Nov 2025 07:21:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://hainh.dev/wp-content/uploads/2025/10/cropped-logomyblog-32x32.png</url>
	<title>học sql - HaiNH</title>
	<link>https://hainh.dev</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">249252746</site>	<item>
		<title>Gửi các em GenZ mê Stored Procedure: &#8220;Không phải cứ nhét hết vào SP là &#8216;pro&#8217; đâu!&#8221;</title>
		<link>https://hainh.dev/gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau</link>
					<comments>https://hainh.dev/gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau/#respond</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Fri, 14 Nov 2025 07:14:16 +0000</pubDate>
				<category><![CDATA[Chuyện Nghề]]></category>
		<category><![CDATA[Cơ sở dữ liệu]]></category>
		<category><![CDATA[chuyện nghề]]></category>
		<category><![CDATA[hainh]]></category>
		<category><![CDATA[học sql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://hainh.dev/?p=7496</guid>

					<description><![CDATA[<p>Chào các đồng nghiệp tương lai, Dạo này lướt TikTok, Facebook, thỉnh thoảng tôi – một &#8220;fossil&#8221; (hóa thạch) freelancer ~U40 – lại thấy các bạn tranh luận nảy lửa. Chủ đề hot nhất có vẻ là: &#8220;Code nghiệp dư mới viết query trực tiếp, &#8216;pro&#8217; phải dùng Stored Procedure (SP) cho mọi thứ!&#8221;. Nghe...</p>
<p>The post <a href="https://hainh.dev/gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau/">Gửi các em GenZ mê Stored Procedure: “Không phải cứ nhét hết vào SP là ‘pro’ đâu!”</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Chào các đồng nghiệp tương lai,</p>



<p>Dạo này lướt TikTok, Facebook, thỉnh thoảng tôi – một &#8220;fossil&#8221; (hóa thạch) freelancer ~U40 – lại thấy các bạn tranh luận nảy lửa. Chủ đề hot nhất có vẻ là: &#8220;Code nghiệp dư mới viết query trực tiếp, &#8216;pro&#8217; phải dùng Stored Procedure (SP) cho mọi thứ!&#8221;.</p>



<p>Nghe xong, tôi chỉ biết cười trừ, lắc đầu nhớ lại thời &#8220;trẻ trâu&#8221; của mình.</p>



<p>Các bạn nói SP bảo mật hơn (chống SQL Injection), chạy nhanh hơn (vì pre-compiled), tái sử dụng tốt hơn&#8230; <strong>Vâng, về lý thuyết, sách giáo khoa viết thế, không sai!</strong></p>



<p>Nhưng các bạn à, sách giáo khoa nó khác với cái &#8220;thực tế phũ phàng&#8221; khi các bạn đi làm, nhất là freelancer, va chạm với 10 loại dự án, 5 loại khách hàng, và 3 loại &#8220;deadline dí sml&#8221; (sấp mặt luôn).</p>



<p>Là một người đã từng &#8220;thề non hẹn biển&#8221; chỉ dùng SP, đây là lý do tôi phải &#8220;quay xe&#8221; trong 80% các dự án hiện đại:</p>



<h4 class="wp-block-heading">1. Ác mộng mang tên &#8220;Version Control&#8221; (Quản lý phiên bản)</h4>



<p>Đây là cái &#8220;đau&#8221; nhất.</p>



<p>Code ứng dụng (C#, Java, Node.js) của các bạn nằm gọn gàng trên Git. Bạn <code>feature/them-chuc-nang-A</code>, bạn <code>fix/bug-B</code>. Mọi thứ rõ ràng.</p>



<p>Giờ bạn dùng SP. Cái SP đó nằm ở đâu? <strong>Nó nằm dưới Database.</strong></p>



<p>Bạn sửa 1 cái SP. Làm sao bạn đồng bộ nó với cái branch code của em? Bạn viết script <code>ALTER PROCEDURE</code> à? Bạn commit cái file <code>.sql</code> đó lên Git à?</p>



<p>Rồi 10 ông dev cùng sửa 1 cái SP? Merge kiểu gì? Khóc thét!</p>



<p>Chưa kể dự án có CI/CD (DevOps). Deploy code thì dễ, auto-deploy thay đổi database (mà cụ thể là SP) nó là cả một câu chuyện &#8220;đau thương&#8221; mà chỉ người trong cuộc mới hiểu. Trong khi dùng ORM (như Entity Framework, TypeORM), mình chỉ cần &#8220;Code-First&#8221; rồi chạy <code>migration</code>, khỏe re.</p>



<h4 class="wp-block-heading">2. &#8220;Đi tìm logic&#8221; &#8211; Cuộc phiêu lưu mệt mỏi</h4>



<p>Dự án to lên, business logic (logic nghiệp vụ) bắt đầu phức tạp. Khi dùng SP, logic của các bạn bị xé ra làm hai:</p>



<ul class="wp-block-list">
<li>Một nửa nằm trong code (Application Layer).</li>



<li>Một nửa nằm trong Database (Stored Procedures).</li>
</ul>



<p>Tưởng tượng bạn nhận bug: &#8220;Anh ơi, sao đơn hàng X không tự động giảm giá 10%?&#8221;.</p>



<p>Bạn bắt đầu hành trình: Lục code&#8230; không thấy. À, chắc nó nằm dưới SP. Bạn mở cái <code>sp_CalculateOrderTotal</code> dài 1000 dòng, viết bằng T-SQL (thứ ngôn ngữ &#8220;tù&#8221; hơn C# hay JS vạn lần), với 15 cái <code>IF...ELSE</code>, 10 cái <code>JOIN</code>, 5 cái <code>TEMP TABLE</code>.</p>



<p>Bạn debug cái SP đó kiểu gì? <code>PRINT</code> à? Chúc bạn may mắn.</p>



<p>Trong khi nếu logic nằm hết trong code, mình chỉ cần đặt &#8220;breakpoint&#8221;, F10, F11 vài phát là ra bug.</p>



<h4 class="wp-block-heading">3. &#8220;Khóa cổ&#8221; vào một Database (Vendor Lock-in)</h4>



<p>Đây là cái freelancer rất sợ. Hôm nay bạn viết 500 cái SP bằng T-SQL cho <strong>SQL Server</strong>. Mọi thứ chạy mượt. Ngày mai, khách hàng bảo: &#8220;Ngân sách eo hẹp quá, mình chuyển sang <strong>PostgreSQL</strong> (miễn phí) đi em.&#8221;</p>



<p>Toang!</p>



<p>Toàn bộ 500 cái SP đó (vốn chứa logic nghiệp vụ) gần như phải <strong>viết lại từ đầu</strong> vì cú pháp khác nhau.</p>



<p>Nếu bạn viết logic trong code (dùng ORM), bạn chỉ cần đổi cái &#8220;ConnectionString&#8221;, cài cái &#8220;Provider&#8221; (driver) cho Postgres. Xong! Code nghiệp vụ giữ nguyên 99%.</p>



<h4 class="wp-block-heading">4. Cái &#8220;Nhanh hơn&#8221; nó có thật sự &#8220;Nhanh&#8221;?</h4>



<p>Ngày xưa, 15-20 năm trước, SP nhanh hơn rõ rệt. Giờ thì sao? Các ORM hiện đại quá thông minh rồi. Chúng biết &#8220;parameterize&#8221; (chống SQL Injection y hệt SP), biết &#8220;caching query plan&#8221; (cũng gần như pre-compiled).</p>



<p>Trừ khi bạn làm báo cáo (reporting) siêu nặng, xử lý dữ liệu hàng loạt (batch processing) cả triệu record, còn với 90% các nghiệp vụ CRUD (Create, Read, Update, Delete) thông thường, sự chênh lệch hiệu năng giữa SP và một câu query tối ưu (viết bằng Dapper hay EF Core) là <strong>không đáng kể</strong>.</p>



<p>Đánh đổi sự &#8220;nhanh hơn 0.01 giây&#8221; để lấy mớ &#8220;ác mộng&#8221; ở mục 1, 2, 3? Mình xin kiếu.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f631.png" alt="😱" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Khi nào thì &#8220;ông chú&#8221; này dùng SP?</h4>



<p>Tất nhiên, SP không phải là rác. Mình vẫn dùng khi:</p>



<ol start="1" class="wp-block-list">
<li><strong>Làm báo cáo phức tạp:</strong> Những cái query <code>JOIN</code> 10 bảng, <code>GROUP BY</code> 5 tầng, <code>PIVOT</code> các kiểu. Nhét nó vào SP cho gọn code.</li>



<li><strong>Xử lý dữ liệu lớn/theo lô:</strong> Cần import/export 1 triệu dòng, chạy job đêm&#8230; SP xử lý mấy cái này &#8220;nuốt&#8221; tốt hơn code ứng dụng.</li>



<li><strong>Bảo mật cực cao:</strong> Một số nghiệp vụ yêu cầu ứng dụng không được phép <code>SELECT</code> trực tiếp vào bảng, mà chỉ được gọi qua SP (đã được phân quyền <code>EXECUTE</code>) để che giấu cấu trúc bảng. (Hiếm, nhưng có).</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h4 class="wp-block-heading"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Túm cái váy lại&#8230;</h4>



<p>Các bạn à, nghề lập trình không có &#8220;viên đạn bạc&#8221; (silver bullet) – tức là không có công nghệ nào &#8220;đúng&#8221; cho mọi trường hợp.</p>



<p>Việc các bạn cuồng tín SP (hoặc bất cứ thứ gì) và chê bai cách làm khác chỉ cho thấy các bạn &#8230; chưa &#8220;va chạm&#8221; đủ nhiều thôi.</p>



<p>Hãy dùng não, phân tích bối cảnh dự án, ưu nhược điểm của từng cái, rồi hãy chọn. Đừng vì thấy ai đó trên TikTok bảo &#8220;dùng SP mới pro&#8221; mà vội tin sái cổ.</p>



<p><strong>Tools (công cụ) là để phục vụ mình, chứ đừng làm nô lệ cho tools.</strong></p>



<p>Thân ái và chúc bớt &#8220;cuồng tín&#8221;! <em>Một Freelancer già hay lướt TikTok.</em></p><p>The post <a href="https://hainh.dev/gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau/">Gửi các em GenZ mê Stored Procedure: “Không phải cứ nhét hết vào SP là ‘pro’ đâu!”</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/gui-cac-em-genz-me-stored-procedure-khong-phai-cu-nhet-het-vao-sp-la-pro-dau/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7496</post-id>	</item>
		<item>
		<title>Sao lưu dữ liệu MS SQL EXPRESS tự động</title>
		<link>https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sao-luu-du-lieu-ms-sql-express-tu-dong</link>
					<comments>https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/#respond</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Tue, 01 Nov 2022 11:05:09 +0000</pubDate>
				<category><![CDATA[Cơ sở dữ liệu]]></category>
		<category><![CDATA[học sql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://hainh2k3.com/?p=4205</guid>

					<description><![CDATA[<p>Như chúng ta đã biết thì SQL EXPRESS không có service Agent nên việc sao lưu dữ liệu tự động sẽ không thể thực hiện được như bản dev hay enterprise. Để khắc phục vấn đề này thì mình sẽ sử dụng câu lệnh SQL Command kết hợp với Windows Task Schedule như dưới đây...</p>
<p>The post <a href="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/">Sao lưu dữ liệu MS SQL EXPRESS tự động</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Như chúng ta đã biết thì SQL EXPRESS không có service Agent nên việc sao lưu dữ liệu tự động sẽ không thể thực hiện được như bản dev hay enterprise. Để khắc phục vấn đề này thì mình sẽ sử dụng câu lệnh SQL Command kết hợp với Windows Task Schedule như dưới đây</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bat" data-enlighter-theme="rowhammer" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Sqlcmd -E -S ATM-5501256-01\SQLEXPRESS -d master -Q" BACKUP DATABASE [HTEN-EPR-CBP-HUNGPHAT] TO DISK = 'C:\BackupSQL\db_%TIME:~0,2%h.bak' WITH INIT"</pre>



<p>Tạo file .BAT có nội dung như trên và lưu vào 1 folder &#8220;C:\BackupSQL\scripts.bat&#8221;, các bạn thay đổi đường dẫn thì tùy. Nếu muốn thêm bước nén file thì sử dụng nội dung file .BAT dưới đây. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="bat" data-enlighter-theme="rowhammer" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Sqlcmd -E -S ATM-5501256-01\SQLEXPRESS -d master -Q" BACKUP DATABASE [HTEN-EPR-CBP-HUNGPHAT] TO DISK = 'C:\BackupSQL\db_%TIME:~0,2%h.bak' WITH INIT"

"C:\Program Files\WinRar\rar.exe"  a -r -ep1 "C:\BackupSQL\db_%TIME:~0,2%h.rar"  "C:\BackupSQL\db_%TIME:~0,2%h.bak"

Del "C:\BackupSQL\db_%TIME:~0,2%h.bak"</pre>



<p>Mở Windows Task Schedule để tạo 1 job như các bước dưới đây</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="646" height="487" data-attachment-id="4206" data-permalink="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/sao-luu-sql-1/" data-orig-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1.png" data-orig-size="646,487" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sao-luu-sql-1" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1-300x226.png" data-large-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1.png" src="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1.png" alt="" class="wp-image-4206" srcset="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1.png 646w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-1-1-300x226.png 300w" sizes="(max-width: 646px) 100vw, 646px" /><figcaption>Mục Triggers có thể add các khung giờ sẽ backup, mình để các mốc giờ như trên.</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="646" height="487" data-attachment-id="4207" data-permalink="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/sao-luu-sql-2/" data-orig-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1.png" data-orig-size="646,487" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sao-luu-sql-2" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1-300x226.png" data-large-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1.png" src="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1.png" alt="" class="wp-image-4207" srcset="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1.png 646w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-2-1-300x226.png 300w" sizes="(max-width: 646px) 100vw, 646px" /><figcaption>Mục Actions add file scripts.bat mà mình đã tạo bước trên.</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" width="884" height="570" data-attachment-id="4208" data-permalink="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/sao-luu-sql-3/" data-orig-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1.png" data-orig-size="884,570" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sao-luu-sql-3" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1-300x193.png" data-large-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1.png" src="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1.png" alt="" class="wp-image-4208" srcset="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1.png 884w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1-300x193.png 300w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-3-1-768x495.png 768w" sizes="(max-width: 884px) 100vw, 884px" /><figcaption>Chúng ta có thể kiểm tra như hình trên thấy data đã được sao lưu hàng ngày vào các mốc thời gian mình đã cài</figcaption></figure>
</div>


<p>Nếu muốn backup lên Cloud nữa thì ta có thể sử dụng Google drive for desktop, cài nó lên và add folder backup vào là sẽ tự syns lên cloud</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="784" height="617" data-attachment-id="4209" data-permalink="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/sao-luu-sql-4/" data-orig-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1.png" data-orig-size="784,617" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sao-luu-sql-4" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1-300x236.png" data-large-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1.png" src="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1.png" alt="" class="wp-image-4209" srcset="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1.png 784w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1-300x236.png 300w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-4-1-768x604.png 768w" sizes="(max-width: 784px) 100vw, 784px" /><figcaption>Add bolder BackupSQL để syns lên google drive</figcaption></figure>
</div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="1085" height="686" data-attachment-id="4210" data-permalink="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/sao-luu-sql-5/" data-orig-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1.png" data-orig-size="1085,686" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="sao-luu-sql-5" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1-300x190.png" data-large-file="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1-1024x647.png" src="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1.png" alt="" class="wp-image-4210" srcset="https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1.png 1085w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1-300x190.png 300w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1-1024x647.png 1024w, https://hainh.dev/wp-content/uploads/2022/11/sao-luu-sql-5-1-768x486.png 768w" sizes="(max-width: 1085px) 100vw, 1085px" /><figcaption>Kiểm tra trên google drive</figcaption></figure>
</div>


<p>Ngoài ra nếu syns lên google drive này ta có thể lưu được 100 phiên bản &#8211; tức là ta có thể lưu backup sql được trong phạm vi 100 ngày, quá thoải mái để kiểm tra restore khi có sự cố phải không.</p><p>The post <a href="https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/">Sao lưu dữ liệu MS SQL EXPRESS tự động</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/sao-luu-du-lieu-ms-sql-express-tu-dong/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4205</post-id>	</item>
		<item>
		<title>Tìm hiểu về WITH (NOLOCK) trong MS SQL Server</title>
		<link>https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tim-hieu-ve-with-nolock-trong-ms-sql-server</link>
					<comments>https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Mon, 03 Dec 2018 04:28:01 +0000</pubDate>
				<category><![CDATA[Cơ sở dữ liệu]]></category>
		<category><![CDATA[học sql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://hainh2k3.com/?p=1192</guid>

					<description><![CDATA[<p>Nếu search trên google các bài hướng dẫn về việc tăng tốc độ truy vấn sql thì chúng ta sẽ thấy rất nhiều bài nhắc đến việc sử dụng WITH (NOLOCK) trong các câu truy vấn. Trước kia mình cũng sử dụng nó trong tất cả các câu truy vấn của mình thì phát hiện...</p>
<p>The post <a href="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/">Tìm hiểu về WITH (NOLOCK) trong MS SQL Server</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Nếu search trên google các bài hướng dẫn về việc tăng tốc độ truy vấn sql thì chúng ta sẽ thấy rất nhiều bài nhắc đến việc sử dụng WITH (NOLOCK) trong các câu truy vấn. Trước kia mình cũng sử dụng nó trong tất cả các câu truy vấn của mình thì phát hiện ra là tốc độ truy vấn của mình được cải thiện lên rất nhiều khi database được truy cập liên tục tục và nhiều client sử dụng, tuy nhiên cũng có trường hợp nó làm chậm câu truy vấn của mình đi rõ rệt. Vì thế chúng ta thử tìm hiểu xem nó hoạt động như thế nào nhé.</p>



<h3 class="wp-block-heading"><span style="color: #008000;">Sử dụng WITH (NOLOCK) có chức năng gì ?</span></h3>



<p>Cho phép SQL đọc dữ liệu từ các bảng kể cả khi đang bị lock mà không bị chặn bởi các tiến trình khác. Việc này giúp cải thiện hiệu suất truy vấn, tuy nhiên có khả năng dữ liệu đọc được bị sai lệch, không chuẩn.</p>



<p><span style="color: #993366;"><strong>Hãy thử xem ví dụ dưới đây để hiểu rõ hơn nhé:</strong></span></p>



<p>Đây là một truy vấn trả về tất cả dữ liệu từ bảng Person.Contact (database AdventureWorks). Khi nhìn vào kết quả trả về chúng ta thấy duy nhất có 1 bản ghi có cột Suffix là có giá trị ( khác null), đó là bản ghi có ContactID = 12.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT * FROM Person.Contact WHERE ContactID &lt; 20</pre>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="729" height="385" data-attachment-id="1193" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-1/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-1.gif" data-orig-size="729,385" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 1" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-1-300x158.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-1.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-1.gif" alt="" class="wp-image-1193"/></figure>
</div>


<p>Bây giờ hãy thử chạy câu truy vấn dưới đây trên một cửa sổ sql studio management khác với transaction xem sao nhé, tuy nhiên chúng ta sẽ không comit tới database để những bản ghi đó bị khóa lại xem sao:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 1
BEGIN TRAN
UPDATE Person.Contact SET Suffix = 'B' WHERE ContactID &lt; 20
-- ROLLBACK or COMMIT</pre>



<p>Nếu chúng ta chạy lại câu lệnh select bên trên kia thì sẽ thấy câu truy vấn này sẽ cứ treo mãi mà không trả về kết quả nào.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT * FROM Person.Contact WHERE ContactID &lt; 20</pre>



<p>Nếu chạy lệnh&nbsp;<strong><span style="color: #800080;">sp_who2</span>&nbsp;&nbsp;</strong>chúng ta có thể biết được là lệnh SELECT đó đã bị block lại.&nbsp;Nguyên nhân là do lệnh UPDATE trên kia chưa được COMIT hoặc ROLLBACK, các bản ghi đó sẽ bị khóa lại, câu truy vấn SELECT kia sẽ bị treo mãi. Khi đó chỉ có cách hủy câu select đó đi hoặc là COMIT, ROLLBACK để lệnh UPDATE trên kia hoàn thành.</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="436" height="60" data-attachment-id="1194" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-2/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-2.gif" data-orig-size="436,60" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 2" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-2-300x41.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-2.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-2.gif" alt="" class="wp-image-1194"/></figure>
</div>


<p>Để truy xuất những bản ghi đã bị khóa đó, Chúng ta có thể sử dụng chỉ thị NOLOCK như dưới đây. Câu lệnh SELECT vẫn sẽ trả về kết quả thậm chí lệnh UPDATE trên kia chưa được COMIT hay ROLLBACK và câu lệnh SELECT tại window 1 vẫn đang loading &#8230;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 2
SELECT * FROM Person.Contact WITH (NOLOCK) WHERE ContactID &lt; 20</pre>



<p>Chúng ta có thể thấy cột Suffix bây giờ tất cả giá trị đều là &#8220;B&#8221;. Nguyên nhân là do câu lệnh UPDATE tại windows 1 đã thực hiện việc update các bản ghi đó. Mặc dù transaction vẫn chưa được commit nhưng nếu sử dụng chỉ thị NOLOCK thì SQL Server sẽ bỏ qua việc lock và trả về dữ liệu. Nếu trong trường hợp lệnh UPDATE&nbsp; trên kia được rollback lại thì dữ liệu trả về bị sai lệch, không chuẩn là ở chỗ đó.</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="727" height="385" data-attachment-id="1195" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-3/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-3.gif" data-orig-size="727,385" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 3" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-3-300x159.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-3.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-3.gif" alt="" class="wp-image-1195"/></figure>
</div>


<p>Nếu chúng ta rollback lại lệnh UPDATE trên kia bằng lệnh ROLLBACK thì câu lệnh SELECT ở window 1 lúc này cũng sẽ trả về kết quả ta có thể thấy cột Suffix giống như ban đầu.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 1
ROLLBACK</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 2
SELECT * FROM Person.Contact WITH (NOLOCK) WHERE ContactID &lt; 20 
-- or
SELECT * FROM Person.Contact WHERE ContactID &lt; 20</pre>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="730" height="382" data-attachment-id="1196" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-4/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-4.gif" data-orig-size="730,382" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 4" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-4-300x157.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-4.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-4.gif" alt="" class="wp-image-1196"/></figure>
</div>


<p>Vì vậy với việc sử dụng chỉ thị NOLOCK thì dữ liệu đọc được là đã được thay đổi, tuy nhiên dữ liệu đó chưa thực sự được xác nhận bởi CSDL.&nbsp; Nếu các bạn đang chạy báo cáo mà không quan tâm đến việc dữ liệu có thể bị ngắt hay không sử dụng transaction thì không vấn đề, nhưng nếu việc tạo transaction và yêu cầu dữ liệu có tính nhất quán thì việc sử dụng chỉ thị NOLOCK có thể khiến dữ liệu báo cáo bị sai hoặc ngắt.</p>



<h3 class="wp-block-heading"><span style="color: #008000;">Các kiểu Lock của SQL Server khi suwe dụng với NOLOCK</span></h3>



<p>Nếu chúng ta chạy truy vấn SELECT trên kia mà không có NOLOCK và sử dụng&nbsp;<strong><span style="color: #800080;">sp_who2</span></strong>&nbsp;có thể thấy kết quả như sau</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="564" height="98" data-attachment-id="1197" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-5/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-5.gif" data-orig-size="564,98" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 5" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-5-300x52.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-5.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-5.gif" alt="" class="wp-image-1197"/></figure>
</div>


<p>Còn vẫn câu truy vấn đó nhưng với NOLOCK và&nbsp;sử dụng&nbsp;<strong><span style="color: #800080;">sp_who2</span></strong>&nbsp;có thể thấy kết quả như sau</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="567" height="78" data-attachment-id="1198" data-permalink="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/with-nolock-6/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-6.gif" data-orig-size="567,78" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="with nolock 6" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-6-300x41.gif" data-large-file="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-6.gif" src="https://hainh.dev/wp-content/uploads/2018/12/with-nolock-6.gif" alt="" class="wp-image-1198"/></figure>
</div>


<p>Điểm khác nhau ở đây là một khóa &#8220;S&#8221; được đặt trên page (PAG) khi không sử dụng NOLOCK. Tương tự chúng ta thấy khó Sch-S tương tứng với IS cho table (TAB). Vì vậy, các vấn đề chúng ta thấy được là ngay cả khi chúng ta chỉ SELECT dữ liệu thôi nhưng SQL&nbsp; Server vẫn tạo ra Lock để đảm bảo dữ liệu được nhất quán.</p>



<p>Đây là các kiểu Lock và chế độ Lọc được sử dụng cho cả 2 câu truy vấn select bên trên:</p>



<p><span style="color: #800080;"><strong>Lock Types:</strong></span></p>



<ul class="wp-block-list">
<li>MD &#8211; metadata lock</li>



<li>DB &#8211; database lock</li>



<li>TAB &#8211; table lock</li>



<li>PAG &#8211; page lock</li>
</ul>



<p><span style="color: #800080;"><strong>Lock Mode:</strong></span></p>



<ul class="wp-block-list">
<li>S &#8211; Cho phép truy cập</li>



<li>Sch-S &#8211; Schema được ổn định đảm bảo không thay đổi khi đối tượng đang được sử dụng</li>



<li>IS &#8211; Intent được chia sẻ và chỉ định sử dụng Lock S</li>
</ul>



<h3 class="wp-block-heading"><span style="color: #008000;">READUNCOMMITED và NOLOCK</span></h3>



<p>Chỉ thị NOLOCK tương tự với chỉ thị READUNCOMMITED và cho kết quả giống nhau</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT * FROM Person.Contact WITH (READUNCOMMITTED)</pre>



<h3 class="wp-block-heading"><span style="color: #008000;">With NOLOCK chỉ sử dụng cho lệnh SELECT</span></h3>



<p>Chỉ thị NOLOCK và READUNCOMMITED chỉ được sử dụng duy nhất với lệnh SELECT. Nếu chúng ta sử dụng chúng cho UPDATE, DELETE, INSERT sẽ gặp lỗi.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">UPDATE Person.Contact with (NOLOCK) SET Suffix = 'B' WHERE ContactID &lt; 20</pre>


<p>[su_highlight background=&#8221;#FFFFE1&#8243; color=&#8221;#666666&#8243;]Msg 1065, Level 15, State 1, Line 15 The NOLOCK and READUNCOMMITTED lock hints are not allowed for target tables of INSERT, UPDATE, DELETE or MERGE statements.[/su_highlight]</p>



<h3 class="wp-block-heading"><span style="color: #008000;">Schema thay đổi việc Block với NOLOCK</span></h3>



<p>NOLOCK cần một khóa Sch-S ( ổn định schema ), SELECT sử dụng NOLOCK vẫn có thể bị chặn nếu một bảng bị thay đổi và không được comit. Đây là một ví dụ:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 1
BEGIN TRAN
ALTER TABLE Person.Contact ADD column_b 
VARCHAR(20) NULL ;</pre>



<p>Nếu chúng ta chạy câu lệnh SELECT nó vẫn sẽ bị chặn trừ khi chúng ta comit hoặc rollback.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- run in query window 2
SELECT * FROM Person.Contact WITH (NOLOCK) WHERE ContactID &lt; 20</pre>



<h3 class="wp-block-heading"><span style="color: #008000;">Các vấn đề với NOLOCK</span></h3>



<p>Chúng ta đã nhắc đến vấn đề dữ liệu có thể không nhất quán nếu sử dụng NOLOCK, dưới đây là một vài vấn đề chúng ta có thể gặp nếu sử dụng nó</p>



<ul class="wp-block-list">
<li>Dirty Reads &#8211; Đọc bẩn,&nbsp;điều này xảy ra khi cập nhật được thực hiện, dữ liệu select ra có thể khác nhau.</li>



<li>Nonrepeatable Reads &#8211; Đọc không lặp lại, việc này xảy ra khi chúng ta cần đọc dữ liệu nhiều lần và dữ liệu thay đổi trong quá trình đó.</li>



<li>Phantom Reads &#8211; Đọc ma, việc này xảy ra khi dữ liệu được insert hoặc delete và transaction đã rollback lại. Vì vậy dữ liệu insert mà chúng ta nhận được có thể nhiều hơn thực tế, tương tự số bản ghi bị xóa sẽ ít hơn nếu như rollback được thực hiện.</li>
</ul>



<p>Các vấn đề khác và chi tiết chúng ta có thể tham khảo tại link :&nbsp;<a href="http://msdn.microsoft.com/en-us/library/ms190805.aspx" target="_blank" rel="noopener">http://msdn.microsoft.com/en-us/library/ms190805.aspx</a></p>



<h3 class="wp-block-heading"><span style="color: #008000;">Isolation Level</span></h3>



<p>Chúng ta cũng có thể thiết lập&nbsp;Isolation Level cho tất cả truy vấn sử dụng NOLOCK hoặc&nbsp;READUNCOMMITTED . Isolation level sẽ chấp nhận&nbsp;READUNCOMMITTED cho tất cả lệnh SELECT cho đến khi chúng ta tắt nó đi.</p>



<p>Trong ví dụ dưới đây, hai câu lệnh SELECT sẽ sử dụng READUNCOMMITED hoặc NOLOCK và UPDATE sẽ vẫn hoạt động như bình thường. Bằng cách này, bạn có thể đặt một loạt các câu lệnh thay vì sửa đổi mỗi truy vấn.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; -- turn it on

SELECT * FROM Person.Contact WHERE ContactID &lt; 20 

UPDATE Person.Contact SET Suffix = 'B' WHERE ContactID = 1 

SELECT * FROM Person.Contact WHERE ContactID &lt; 20 

SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -- turn it off</pre><p>The post <a href="https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/">Tìm hiểu về WITH (NOLOCK) trong MS SQL Server</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/tim-hieu-ve-with-nolock-trong-ms-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1192</post-id>	</item>
		<item>
		<title>Phân biệt và cách sử dụng các loại JOIN trong MSSQL Server</title>
		<link>https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server</link>
					<comments>https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/#comments</comments>
		
		<dc:creator><![CDATA[NGUYỄN HOÀNG HẢI]]></dc:creator>
		<pubDate>Sat, 10 Nov 2018 13:30:56 +0000</pubDate>
				<category><![CDATA[Cơ sở dữ liệu]]></category>
		<category><![CDATA[học sql]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">https://hainh2k3.com/?p=1075</guid>

					<description><![CDATA[<p>Khi bạn cần truy vấn các cột dữ liệu từ nhiều bảng khác nhau để trả về trong cùng một tập kết quả thì JOIN là phép kết nối dữ liệu từ nhiều bảng lại với nhau ( bao gồm cả các&#160;view, sub query). VD: Chúng ta có 2 bảng HOC_VIEN (A) và DIEM_THI (B)...</p>
<p>The post <a href="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/">Phân biệt và cách sử dụng các loại JOIN trong MSSQL Server</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Khi bạn cần truy vấn các cột dữ liệu từ nhiều bảng khác nhau để trả về trong cùng một tập kết quả thì <strong>JOIN</strong> là phép kết nối dữ liệu từ nhiều bảng lại với nhau ( bao gồm cả các&nbsp;view, sub query).</p>



<p>VD: Chúng ta có 2 bảng <strong><span style="color: #008000;">HOC_VIEN</span> <span style="color: #ff0000;">(A)</span></strong> và <strong><span style="color: #008000;">DIEM_THI</span> <span style="color: #ff0000;">(B)</span></strong> như dưới đây:</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="469" height="285" data-attachment-id="1080" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql.jpg" data-orig-size="469,285" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-300x182.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql.jpg" alt="" class="wp-image-1080" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql.jpg 469w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-300x182.jpg 300w" sizes="(max-width: 469px) 100vw, 469px" /></figure>
</div>


<p>Với 2 bảng dữ liệu trên có liên kết thông qua cột MaHV, vậy làm thế nào chúng ta lấy ra được danh sách điểm thi có các cột môn học, họ tên học viên, mã lớp và điểm số cùng lúc? Khi đó chúng ta cần liên kết hay là JOIN các bảng dữ liệu lại với nhau và chỉ định ra các trường dữ liệu cần lấy.</p>



<p>SQL Server cung cấp các kiểu JOIN là INNER JOIN, OUTER JOIN, và CROSS JOIN.</p>



<h4 class="wp-block-heading"><span style="color: #ff0000;">1. INNER JOIN (viết tắt: JOIN)</span></h4>



<p>Kết quả từ Inner join trong SQL là tập hợp dữ liệu trong đó giá trị dùng để join hai table với nhau đều có ở cả hai bảng, nghĩa là kết quả là GIAO của hai tập hợp dữ liệu.Các bản ghi chỉ xuất hiện ở một trong hai bảng sẽ bị loại.</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="316" height="184" data-attachment-id="1083" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/inner-join/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/inner-join.png" data-orig-size="316,184" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="inner join" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/inner-join-300x175.png" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/inner-join.png" src="https://hainh.dev/wp-content/uploads/2018/11/inner-join.png" alt="" class="wp-image-1083" srcset="https://hainh.dev/wp-content/uploads/2018/11/inner-join.png 316w, https://hainh.dev/wp-content/uploads/2018/11/inner-join-300x175.png 300w" sizes="(max-width: 316px) 100vw, 316px" /></figure>
</div>


<p>Câu truy vấn như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT A.MonHoc,A.MaHV,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A INNER JOIN HOC_VIEN B ON A.MaHV = B.MaHV
-- Hoặc có thể viết tắt --
SELECT A.MonHoc,A.MaHV,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A JOIN HOC_VIEN B ON A.MaHV = B.MaHV</pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="375" height="163" data-attachment-id="1082" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql-2/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2.jpg" data-orig-size="375,163" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql 2" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2-300x130.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2.jpg" alt="" class="wp-image-1082" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2.jpg 375w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-2-300x130.jpg 300w" sizes="(max-width: 375px) 100vw, 375px" /></figure>



<p>Kết quả như chúng ta thấy với 6 bản ghi trên thì các giá trị MaHV đều có trong cả 2 bảng HOC_VIEN và DIEM_THI</p>



<h4 class="wp-block-heading"><span style="color: #ff0000;">2. OUTER JOIN trong SQL Server</span></h4>



<p>Lấy về các bản ghi có mặt trong cả hai bảng và cả các bản ghi chỉ xuất hiện ở một trong hai bảng. Kiểu JOIN này được chia làm mấy loại sau:</p>



<p><span style="color: #008000;"><strong>2.1. LEFT OUTER JOIN (viết tắt: LEFT JOIN)</strong></span></p>



<p>Lấy toàn bộ các dòng dữ liệu ở table bên trái và với những dòng dữ liệu tương ứng ở bảng bên phải CÓ giá trị MaHV ở bảng bên trái, nếu không có dữ liệu tương ứng bảng bên phải sẽ trả về kết quả null</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="362" height="165" data-attachment-id="1084" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/left-join/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/left-join.png" data-orig-size="362,165" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="left join" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/left-join-300x137.png" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/left-join.png" src="https://hainh.dev/wp-content/uploads/2018/11/left-join.png" alt="" class="wp-image-1084" srcset="https://hainh.dev/wp-content/uploads/2018/11/left-join.png 362w, https://hainh.dev/wp-content/uploads/2018/11/left-join-300x137.png 300w" sizes="(max-width: 362px) 100vw, 362px" /></figure>
</div>


<p>Câu truy vấn như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT A.MonHoc,A.MaHV,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A LEFT OUTER JOIN HOC_VIEN B ON A.MaHV = B.MaHV
-- Hoặc có thể viết tắt --
SELECT A.MonHoc,A.MaHV,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A LEFT JOIN HOC_VIEN B ON A.MaHV = B.MaHV</pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="383" height="282" data-attachment-id="1085" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql-3/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3.jpg" data-orig-size="383,282" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql 3" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3-300x221.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3.jpg" alt="" class="wp-image-1085" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3.jpg 383w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-3-300x221.jpg 300w" sizes="(max-width: 383px) 100vw, 383px" /></figure>



<p></p>



<p>Kết quả như chúng ta thấy, tất cả các dòng ở bảng DIEM_THI đã được liệt kê ra, một số dòng có cột HoTen và MaLop là null là do nguyên nhân MaHV có ở bảng DIEM_THI nhưng không có ở bảng HOC_VIEN</p>



<p><span style="color: #008000;"><strong>2.2. RIGHT OUTER JOIN (viết tắt: RIGHT JOIN)</strong></span></p>



<p>Ngược lại so với LEFT JOIN, lấy toàn bộ các dòng dữ liệu ở table bên phải và với những dòng dữ liệu tương ứng ở bảng bên trái CÓ giá trị MaHV ở bảng bên phải, nếu không có dữ liệu tương ứng bảng bên trái sẽ trả về kết quả null</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="349" height="167" data-attachment-id="1086" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/right-join/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/right-join.png" data-orig-size="349,167" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="right join" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/right-join-300x144.png" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/right-join.png" src="https://hainh.dev/wp-content/uploads/2018/11/right-join.png" alt="" class="wp-image-1086" srcset="https://hainh.dev/wp-content/uploads/2018/11/right-join.png 349w, https://hainh.dev/wp-content/uploads/2018/11/right-join-300x144.png 300w" sizes="(max-width: 349px) 100vw, 349px" /></figure>
</div>


<p>Câu truy vấn như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT A.MonHoc,A.MaHV,B.MaHV MaHV2,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A RIGHT OUTER JOIN HOC_VIEN B ON A.MaHV = B.MaHV
-- Hoặc có thể viết tắt --
SELECT A.MonHoc,A.MaHV,B.MaHV MaHV2,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A RIGHT JOIN HOC_VIEN B ON A.MaHV = B.MaHV</pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="439" height="315" data-attachment-id="1087" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql-4/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4.jpg" data-orig-size="439,315" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql 4" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4-300x215.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4.jpg" alt="" class="wp-image-1087" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4.jpg 439w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-4-300x215.jpg 300w" sizes="(max-width: 439px) 100vw, 439px" /></figure>



<p></p>



<p>Kết quả như chúng ta thấy tất cả các dòng của bảng HOC_VIEN đã được liệt kê ra, mình thêm cột B.MaHV as MaHV2 ra để các bạn thấy danh sách của nó đầy đủ trong khi cột MaHV, MonHoc,DiemSo là null vì cột MaHV trong bảng DIEM_THI không có trong bảng HOC_VIEN. Các bạn có thể so sánh với bảng kết quả bên trên kia khi select left join nhé.</p>



<p><span style="color: #008000;"><strong>2.3. FULL OUTER JOIN (viết tắt: FULL JOIN)</strong></span></p>



<p>Kết quả gồm tất cả các bản ghi của cả hai bảng. Với các bản ghi chỉ xuất hiện trong một bảng thì các cột dữ liệu từ bảng kia được điền giá trị NULL.</p>


<div class="wp-block-image">
<figure class="aligncenter"><img loading="lazy" decoding="async" width="339" height="161" data-attachment-id="1088" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/full-join/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/full-join.png" data-orig-size="339,161" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="full join" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/full-join-300x142.png" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/full-join.png" src="https://hainh.dev/wp-content/uploads/2018/11/full-join.png" alt="" class="wp-image-1088" srcset="https://hainh.dev/wp-content/uploads/2018/11/full-join.png 339w, https://hainh.dev/wp-content/uploads/2018/11/full-join-300x142.png 300w" sizes="(max-width: 339px) 100vw, 339px" /></figure>
</div>


<p>Câu truy vấn như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT A.MonHoc,A.MaHV,B.MaHV MaHV2,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A FULL OUTER JOIN HOC_VIEN B ON A.MaHV = B.MaHV
-- Hoặc có thể viết tắt --
SELECT A.MonHoc,A.MaHV,B.MaHV MaHV2,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A FULL JOIN HOC_VIEN B ON A.MaHV = B.MaHV</pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="440" height="431" data-attachment-id="1089" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql-5/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5.jpg" data-orig-size="440,431" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql 5" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5-300x294.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5.jpg" alt="" class="wp-image-1089" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5.jpg 440w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-5-300x294.jpg 300w" sizes="(max-width: 440px) 100vw, 440px" /></figure>



<p></p>



<p>Nhìn kết quả kia thì chúng ta có thể thấy kết quả là bao gồm là những dòng kết quả của LEFT JOIN và RIGHT JOIN kết hợp với nhau, không bao gồm các dòng trùng lặp. Trên kia, những dòng HoTen là null là do MaHV ở bảng DIEM_THI nhưng không có ở bảng HOC_VIEN và những dòng MonHoc,DiemSo là null nguyên nhân do MaHV có ở bảng HOC_VIEN mà không có ở bảng DIEM_THI.</p>



<h4 class="wp-block-heading"><span style="color: #ff0000;">3. CROSS JOIN</span></h4>



<p>CROSS JOIN trong SQL ít được sử dụng hơn so với các loại JOIN ở trên do tính ít ý nghĩa của nó, kết quả trả về của CROSS JOIN là lấy số dòng của bảng bên trái x số dòng của bảng bên phải. Ví dụ bảng DIEM_THI có 12 dòng, bảng HOC_VIEN có 12 dòng, kết quả trả về là 12&#215;12 = 144 dòng.<br>Dữ liệu được nhân lên do cứ tương ứng với 1 dòng trong DIEM_THI thì sẽ nối với toàn bộ 12 dòng trong HOC_VIEN mà không cần quan tâm đến tiêu chí so sánh nào (không có mệnh đề ON t1.ColumnA = t2.ColumnB như các kiểu JOIN khác).</p>



<p>Câu lệnh truy vấn như sau:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="sql" data-enlighter-theme="classic" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT A.MonHoc,A.MaHV,B.HoTen,B.MaLop,A.DiemSo
FROM DIEM_THI A CROSS JOIN HOC_VIEN B</pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="596" height="707" data-attachment-id="1090" data-permalink="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/lenh-join-sql-6/" data-orig-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6.jpg" data-orig-size="596,707" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="lenh join sql 6" data-image-description="" data-image-caption="" data-medium-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6-253x300.jpg" data-large-file="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6.jpg" src="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6.jpg" alt="" class="wp-image-1090" srcset="https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6.jpg 596w, https://hainh.dev/wp-content/uploads/2018/11/lenh-join-sql-6-253x300.jpg 253w" sizes="(max-width: 596px) 100vw, 596px" /></figure>



<h4 class="wp-block-heading"><span style="color: #008080;">KẾT BÀI</span></h4>



<p>Chúng ta đã tìm hiểu về các loại JOIN trong SQL Server, chúng được sử dụng thường xuyên trong truy vấn dữ liệu và nhiều xử lý khác. Cho nên bạn cần thực hành cẩn thận và nắm thật rõ cách sử dụng các loại Join để khi làm việc với SQL Server được nhuần nhuyễn và hiệu quả hơn.</p><p>The post <a href="https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/">Phân biệt và cách sử dụng các loại JOIN trong MSSQL Server</a> first appeared on <a href="https://hainh.dev">HaiNH</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://hainh.dev/phan-biet-va-cach-su-dung-cac-loai-join-trong-mssql-server/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1075</post-id>	</item>
	</channel>
</rss>
