<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlserver.ro/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Intrebari generale</title><link>http://sqlserver.ro/forums/16/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7585.aspx</link><pubDate>Wed, 09 Sep 2009 09:05:38 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7585</guid><dc:creator>victordomain</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7585.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7585</wfw:commentRss><description>&lt;P&gt;Multumesc foarte mult pentru raspunsuri.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;M-am lamurit, asadar ramane return SCOPE_IDENTITY()&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7550.aspx</link><pubDate>Tue, 01 Sep 2009 05:43:01 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7550</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7550.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7550</wfw:commentRss><description>SCOPE_IDENTITY este functia pe care trebuie sa o folositi. Cum a mentionat si Catalin, in cazul existentei unui trigger e posibil sa obtineti rezultate gresite.&lt;br&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7549.aspx</link><pubDate>Mon, 31 Aug 2009 14:14:59 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7549</guid><dc:creator>katalyn</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7549.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7549</wfw:commentRss><description>Nu este OK&lt;br /&gt;&lt;br /&gt;"The calling stored procedure or Transact-SQL statement must be rewritten to use the SCOPE_IDENTITY() function which will return the latest identity used within the scope of that user statement, and not the identity within the scope of the nested trigger used by replication."&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;@@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.&lt;br /&gt;&lt;br /&gt;Pentru a intelege mai bine despre ce este vorba, te rog sa analizezi exemplul de mai jos:&lt;br /&gt;create table dbo.[Tabela cu trigger] (&lt;br /&gt;	coloana  int identity (1,1)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;create table dbo.[Trigger-ul face insert] (&lt;br /&gt;	coloana  int identity (10000,1000)&lt;br /&gt;)	&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;create trigger trg_ on dbo.[Tabela cu trigger]&lt;br /&gt;for insert as begin&lt;br /&gt;	insert into dbo.[Trigger-ul face insert]default values&lt;br /&gt;end&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;insert into dbo.[Tabela cu trigger] default values&lt;br /&gt;select @@Identity as [@@Identity], scope_identity() as [scope_identity]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;PS: Coloana [@@Identity] va avea valoarea 10000 (identity "produs" in trigger) iar celalalta coloana va avea valoarea 1 ("produs" in procedura)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LE: mesajul initial era asa:&lt;br /&gt;Procedura arata cam asa:&lt;br /&gt;CREATE PROCEDURE Indentity1&lt;br /&gt;AS&lt;br /&gt;insert into tabela1 default values&lt;br /&gt;return @@IDENTITY&lt;br /&gt;GO&lt;br /&gt;Credeti ca este ok? Mie imi returneaza exact ce trebuie."&lt;br /&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7547.aspx</link><pubDate>Mon, 31 Aug 2009 13:50:57 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7547</guid><dc:creator>victordomain</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7547.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7547</wfw:commentRss><description>&lt;P&gt;Procedura arata cam asa:&lt;/P&gt;
&lt;P&gt;CREATE PROCEDURE Indentity1&lt;BR&gt;AS&lt;BR&gt;insert into&amp;nbsp;tabela1 default values&lt;BR&gt;return SCOPE_IDENTITY()&lt;BR&gt;GO&lt;/P&gt;
&lt;P&gt;Credeti ca este ok? Mie imi returneaza exact ce trebuie.&lt;/P&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7540.aspx</link><pubDate>Mon, 31 Aug 2009 11:27:51 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7540</guid><dc:creator>xmldeveloper</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7540.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7540</wfw:commentRss><description>In descrierea initiala a scenariului mentionati o procedura stocata pentru inserarea unei noi inregistrari. &lt;br&gt;Subscriu la aceasta idee sugerand si includerea unui parametru de output pentru preluarea valorii generate pentru coloana cu proprietatea IDENTITY.&lt;br&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7539.aspx</link><pubDate>Mon, 31 Aug 2009 11:22:12 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7539</guid><dc:creator>victordomain</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7539.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7539</wfw:commentRss><description>&lt;P&gt;Adica este EXACT ce-mi trebuie.&lt;/P&gt;
&lt;P&gt;Multumesc mult. &lt;/P&gt;
&lt;P&gt;Topic closed!&lt;/P&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7538.aspx</link><pubDate>Mon, 31 Aug 2009 11:21:09 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7538</guid><dc:creator>ThePuiu</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7538.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7538</wfw:commentRss><description>daca executi linia scrisa de mine mai sus si un alt user executa aceeasi linie la 1 milisecunda mai tirziu, fiecare o sa primeasca corect ID-ul recordului respectiv (cel inserat de el).</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7537.aspx</link><pubDate>Mon, 31 Aug 2009 11:08:45 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7537</guid><dc:creator>katalyn</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7537.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7537</wfw:commentRss><description>"Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch."&lt;br /&gt;&lt;br /&gt;http://msdn.microsoft.com/en-us/library/ms190315.aspx&lt;br /&gt;&lt;br /&gt;(Adica le executi una dupa alta)</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7536.aspx</link><pubDate>Mon, 31 Aug 2009 10:53:52 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7536</guid><dc:creator>victordomain</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7536.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7536</wfw:commentRss><description>&lt;P&gt;Dar daca intre &lt;STRONG&gt;insert into tabela (col_mth) values(0);&lt;/STRONG&gt; si &lt;STRONG&gt;select scope_identity();&amp;nbsp;&lt;/STRONG&gt;un alt user a facut un insert folosind aceeasi metoda si&amp;nbsp;acel &lt;STRONG&gt;scope_identity()&lt;/STRONG&gt; apartine de fapt altui insert facut cu o milisecunda mai devreme? &lt;/P&gt;
&lt;P&gt;Sau sa inteleg ca aceste doua linii trebuie sa fie de fapt incluse intr-o procedura stocata si astfel cele doua operatii sunt cumva vazute ca "atomice" (adica executate deodata)?&lt;/P&gt;
&lt;P&gt;Multumesc.&lt;/P&gt;</description></item><item><title>Re: obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7535.aspx</link><pubDate>Mon, 31 Aug 2009 09:14:49 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7535</guid><dc:creator>ThePuiu</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7535.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7535</wfw:commentRss><description>insert into tabela (col_math) values(0);select scope_identity();&lt;br /&gt;&lt;br /&gt;asa poti sa fii sigur ca iti intoarce ID-ul recordului proaspat inserat.&lt;br /&gt;Sper ca am inteles bine ce vroiai...!&lt;br /&gt;Spor!</description></item><item><title>obtinere camp identity in avans pentru trimiterea catre client</title><link>http://sqlserver.ro/forums/thread/7534.aspx</link><pubDate>Mon, 31 Aug 2009 08:37:42 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:7534</guid><dc:creator>victordomain</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/7534.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=16&amp;PostID=7534</wfw:commentRss><description>&lt;P&gt;Salut,&lt;/P&gt;
&lt;P&gt;Asadar avem urmatoarea situatie. O tabela (&lt;STRONG&gt;tabela1&lt;/STRONG&gt;), doua coloane (&lt;STRONG&gt;col_key&lt;/STRONG&gt; [cheie primara], &lt;STRONG&gt;col_math&lt;/STRONG&gt;).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Col_key&lt;/STRONG&gt; este de tip identity si din cunostintele mele la fiecare insert acea valoare va fi incrementata cu 1(sau alta valoare). Problema este ca valoare lui &lt;STRONG&gt;col_math&lt;/STRONG&gt; depinde strict de valoarea lui &lt;STRONG&gt;col_key&lt;/STRONG&gt; proaspat generat.&lt;/P&gt;
&lt;P&gt;Adica atunci cand eu fac un insert (un insert gol initial pentru ca nu stiu inca valoarea lui &lt;STRONG&gt;col_key&lt;/STRONG&gt;) valoare lui &lt;STRONG&gt;col_math&lt;/STRONG&gt; sa ia automat valoarea lui &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;col_key*2&lt;/STRONG&gt; (doar ca exemplu pentru ca e cu totul o alta operatie ce nu are legatura cu matematica si nu poate fi tranpus in TSQL).&lt;/P&gt;
&lt;P&gt;Inseram un camp vid si rezulta sa zicem &lt;STRONG&gt;col_key&lt;/STRONG&gt; = 417. Acum trebuie sa gasesc o metoda prin care acel &lt;STRONG&gt;col_key&lt;/STRONG&gt; sa-mi fie returnat in aplicatie, acolo sa fac operatiile mele ca la final sa fac un update de tipul &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;UPDATE tabela 1 set col_math= valoare_mea_ce_depinde_de_col_key where col_key=417&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Problema este ca aplicatia este concurenta si pot sa am chiar 20-30 de clienti conectata la ea asadar o abordare de tipul:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;SELECT col_key top 1 where col_math = null&lt;/STRONG&gt; nu poate functiona deoarece pot sa am mai multi clienti care tocmai au facut acel insert null si deci mi se poate returna &lt;STRONG&gt;col_key-&lt;/STRONG&gt;ul corespunzator altui client.&lt;/P&gt;
&lt;P&gt;Metoda la care m-am gandit eu:&lt;BR&gt;Pe partea de server fac o procedura stocata care sa-mi faca acel insert vid si-mi returneaza fix acel &lt;STRONG&gt;col_key&lt;/STRONG&gt; . &lt;BR&gt;Pe partea de client apelez procedura stocata si fac acel update.&lt;/P&gt;
&lt;P&gt;Acum nu stiu cum sa fac procedura aceea stocata ca sa fi sigur ca fiecare client concurent primeste exact &lt;STRONG&gt;col_key&lt;/STRONG&gt;-ul care trebuie. Sau mai pe scurt, cum generez acel identity in avas (dar sa mai fie si rezervat) urmad ca apoi sa-l transmit catre client.&lt;/P&gt;
&lt;P&gt;Stiu ca e simplu dar nu am experienta cu SQL.&lt;/P&gt;
&lt;P&gt;Multumesc pentru sprijin.&lt;/P&gt;</description></item></channel></rss>