<?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>T-SQL</title><link>http://sqlserver.ro/forums/15/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2495.aspx</link><pubDate>Thu, 16 Aug 2007 11:17:22 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2495</guid><dc:creator>rsocol</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2495.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2495</wfw:commentRss><description>&lt;p&gt;Dacă totuşi cerinţa nu era cea scrisă de mine, iar condiţia din mesajul tău anterior furniza rezultatele dorite, atunci poţi să o simplifici astfel:&lt;/p&gt;&lt;p&gt;f.soc=0 or (f.soc is null and @soc=0) or f.soc=@soc&lt;/p&gt;&lt;p&gt;Răzvan&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2494.aspx</link><pubDate>Thu, 16 Aug 2007 10:49:02 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2494</guid><dc:creator>rsocol</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2494.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2494</wfw:commentRss><description>&lt;p&gt;Dacă cerinţa era cea scrisă de mine în al doilea mesaj (adică schimbarea e în funcţie de valoarea parametrului, nu în funcţie de valoarea câmpului), atunci cred că rezolvarea ta nu este bună (lipseşte o paranteză în jurul primului OR). De exemplu, atunci când @soc=3 sunt incluse şi rândurile în care f.soc=0 şi rândurile în care f.soc=3, dar nu şi rândurile în care f.soc=2. Asta vroiai ?&lt;br&gt;&lt;/p&gt;&lt;p&gt;Răzvan&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2493.aspx</link><pubDate>Thu, 16 Aug 2007 10:41:57 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2493</guid><dc:creator>Abadon83</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2493.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2493</wfw:commentRss><description>&lt;p&gt;mersi&lt;/p&gt;&lt;p&gt;eu vroiam sa schimbam conditia de la clauza where in fct de valoarea campului soc.&lt;/p&gt;&lt;p&gt;da am rezolvat asa&lt;/p&gt;&lt;p&gt;(f.soc=0&amp;nbsp; or f.soc is null and @soc=0) or (f.soc=@soc and @soc!=0 and f.soc is not null )"&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2492.aspx</link><pubDate>Thu, 16 Aug 2007 10:41:00 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2492</guid><dc:creator>rsocol</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2492.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2492</wfw:commentRss><description>&lt;p&gt;Cred că ai exprimat greşit cerinţa atunci când ai scris:&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://sqlserver.ro/Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;Abadon83:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;&lt;/p&gt;&lt;p&gt;daca f.soc =0 vreau ca si conditia de where sa fie f.soc=0 sau f.soc is null&lt;/p&gt;&lt;p&gt;si daca f.soc!=0 vreau ca sa fie f.soc=@soc,unde @soc e valoarea dintr-un combo filtru.&lt;/p&gt;&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;&lt;p&gt;Cumva vroiai să scrii:&lt;/p&gt;&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;/p&gt;&lt;p&gt;daca @soc =0 vreau ca si conditia de where sa fie f.soc=0 sau f.soc is null&lt;/p&gt;&lt;p&gt;si daca @soc!=0 vreau ca sa fie f.soc=@soc,unde @soc e valoarea dintr-un combo filtru.&lt;/p&gt;&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;&lt;p&gt;?&amp;nbsp;&lt;/p&gt;&lt;p&gt;Răzvan&lt;br&gt;&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2491.aspx</link><pubDate>Thu, 16 Aug 2007 10:38:11 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2491</guid><dc:creator>rsocol</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2491.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2491</wfw:commentRss><description>&lt;p&gt;Nu prea înţeleg ce ceri... poţi să faci:&lt;/p&gt;&lt;p&gt;&lt;b&gt;WHERE (CASE WHEN a=b THEN c ELSE d END)=e&lt;/b&gt;&lt;/p&gt;&lt;p&gt;De exemplu, considerând tabela Employees din Northwind:&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table style="border-collapse:collapse;width:328pt;" cellpadding="0" cellspacing="0"&gt;
 
 
 
 
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl25" style="height:12.75pt;width:62pt;"&gt;EmployeeID&lt;/td&gt;
  &lt;td class="xl24" style="width:53pt;"&gt;LastName&lt;/td&gt;
  &lt;td class="xl24" style="width:53pt;"&gt;FirstName&lt;/td&gt;
  &lt;td class="xl24" style="width:113pt;"&gt;Title&lt;/td&gt;
  &lt;td class="xl24" style="width:47pt;"&gt;City&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;1&lt;/td&gt;
  &lt;td&gt;Davolio&lt;/td&gt;
  &lt;td&gt;Nancy&lt;/td&gt;
  &lt;td&gt;Sales Representative&lt;/td&gt;
  &lt;td&gt;Seattle&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;2&lt;/td&gt;
  &lt;td&gt;Fuller&lt;/td&gt;
  &lt;td&gt;Andrew&lt;/td&gt;
  &lt;td&gt;Vice President, Sales&lt;/td&gt;
  &lt;td&gt;Tacoma&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;3&lt;/td&gt;
  &lt;td&gt;Leverling&lt;/td&gt;
  &lt;td&gt;Janet&lt;/td&gt;
  &lt;td&gt;Sales Representative&lt;/td&gt;
  &lt;td&gt;Kirkland&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;4&lt;/td&gt;
  &lt;td&gt;Peacock&lt;/td&gt;
  &lt;td&gt;Margaret&lt;/td&gt;
  &lt;td&gt;Sales Representative&lt;/td&gt;
  &lt;td&gt;Redmond&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;5&lt;/td&gt;
  &lt;td&gt;Buchanan&lt;/td&gt;
  &lt;td&gt;Steven&lt;/td&gt;
  &lt;td&gt;Sales Manager&lt;/td&gt;
  &lt;td&gt;London&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;&lt;i&gt;6&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Suyama&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Michael&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Sales Representative&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;London&lt;/i&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;&lt;i&gt;7&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;King&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Robert&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Sales Representative&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;London&lt;/i&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;&lt;i&gt;8&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Callahan&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Laura&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Inside Sales Coordinator&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Seattle&lt;/i&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr style="height:12.75pt;"&gt;
  &lt;td class="xl26" style="height:12.75pt;"&gt;&lt;i&gt;9&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Dodsworth&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Anne&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;Sales Representative&lt;/i&gt;&lt;/td&gt;
  &lt;td&gt;&lt;i&gt;London&lt;/i&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt;&lt;p&gt;&lt;br&gt;Să presupunem că vrei să selectezi cu condiţia ca City='London' dacă Title='Sales Representative', însă dacă e alt Title, vrei să pui condiţia City='Seattle', deci să selectezi rândurile care sunt scrise cu &lt;i&gt;italic&lt;/i&gt; mai sus. &lt;/p&gt;&lt;p&gt;În acest caz, NU poţi să scrii:&lt;/p&gt;&lt;p&gt;&lt;b&gt;WHERE CASE WHEN Title='Sales Representative' THEN City='London' ELSE City='Seattle' END&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Ci trebuie să spui:&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;WHERE CASE WHEN Title='Sales Representative' THEN 'London' ELSE 'Seattle' END=City&lt;/b&gt;&lt;/p&gt;&lt;p&gt;sau, mai clar:&lt;/p&gt;&lt;p&gt;&lt;b&gt;WHERE (Title='Sales Representative' AND City='London') OR (Title&amp;lt;&amp;gt;'Sales Representative' AND City='Seattle')&lt;br&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;(de fapt aici ar fi trebuit să scriem "ISNULL(Title,'')&amp;lt;&amp;gt;...", pt. ca expresia să fie echivalentă cu cea de mai sus, însă dacă presupunem că Title nu permite NULL-uri, nu ar fi nevoie)&lt;/p&gt;&lt;p&gt;sau, mai complicat:&lt;/p&gt;&lt;p&gt;&lt;b&gt;WHERE CASE WHEN Title='Sales Representative'&amp;nbsp; THEN &lt;br&gt;&amp;nbsp; CASE WHEN City='London' THEN 1 ELSE 0 END&lt;br&gt;ELSE&lt;br&gt;&amp;nbsp; CASE WHEN City='Seattle' THEN 1 ELSE 0 END&lt;br&gt;END=1&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Răzvan&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2490.aspx</link><pubDate>Thu, 16 Aug 2007 10:33:30 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2490</guid><dc:creator>Abadon83</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2490.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2490</wfw:commentRss><description>&lt;p&gt;daca f.soc =0 vreau ca si conditia de where sa fie f.soc=0 sau f.soc is null&lt;/p&gt;&lt;p&gt;si daca f.soc!=0 vreau ca sa fie f.soc=@soc,unde @soc e valoarea dintr-un combo filtru.&lt;/p&gt;&lt;p&gt;Ma refeream la sintaxa corecta&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2489.aspx</link><pubDate>Thu, 16 Aug 2007 10:21:08 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2489</guid><dc:creator>Diana</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2489.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2489</wfw:commentRss><description>Nu prea inteleg ce vrei. De exemplu, daca pui conditia "WHERE f.soc =
0" atunci setul de rezultate va contine toate inregistrarile pentru
care valoarea campului "f.soc" este 0. Poti sa detaliezi cerinta?&lt;br&gt;</description></item><item><title>clauza where cu case sau if</title><link>http://sqlserver.ro/forums/thread/2488.aspx</link><pubDate>Thu, 16 Aug 2007 09:59:21 GMT</pubDate><guid isPermaLink="false">963f3a18-65dd-4aad-905c-b785cfe1d7c7:2488</guid><dc:creator>Abadon83</dc:creator><slash:comments>0</slash:comments><comments>http://sqlserver.ro/forums/thread/2488.aspx</comments><wfw:commentRss>http://sqlserver.ro/forums/commentrss.aspx?SectionID=15&amp;PostID=2488</wfw:commentRss><description>&lt;p&gt;am un select cu clauza where&lt;/p&gt;&lt;p&gt;pot pune la clauza where case sau if&lt;/p&gt;&lt;p&gt;adica vreau sa pun ceva de genul&lt;/p&gt;&lt;p&gt;SELECT p.id, p.id AS masina_id, p.tip_masina, p.marca_model, f.id AS Expr1, f.id_masina, f.polita, f.data_asig,f.soc, f.data_exp, f.suma_asig, f.prima, f.val_casco, f.procent, f.desp_casco, f.RSA FROM tblMasina AS p left JOIN tblASIBAN AS f ON p.id = f.id_masina where case when (f.soc=0 ) then&amp;nbsp; f.soc=0&amp;nbsp; else f.soc=3 end&lt;/p&gt;&lt;p&gt;primesc eroarea&lt;/p&gt;&lt;p&gt;Msg 102, Level 15, State 1, Line 1&lt;br&gt;Incorrect syntax near '='.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Adica as vrea ca in clauza where sa aiba o conditie daca f.soc=0 si alta daca f.soc!=0&lt;/p&gt;&lt;p&gt;Se poate,ceva sugestii?&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>