<?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>tables &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/tag/tables/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Tue, 28 Dec 2021 21:09:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>tables &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>DBCC CHECKIDENT RESEED behaves differently when TRUNCATE or DELETE was used on the table</title>
		<link>https://sqlpowered.com/dbcc-checkident-reseed-behaves-differently-when-truncate-or-delete-was-used-on-the-table/</link>
					<comments>https://sqlpowered.com/dbcc-checkident-reseed-behaves-differently-when-truncate-or-delete-was-used-on-the-table/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Tue, 28 Dec 2021 20:41:41 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DML]]></category>
		<category><![CDATA[tables]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=5202</guid>

					<description><![CDATA[Let&#8217;s assume we have some script where tables are filled with data and then DELETED/TRUNCATED again to rerun the script. Some of these tables are large and they don&#8217;t have a foreign key reference so we will decide to use TRUNCATE to remove the data. For tables referenced by foreign...]]></description>
										<content:encoded><![CDATA[<p>Let&#8217;s assume we have some script where tables are filled with data and then DELETED/TRUNCATED again to rerun the script. Some of these tables are large and they don&#8217;t have a foreign key reference so we will decide to use TRUNCATE to remove the data. For tables referenced by foreign keys, we will use DELETE instead (because TRUNCATE can&#8217;t be executed on tables referenced in foreign keys). All the tables are using IDENTITY(1,1) property and we will use DBCC CHECKIDENT to RESEED identity values to be starting again from 1. But what is our surprise if we will discover later, that for some tables the first identity value is 1 and for other tables it&#8217;s starting from 2?</p>
<p>Let&#8217;s demonstrate it.</p>
<p>We will create two tables and fill in some data. These tables are absolutely the same including the IDENTITY specification. The only difference is their name.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DROP TABLE IF EXISTS [dbo].[To_Be_Deleted]
DROP TABLE IF EXISTS [dbo].[To_Be_Truncated]
GO

CREATE TABLE [dbo].[To_Be_Deleted] (
	Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY
)
GO
CREATE TABLE [dbo].[To_Be_Truncated] (
	Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY
)
GO

INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
GO
INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
GO

SELECT 'To_Be_Deleted', * FROM [dbo].[To_Be_Deleted] UNION ALL
SELECT 'To_Be_Truncated', * FROM [dbo].[To_Be_Truncated]
GO</pre>
<p><img decoding="async" class="alignnone wp-image-5273" src="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1.png" alt="" width="189" height="113" srcset="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1.png 333w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1-300x179.png 300w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1-150x90.png 150w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1-160x96.png 160w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_1-320x191.png 320w" sizes="(max-width: 189px) 100vw, 189px" /></p>
<p>We will also check the current identity value after data is inserted. It&#8217;s the same.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">SELECT 'To_Be_Deleted',	'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Deleted]') UNION ALL
SELECT 'To_Be_Truncated', 'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Truncated]')
GO</pre>
<p><img decoding="async" class="alignnone wp-image-5274" src="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2.png" alt="" width="418" height="38" srcset="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2.png 737w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-300x27.png 300w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-150x14.png 150w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-360x33.png 360w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-160x15.png 160w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-320x29.png 320w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-520x47.png 520w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_2-720x65.png 720w" sizes="(max-width: 418px) 100vw, 418px" /></p>
<p>Now is time to remove data from our tables. There is a difference coming: We will empty the first table via the DELETE command and the second one will be emptied using the TRUNCATE command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DELETE FROM [dbo].[To_Be_Deleted]
TRUNCATE TABLE [dbo].[To_Be_Truncated]
GO

DBCC CHECKIDENT('[dbo].[To_Be_Deleted]', RESEED, 1)
DBCC CHECKIDENT('[dbo].[To_Be_Truncated]', RESEED, 1)
GO

SELECT 'To_Be_Deleted',	'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Deleted]') UNION ALL
SELECT 'To_Be_Truncated', 'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Truncated]')
GO</pre>
<p>We will check the current identity value now: It&#8217;s the same.</p>
<p><img decoding="async" class="alignnone wp-image-5275" src="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3.png" alt="" width="407" height="37" srcset="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3.png 737w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-300x27.png 300w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-150x14.png 150w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-360x33.png 360w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-160x15.png 160w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-320x29.png 320w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-520x47.png 520w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_3-720x65.png 720w" sizes="(max-width: 407px) 100vw, 407px" /></p>
<p>Next, we will again insert some data into our tables. Exactly as before.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Deleted] DEFAULT VALUES
GO

INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
INSERT INTO [dbo].[To_Be_Truncated] DEFAULT VALUES
GO

SELECT 'To_Be_Deleted', * FROM [dbo].[To_Be_Deleted] UNION ALL
SELECT 'To_Be_Truncated', * FROM [dbo].[To_Be_Truncated]
GO

SELECT 'To_Be_Deleted', 'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Deleted]') UNION ALL
SELECT 'To_Be_Truncated', 'IDENT_CURRENT', IDENT_CURRENT('[dbo].[To_Be_Truncated]')
GO</pre>
<p>If we will check data in both tables, there is the surprise immediately visible:</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-5276" src="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_4.png" alt="" width="162" height="122" srcset="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_4.png 264w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_4-133x100.png 133w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_4-160x121.png 160w" sizes="auto, (max-width: 162px) 100vw, 162px" /></p>
<p>The table which was emptied using the DELETE command has the first identity value 2 whereas the second table emptied using the TRUNCATE command has the first identity value 1!</p>
<p>This difference is also approved via the IDENT_CURRENT() function.</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-5277" src="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5.png" alt="" width="363" height="33" srcset="https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5.png 737w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-300x27.png 300w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-150x14.png 150w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-360x33.png 360w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-160x15.png 160w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-320x29.png 320w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-520x47.png 520w, https://sqlpowered.com/wp-content/uploads/2021/12/DBCC_CHECKIDENT_RESEED_Truncate_Delete_5-720x65.png 720w" sizes="auto, (max-width: 363px) 100vw, 363px" /></p>
<p>I know that this behavior isn&#8217;t too intuitive at first look. But what is positive (only:) about it is that it&#8217;s properly documented at the right place in the <a href="https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-checkident-transact-sql?view=sql-server-ver15" target="_blank" rel="noopener">DBCC CHECKIDENT()</a> command description:</p>
<p><em>The current identity value is set to the new_reseed_value. If no rows have been inserted into the table since the table was created, or if all rows have been removed by using the <strong>TRUNCATE TABLE</strong> statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. If rows are present in the table, or if all rows have been removed by using the <strong>DELETE</strong> statement, the next row inserted uses new_reseed_value + the current increment value.</em></p>
<p>To be honest, seniors should know, for sure. But juniors are spending hours investigating this based on my experience. There is a good comparison of DELETE vs TRUNCATE on <a href="https://www.sqlshack.com/difference-between-sql-truncate-and-sql-delete-statements-in-sql-server/" target="_blank" rel="noopener">sqlhack.com</a>, but still, the RESEED issue isn&#8217;t clearly explained there.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/dbcc-checkident-reseed-behaves-differently-when-truncate-or-delete-was-used-on-the-table/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>IDENTITY sloupec se záporným inkrementem</title>
		<link>https://sqlpowered.com/identity-sloupec-s-negativnim-inkrementem/</link>
					<comments>https://sqlpowered.com/identity-sloupec-s-negativnim-inkrementem/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Tue, 17 Jan 2017 09:21:33 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[tables]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=466</guid>

					<description><![CDATA[Kromě klasického nastavení IDENTITY(1,1), tedy inkrementace od jedničky a vždy o +1, můžeme nastavit inkrement i záporný, tedy vždy odečítat jedničku, jak je vidět v příkladu níže. CREATE TABLE dbo.SampleTable ( Id INT IDENTITY(0, -1) NOT NULL PRIMARY KEY ) GO INSERT dbo.SampleTable DEFAULT VALUES GO 5 SELECT * FROM...]]></description>
										<content:encoded><![CDATA[<p>Kromě klasického nastavení IDENTITY(1,1), tedy inkrementace od jedničky a vždy o +1, můžeme nastavit inkrement i záporný, tedy vždy odečítat jedničku, jak je vidět v příkladu níže.<span id="more-466"></span></p>
<pre class="lang:tsql decode:true">CREATE TABLE dbo.SampleTable
(
    Id INT IDENTITY(0, -1) NOT NULL PRIMARY KEY
)
GO

INSERT dbo.SampleTable
    DEFAULT VALUES
GO 5

SELECT * FROM dbo.SampleTable
GO</pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-467" src="https://sqlpowered.com/wp-content/uploads/2015/06/IdentityWithNegativeIncrement.jpg" alt="IdentityWithNegativeIncrement" width="59" height="115" /></p>
<p>Kromě záporného inkrementu může nastavit i zápornou výchozí hodnotu prvního záznamu, například takto:</p>
<pre class="lang:tsql decode:true ">CREATE TABLE dbo.SampleTable
(
    Id SMALLINT IDENTITY(-32768, 1) NOT NULL PRIMARY KEY
)
GO

INSERT INTO dbo.[SampleTable]
	DEFAULT VALUES
GO 5

SELECT * FROM dbo.[SampleTable]
GO</pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1348" src="https://sqlpowered.com/wp-content/uploads/2015/01/NegativeIdentitySeed.png" alt="NegativeIdentitySeed" width="83" height="115" /></p>
<p>Záporná výchozí hodnota nám přináší obrovskou výhodu pro scénáře, kdy pracujeme například s rozsáhlými faktovými tabulkami a snažíme se ušetřit velikost uložených dat tím, že dimenzionální klíče budou mít co nejmenší datový typ, např. tiny nebo smallint. Pokud ale například počet členů naší dimenze bude někde mezi 30 a 60 tisíci, napadne nás, že již nemůžeme použít smallint datový typ, protože jeho maximální hodnota je 32,767. To ale není pravda, pokud si uvědomíme, že jeho minimální hodnota je -32,768, takže celkem včetně nuly lze do tohoto datového typu uložit 65,535 hodnot. A právě nastavením identity sloupce s výchozí hodnotou -32768 můžeme celý tento rozsah vyčerpat.</p>
<p>Hodnoty klíčů s negativním znamínkem můžou být pochopitelně ne úplně esteticky přijatelné a je třeba na znaménko myslet i při dotazech. Lehce vylepšit lze uvedenou situaci tím, že jako výchozí hodnotu zvolíme maximum 32767 a negativní inkrement a většina našich hodnot bude v kladném rozsahu.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/identity-sloupec-s-negativnim-inkrementem/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>IDENTITY column with negative increment</title>
		<link>https://sqlpowered.com/identity-column-with-negative-increment/</link>
					<comments>https://sqlpowered.com/identity-column-with-negative-increment/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Sat, 17 Jan 2015 09:21:32 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[tables]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=1366</guid>

					<description><![CDATA[Instead of standard setting IDENTITY(1,1) &#8211; increasing value from 1 by +1, we can set increment to be negative as by the following example: CREATE TABLE dbo.SampleTable ( Id INT IDENTITY(0, -1) NOT NULL PRIMARY KEY ) GO INSERT dbo.SampleTable DEFAULT VALUES GO 5 SELECT * FROM dbo.SampleTable GO We can...]]></description>
										<content:encoded><![CDATA[<p>Instead of standard setting IDENTITY(1,1) &#8211; increasing value from 1 by +1, we can set increment to be negative as by the following example:<span id="more-1366"></span></p>
<pre class="lang:tsql decode:true">CREATE TABLE dbo.SampleTable
(
    Id INT IDENTITY(0, -1) NOT NULL PRIMARY KEY
)
GO

INSERT dbo.SampleTable
    DEFAULT VALUES
GO 5

SELECT * FROM dbo.SampleTable
GO</pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-467" src="https://sqlpowered.com/wp-content/uploads/2015/06/IdentityWithNegativeIncrement.jpg" alt="IdentityWithNegativeIncrement" width="59" height="115" /></p>
<p>We can also set value for the first loaded row (SEED) to be negative:</p>
<pre class="lang:tsql decode:true ">CREATE TABLE dbo.SampleTable
(
    Id SMALLINT IDENTITY(-32768, 1) NOT NULL PRIMARY KEY
)
GO

INSERT INTO dbo.[SampleTable]
	DEFAULT VALUES
GO 5

SELECT * FROM dbo.[SampleTable]
GO</pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1348" src="https://sqlpowered.com/wp-content/uploads/2015/01/NegativeIdentitySeed.png" alt="NegativeIdentitySeed" width="83" height="115" /></p>
<p>Negative value of the first row has the advantage in case we are working with large tables and we will eliminate table size and storage usage by using the smallest datatype we can. In this case when we will start for example for SMALLINT datatype identity sequence from -32768, we can insert in this column 65,535 values and if your data fits into this range there is no reason to use INT in this case. For sure we should think all the time that (-) sign is presented there, but that´s not an issue for ORMs or properly parameterized queries.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/identity-column-with-negative-increment/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Identity sloupce a práce s nimi</title>
		<link>https://sqlpowered.com/identity-sloupce-a-prace-s-nimi/</link>
					<comments>https://sqlpowered.com/identity-sloupce-a-prace-s-nimi/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Tue, 30 Dec 2014 22:43:24 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[tables]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=443</guid>

					<description><![CDATA[Dnes se podíváme na stručný přehled všech důležitých funkcí, globálních proměnných a DBCC příkazů pro práci s IDENTITY hodnotami v SQL Serveru. Programátoři často chybují, kdy použít kterou z nich, zejména pak z neznalosti toho, jak se jednotlivé funkce chovají v rámci kontextu, ve kterém jsou volány (tělo uložené procedury,...]]></description>
										<content:encoded><![CDATA[<p>Dnes se podíváme na stručný přehled všech důležitých funkcí, globálních proměnných a DBCC příkazů pro práci s IDENTITY hodnotami v SQL Serveru. Programátoři často chybují, kdy použít kterou z nich, zejména pak z neznalosti toho, jak se jednotlivé funkce chovají v rámci kontextu, ve kterém jsou volány (tělo uložené procedury, triggery a jejich vzájemné vztahy). Snad to následující shrnutí osvětlí.</p>
<p><span id="more-443"></span></p>
<p><strong>@@IDENTITY</strong></p>
<ul>
<li>vrací datový typ <strong>numeric (38,0)</strong></li>
<li>po dokončení INSERT, SELECT INTO nebo BULKCOPY vrací poslední hodnotu identity sloupce v tabulce</li>
<li>pokud není v tabulce identity sloupec, vrací NULL</li>
<li>pokud je vloženo více řádků a vygenerováno více identity hodnot, je vrácena poslední hodnota</li>
<li>pokud dojde k odpálení triggeru, který zvýší identity hodnotu, je @@IDENTITY tato triggerem zvýšená hodnota</li>
<li>@@IDENTITY se nevrací k předchozí hodnotě, pokud dojde k rolbacku nebo failu insertu – např. dojde-li k failu na IGNORE_DUP_KEY, identity pro danou tabulku se stále inkrementuje</li>
<li>@@IDENTITY, SCOPE_IDENTITY a IDENT_CURRENT jsou shodné funkce, všechny vrací poslední hodnotu insertu do identity sloupce v tabulce</li>
<li>@@IDENTITY a SCOPE_IDENTITY vrací poslední identity hodnotu vygenerovanou v kterékoliv tabulce aktuální session. SCOPE_IDENTITY vrací hodnotu pouze ve scope, @@IDENTITY nezávisle na scopu.</li>
<li>IDENT_CURRENT není limitována session ani scopem, ale pouze konkrétní tabulkou, pro niž nezávisle vrací poslední identity</li>
<li>@@IDENTITY platí pouze pro aktuální session na lokálním serveru. Nelze ji volat pro linkovaný nebo vzdálený server =&gt; je nutné vytvořit na vzdáleném serveru uloženou proceduru, v ní použít @@IDENTITY a volat ji v kontextu vzdáleného serveru</li>
<li>není vhodné používat v replikacích – replikační triggery mohou ovlivnit identity hodnotu, protože není vráceno identity posledního řádku tabulky, ale identity řádku vloženého triggerem do replikačních tabulek =&gt; již existující procedury je nutné přepsat na SCOPE_IDENTITY()</li>
</ul>
<p><!--more--></p>
<p><strong>SCOPE_IDENTITY()</strong></p>
<ul>
<li><strong> </strong>vrací poslední identity hodnotu v rámci scopu =&gt; scopem se rozumí modul: uložená procedura, trigger, funkce nebo batch</li>
<li>vrací numeric</li>
<li>rozdíl mezi @@IDENTITY a SCOPE_IDENTITY: (BOL příklad)
<ul>
<li>For example, there are two tables, <strong>T1</strong> and <strong>T2</strong>, and an INSERT trigger is defined on <strong>T1</strong>. When a row is inserted to <strong>T1</strong>, the trigger fires and inserts a row in <strong>T2</strong>. This scenario illustrates two scopes: the insert on <strong>T1</strong>, and the insert on <strong>T2</strong> by the trigger.</li>
<li>Assuming that both <strong>T1</strong> and <strong>T2</strong> have identity columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on <strong>T1</strong>. @@IDENTITY will return the last identity column value inserted across any scope in the current session. This is the value inserted in <strong>T2</strong>. SCOPE_IDENTITY() will return the IDENTITY value inserted in <strong>T1</strong>. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function will return the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.</li>
</ul>
</li>
</ul>
<p><strong>IDENT_CURRENT()</strong></p>
<ul>
<li> vrací identity poslední operace pro specifickou tabulku a to nezávisle na session a scope</li>
<li>použití: <strong>IDENT_CURRENT( ‘table_name’ )</strong></li>
<li>vrací numeric(38,0)</li>
<li>pokud caller nemá práva na tabulku, vrací NULL nebo error</li>
<li>pokud tabulka neexistuje, vrací NULL</li>
<li>pokud je tabulka prázdná, vrací první identity seed</li>
<li>pozor na rozdíl při mazání pomocí DELETE (neruší identity) a TRUNCATE (nuluje identity)</li>
<li>odrolování transakce nebo chyba nikdy nevrací hodnotu identity =&gt; číslo zůstává inkrementované jako by transakce byla commitnuta</li>
</ul>
<p><strong>IDENT_INCR()</strong></p>
<ul>
<li> vrací velikost incrementu (např. 1)</li>
<li>použití: <strong>IDENT_INCR ( ‘table_or_view’ )</strong></li>
</ul>
<p><strong>IDENT_SEED()</strong></p>
<ul>
<li>vrací počáteční hodnotu, od které startuje increment</li>
<li>použití: <strong>IDENT_SEED ( ‘table_or_view’ )</strong></li>
</ul>
<p><strong>DBCC CHECKIDENT</strong></p>
<ul>
<li> vrací počáteční hodnotu, od které startuje increment</li>
<li>použití:
<ul>
<li><strong>DBCC CHECKIDENT  ( table_name  [ , { NORESEED | { RESEED [ , new_reseed_value ] } } ])<br />
[ WITH NO_INFOMSGS ]</strong></li>
</ul>
</li>
<li>zjistí aktuální hodnotu identity ve specifické tabulce a provede případně její změnu – umožňuje nastavit novou příští identity hodnotu pro tabulku</li>
<li>tabulka musí obsahovat identity sloupec</li>
<li>nelze tímto změnit inicializační seed hodnotu při vytvoření sloupce ani přeseedovat exitující data =&gt; na to je nutné vytvořit znovu identity sloupec</li>
<li>příklad: <strong>DBCC CHECKIDENT (“HumanResources.Employee”, RESEED, 30);</strong></li>
</ul>
<pre class="lang:tsql decode:true ">DBCC CHECKIDENT ('dbo.SampleTable', RESEED, 0);</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/identity-sloupce-a-prace-s-nimi/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
