<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Khai báo biến trong SQL Server</title>
<meta name="description" content="Khai báo biến trong SQL Server - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;khai-bao-bien-trong-sql-server-9972.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Khai báo biến trong SQL Server">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;khai-bao-bien-trong-sql-server-9972.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/khai-bao-bien-trong-sql-server-9972.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/khai-bao-bien-trong-sql-server-9972.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Khai báo biến trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ tư - 12/03/2025 00:23</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Khai báo biến trong SQL Server" src="https://st.quantrimang.com/photos/image/2018/03/31/ms-sql-server-primary-key-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p><strong>Declare trong SQL Server</strong>&nbsp;là gì?&nbsp;<strong>Cách dùng Declare SQL Server</strong>&nbsp;như thế nào? Hãy cùng Quantrimang.com tìm hiểu&nbsp;<strong>cách khai báo biến trong SQL Server&nbsp;</strong>như thế nào nhé!</p>

<p>Không thể phủ nhận lập trình ngày càng có ý nghĩa quan trọng đối với cuộc sống của chúng ta. Nó là “cái nôi” sản sinh ra rất nhiều sản phẩm, phần mềm hữu ích cho cuộc sống, cả về vật chất lẫn tinh thần. Chính vì thế, lập trình đang ngày càng trở thành một ngành nghề “hot” với mức lương cao cho lập trình viên tài năng.</p>

<p>Thế giới lập trình vô cùng đa dạng và phong phú với nhiều ngôn ngữ có ưu và nhược điểm riêng. Song song với lập trình, bạn cần phải có một hệ quản trị cơ sở dữ liệu quan hệ vững chắc và SQL Server sẽ giúp bạn đạt được điều đó.</p>

<p>SQL Server là một trong số nhiều hệ quản trị cơ sở dữ liệu quan hệ phổ biến nhất hiện nay. Đây là một sản phẩm của Microsoft. Nó có đầy đủ phiên bản dành cho mọi nhu cầu phát triển của cá nhân và doanh nghiệp.</p>

<p>Nghe có vẻ hơi phức tạp nhưng thực tế học SQL Server không khó. Bạn có thể bắt đầu từ những kiến thức cơ bản nhất đã được tổng hợp trên Quantrimang.com. Ở bài viết này, chúng ta hãy cùng nhau tìm hiểu cách gán giá trị trong SQL hay declare trong SQL là gì nhé!</p>

<p><strong>Biến (Variable)</strong>&nbsp;dùng để lưu trữ các giá trị tạm thời trong quá trình thực thi&nbsp;thuật toán.</p>

<h2 id="mcetoc_1crm7ekai0">Cú pháp khai báo biến trong SQL Server</h2>

<p>Để khai báo biến trong SQL Server, ta sử dụng câu lệnh DECLARE, được biểu diễn như sau:</p>

<pre id="pre0">
DECLARE @variable_name datatype &#91; = initial_value &#93;, @variable_name datatype &#91; = initial_value &#93;, ...;</pre>

<p><strong>Tham số:</strong></p>

<ul>
	<li><em>variable_name</em>: tên gán cho biến.</li>
	<li><em>datatype:</em>&nbsp;kiểu dữ liệu của biến.</li>
	<li><em>initial_value:</em>&nbsp;giá trị mặc định gán cho biến (không bắt buộc).</li>
</ul>

<h2 id="mcetoc_1crm7ekai1">Khai báo một biến trong SQL Server</h2>

<p>Dùng DECLARE khai báo một biến bất kì</p>

<pre id="pre1">
DECLARE @quantrimang VARCHAR(50);</pre>

<p>Câu lệnh DECLARE này khai báo một biến có tên&nbsp;<em>@quantrimang,</em>&nbsp;với kiểu dữ liệu VARCHAR và độ dài 50 kí tự.</p>

<p>Sau đó bạn thay đổi giá trị biến&nbsp;<em>@quantrimang</em>&nbsp;bằng cách sử dụng câu lệnh SET.</p>

<pre id="pre2">
SET @quantrimang = &#039;Hello world&#039;;</pre>

<p>Tiếp theo hãy thử với kiểu dữ liệu INT:</p>

<pre id="pre3">
DECLARE @site_value INT;</pre>

<p>Sử dụng câu lệnh SET để gán giá trị cho biến&nbsp;<em>@site_value</em></p>

<pre id="pre4">
SET @site_value = 10;</pre>

<p>Vậy biến&nbsp;<em>@site_value</em>&nbsp;ở đây được gán cho số nguyên 10.</p>

<h2 id="mcetoc_1crm7ekai2">Khai báo nhiều biến trong SQL Server</h2>

<p>Cách sử dụng lệnh như sau:</p>

<pre id="pre5">
DECLARE @quantrimang VARCHAR(50),        @site_value INT;</pre>

<p>Trong ví dụ này, chúng ta có hai biến được khai báo: biến&nbsp;<em>@quantrimang</em>&nbsp;với kiểu dữ liệu VARCHAR(50) và biến&nbsp;<em>@site_value</em>&nbsp;kiểu dữ liệu INT.</p>

<h2 id="mcetoc_1crm7ekai3">Khai báo biến với giá trị mặc định trong SQL Server</h2>

<p>Trong SQL Server, ta có thể gán giá trị mặc định cho biến ngay lúc khai báo.</p>

<pre id="pre6">
DECLARE @quantrimang VARCHAR(50) = &#039;Hello world&#039;;</pre>

<p>Vậy ở đây biến&nbsp;<em>@quantrimang</em>&nbsp;với kiểu dữ liệu VARCHAR và độ dài 50 kí tự được gán mặc định cho giá trị &#039;Hello world&#039;.</p>

<p>Tương tự ta khai báo với kiểu dữ liệu INT:</p>

<pre id="pre7">
DECLARE @site_value INT = 10;</pre>

<h2 id="mcetoc_1crm7ekai4">Khai báo nhiều hơn một biến có gán giá trị ban đầu</h2>

<p>Cách sử dụng lệnh như sau:</p>

<pre id="pre8">
DECLARE @quantrimang VARCHAR(50) = &#039;Hello world&#039;;@site_value INT = 10;</pre>

<p>Hai biến&nbsp;<em>@quantrimang</em>&nbsp;và&nbsp;<em>@site_value</em>&nbsp;đã được khai báo ở chung một lệnh và được gán giá trị ban đầu mặc định.</p>

<h2>Khai báo một biến của bảng type</h2>

<p>Ví dụ sau tạo một biến table, chứa giá trị được xác định trong mệnh đề OUTPUT của lệnh UPDATE. Hai câu lệnh SELECT theo sau trả về các giá trị trong @MyTableVar và kết quả của thao tác cập nhật trong bảng Employee. Kết quả trong cột INSERTED.ModifiedDate khác với các giá trị trong cột ModifiedDate của bảng Employee. Điều này là do trình kích hoạt AFTER UPDATE, cập nhật giá trị của ngày sửa đổi thành ngày hiện tại, được xác định trên bảng Employee. Tuy nhiên, các cột được trả về từ OUTPUT phản ánh dữ liệu trước khi được kích hoạt.</p>

<p>Ví dụ:</p>

<pre id="pre9">
USE AdventureWorks2012;
GO
DECLARE @MyTableVar TABLE (
    EmpID INT NOT NULL,
    OldVacationHours INT,
    NewVacationHours INT,
    ModifiedDate DATETIME);
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25
OUTPUT INSERTED.BusinessEntityID,
       DELETED.VacationHours,
       INSERTED.VacationHours,
       INSERTED.ModifiedDate
INTO @MyTableVar;
--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
--Note that ModifiedDate reflects the value generated by an
--AFTER UPDATE trigger.
SELECT TOP (10) BusinessEntityID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO</pre>

<h2>Khai báo một biến của bảng type với các chỉ mục nội tuyến</h2>

<p>Ví dụ sau tạo một biến table với một chỉ mục nội tuyến được nhóm lại với nhau và hai chỉ mục inline không nhóm:</p>

<pre id="pre10">
DECLARE @MyTableVar TABLE (
    EmpID INT NOT NULL,
    PRIMARY KEY CLUSTERED (EmpID),
    UNIQUE NONCLUSTERED (EmpID),
    INDEX CustomNonClusteredIndex NONCLUSTERED (EmpID)
);
GO</pre>

<p>Truy vấn sau trả thông tin về các chỉ mục được tạo ở truy vấn trước.</p>

<pre id="pre11">
SELECT *
FROM tempdb.sys.indexes
WHERE object_id &lt; 0;
GO</pre>

<h2>Khai báo một biến của kiểu bảng do người dùng định nghĩa</h2>

<p>Ví dụ sau tạo một tham số có giá trị bảng hoặc biến bảng tên @LocationTVP. Điều này yêu cầu một kiểu bảng do người dùng định nghĩa tên LocationTableType.</p>

<pre id="pre12">
DECLARE @LocationTVP
AS LocationTableType;</pre>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/khai-bao-bien-trong-sql-server-9972.html" title="Khai báo biến trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/khai-bao-bien-trong-sql-server-9972.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>