Transact-SQL ForumTSQL challenges? This is the place for advice / discussions© 2009 Microsoft Corporation. All rights reserved.Wed, 25 Nov 2009 07:53:54 Z6a68166e-b521-48a8-9454-ec36622eb8aehttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/039d24f6-5c9c-4e11-bb5c-46056261a208http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/039d24f6-5c9c-4e11-bb5c-46056261a208Arnie Rowlandhttp://social.msdn.microsoft.com/Profile/en-US/?user=Arnie%20RowlandTest Post for Moderators<p>This is a test post for Moderators to use when testing the 'self-answer'/points question.<br/><br/>Just reply, and immediately mark it as answer. System latency may require several hours for points to display.<br/><br/>This thread will be deleted by 11/30</p><hr class="sig">You may be only one person in the world, but you may also be the world to one person.Tue, 24 Nov 2009 22:56:42 Z2009-11-25T07:53:54Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/383a92ff-acdf-4f30-9e86-ec9b9967d58fhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/383a92ff-acdf-4f30-9e86-ec9b9967d58fAsharaf Ali Phttp://social.msdn.microsoft.com/Profile/en-US/?user=Asharaf%20Ali%20Psubtracting millisecond from datetime<div>Hi</div> <div><br/></div> <div>Why the below query returns 2009-11-25 23:59:59.997 (input value) as result?</div> <div><br/></div> <div>SELECT DATEADD(ms,1,'2009-11-25 23:59:59.997')</div> <div><br/></div> <div>Asharaf</div><hr class="sig">asharafmail@gmail.comWed, 25 Nov 2009 07:22:28 Z2009-11-25T07:45:38Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/7ce12834-ae84-4664-9573-9dbb3be002bdhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/7ce12834-ae84-4664-9573-9dbb3be002bdLindCumshttp://social.msdn.microsoft.com/Profile/en-US/?user=LindCumsAggregate errors<p>Hi all,  need help on this query.  I need to select the total rents to be paid and I also need to know a count of each amount because rents can be equal or different depending on the service charges.  There may be more than one service charge which is why I am using a sum of the serv.amount.  My problem is that I cannot include the sum(x) in a count statement.  Can anyone help on how to do this accurately, not very good with aggregate functions.  my code is below:</p> <p>DECLARE @ID BIGINT<br/>SET @ID=543355</p> <p>SELECT DISTINCT</p> <p>--rent.valuedate   as rentdate,<br/>--rent.amount      as rent,<br/>--serv.amount      as services,<br/>--expense.amount   as expense,<br/>--ins.amount       as insuarance,      <br/>  <br/>(rent.amount+sum(serv.amount)+expense.amount +ins.amount)  as totalrent,<br/>count((rent.amount+sum(serv.amount)+expense.amount +ins.amount))  as numRents</p> <p>FROM operation op<br/>  INNER JOIN rent  ON co.Did = rent.ownerdid<br/>  INNER JOIN ins  ON rent.ownerdid = ins.aptdid<br/>  INNER JOIN expense ON ins.expnumber=expense.nid<br/>  LEFT OUTER JOIN serv     on ins.servaptdid= serv.did</p> <p>where 1=1<br/>And <a href="mailto:op.did=@id">op.did=@id</a><br/>group by rent.amount,serv.amount,expense.amount,ins.amount<br/>  </p> <p> </p>Wed, 25 Nov 2009 06:38:38 Z2009-11-25T07:26:49Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/eef8c737-49b5-4781-9ecf-9c7effd39b29http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/eef8c737-49b5-4781-9ecf-9c7effd39b29gomshttp://social.msdn.microsoft.com/Profile/en-US/?user=gomsRestoreI have a valid backup in one server (admindb)<br/>i want to restore that backup file into another server(AdminRS)<br/>how to do that?Wed, 25 Nov 2009 05:15:58 Z2009-11-25T06:37:53Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/df76031e-4e47-4538-9288-a2607b8a0221http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/df76031e-4e47-4538-9288-a2607b8a0221Rudy68http://social.msdn.microsoft.com/Profile/en-US/?user=Rudy68Dynamic stored procedure execution form in SQL Server 2005 Management Studio is missing.Hello!<br/> <br/> I'm not sure what happened.  But when I right click on a store procedure, the execute option is gone.  I'm not sure what happened.  I double checked my permissions, everything seems to be right.  I'm looking how I can get that option back.  Running Sql2005.<br/> <br/> Thanks!<br/> <br/> RudyWed, 25 Nov 2009 06:15:50 Z2009-11-25T06:23:32Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/11236f77-b696-4a75-b8b9-2ebeb92b91d8http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/11236f77-b696-4a75-b8b9-2ebeb92b91d8gomshttp://social.msdn.microsoft.com/Profile/en-US/?user=gomsAccess Object <p>I ve created the user &quot;Tom&quot;<br/>I gave rights to access only select tables on sample db<br/>now i want to give rights to view only particular tables like salesorder<br/>other than sales order he wont access</p> <p>How to give privileges?</p>Wed, 25 Nov 2009 06:05:49 Z2009-11-25T06:51:07Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a512be8a-376f-4fc9-8243-78dbdbe59e55http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a512be8a-376f-4fc9-8243-78dbdbe59e55Nishith Shahhttp://social.msdn.microsoft.com/Profile/en-US/?user=Nishith%20ShahHow to delete/drop all the tables from SQL Server Database without using Enterprise Manager?<span style="font-family:Verdana,Geneva,Arial,Sans-serif"><br>How to delete/drop all the tables from SQL Server Database without using Enterprise Manager?</span><br style="font-family:Verdana,Geneva,Arial,Sans-serif"><span style="font-family:Verdana,Geneva,Arial,Sans-serif"><br>I tried using DROP Tables, Truncate Database, Delete and many more but it is not working.  </span><span style="font-family:Verdana,Geneva,Arial,Sans-serif">I want to delete all tables using Query Analyzer, i.e. through SQL Query.</span><br style="font-family:Verdana,Geneva,Arial,Sans-serif"><br style="font-family:Verdana,Geneva,Arial,Sans-serif"><span style="font-family:Verdana,Geneva,Arial,Sans-serif">Please help me out in this concern.</span><br style="font-family:Verdana,Geneva,Arial,Sans-serif"><br style="font-family:Verdana,Geneva,Arial,Sans-serif"><span style="font-family:Verdana,Geneva,Arial,Sans-serif">Nishith Shah</span><br>Wed, 13 Sep 2006 08:42:47 Z2009-11-25T06:04:38Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ae4185b7-cc16-4dc9-98df-640bae800fd1http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ae4185b7-cc16-4dc9-98df-640bae800fd1sanoj_avhttp://social.msdn.microsoft.com/Profile/en-US/?user=sanoj_avTemporary Tables inside a procedure<p><br/>Why this is not working in SQL Server?</p> <div style="color:black;background-color:white"> <pre><span style="color:blue">CReate</span> <span style="color:blue">Proc</span> TestProc <span style="color:blue">as</span> <span style="color:blue">Begin</span> <span style="color:blue">Select</span> * <span style="color:blue">into</span> #abc <span style="color:blue">from</span> sys.tables <span style="color:blue">Drop</span> <span style="color:blue">table</span> #abc <span style="color:blue">Select</span> * <span style="color:blue">into</span> #abc <span style="color:blue">from</span> sys.objects <span style="color:blue">End</span> </pre> </div> <p><br/><br/><br/>I am converting some procs from Sybase to SQL server 2005 and this works fine in Sybase .Is there any work around other than giving different name to temp tables each time I use it? I dont want to use dyanamic SQL.</p>Wed, 25 Nov 2009 05:18:46 Z2009-11-25T05:57:55Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/48febb70-51d3-4bbd-b2a1-746e3563b292http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/48febb70-51d3-4bbd-b2a1-746e3563b292John_Konghttp://social.msdn.microsoft.com/Profile/en-US/?user=John_Kongvariable in Order By clause will affect output<p>Please refer to following TSQL scripts:<br/><br/>-----------------------------------------------<br/>create table #test(<br/> select_value nvarchar(100),<br/> sort_order_1 int,<br/> sort_order_2 int,<br/>)</p> <p>insert into #test values('field1_value1', 1, 2)<br/>insert into #test values('filed1_value2', 2, 1)</p> <p>declare @result nvarchar(max) = ''<br/>declare @parameter int = 1</p> <p>select @result = @result + ' ' + select_value<br/>from #test<br/><span style="text-decoration:underline">order by (case @parameter when 1 then sort_order_1 else sort_order_2 end)</span></p> <p>select @result</p> <p>drop table #test<br/>-----------------------------------------------<br/><br/>Replace @parameter with constant 1 and run again, you will find the value of @result is different.<br/>Any idea how it happens?<br/>BTW, I tested in SQL 2000 (8.00.2040 SP4 Standard Edition) &amp; SQL 2008 (10.0.1600.22 RTM Standard Edition (64-bit))</p>Wed, 25 Nov 2009 04:55:46 Z2009-11-25T05:50:05Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/e4cd75f0-56c0-4aac-8b4b-a730dd58e1c3http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/e4cd75f0-56c0-4aac-8b4b-a730dd58e1c3new to sql serverhttp://social.msdn.microsoft.com/Profile/en-US/?user=new%20to%20sql%20serverWhat is the best way to design a table for keeping a schedule?I want to create a table or tables that will be used to store appointments for multiple people.  This is similar to what a business would use to schedule for four people and to manage their appointments.  There are a few things that struggle with as far as conceptualization of this.   <div><br/></div> <div>One of them is potential of the days in the future.  I'm referring to the way a calender works and how there are different number of days in different months.  It is the variation in the number of days that would make it difficult. Similarly, if someone wants to schedule an appointment in the following year.  a particular date doesn't fall on the same day each year.  For example today is 11/24/2009 which is a Tuesday.  Nest year, 11/24/2010 is a Wednesday. I think this pattern is consistent, that it adds one day of the week each year for subsequent year.  But there are also leap years to consider which adds a day in February.  This is every year that the last two digits are evenly divisible by four.</div> <div><br/></div> <div>I originally was trying to think of how to make the database for the calender dynamic, as far as adding a new row for each day as needed.  Similar to a table that would hold employees adds a new row for each new employee inserted.  But this doesn't seem possible given the way the calender works.  So then I thought I would have to make a table or tables that are preformatted with the dates and days of the week for that year for each of the years that I want included in the db.  That way it is structured to deal with the anomalies of the subsequent years.  It would make sense to design it out a few years.</div> <div><br/></div> <div>The other thing that I was struggling with is the complexity that comes with having a schedule for multiple people.  This is coupled with having 2 or 4 time slots per hour for each work day.  This could end up being (with a 1 hour lunch break) 14 or 28 time slots a day for one person, depending on if they are in 30 minute or 15 minute intervals.  If there are 4 people on the schedule then this turns into 56 or 112 slots per day for the group of people.</div> <div><br/></div> <div>Can someone please help me understand how to deal with this.  Am I on the right track?  Any advice would be greatly appreciated.</div>Tue, 24 Nov 2009 11:55:05 Z2009-11-25T05:21:04Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/c164a3cf-ce80-44b2-b88c-28834e10eeefhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/c164a3cf-ce80-44b2-b88c-28834e10eeefSuperdechttp://social.msdn.microsoft.com/Profile/en-US/?user=SuperdecString pattern - TSQL 2005Hi, how to print below string with PROPER text ALIGNMENT using simple T-SQL 2005? Thanks.<br/>                1<br/>               22<br/>              333<br/>             4444<br/>            55555<br/>           666666<br/>          7777777<br/>         88888888Wed, 25 Nov 2009 04:57:29 Z2009-11-25T05:08:52Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/0bfcb7db-c532-4e42-8a6a-acedda773fc0http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/0bfcb7db-c532-4e42-8a6a-acedda773fc0gomshttp://social.msdn.microsoft.com/Profile/en-US/?user=gomsLogin via Sprocwhen i tried to execute the login as Sproc<br/>it gives the following error<br/><br/><span style="font-size:xx-small"> <p>Msg 102, Level 15, State 1, Procedure test_user, Line 5</p> <p>Incorrect syntax near '@login'.</p> <p>Msg 319, Level 15, State 1, Procedure test_user, Line 5</p> <p>Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.<br/>========================================================<br/><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p>create</p> </span></span> <p> </p> <p><span style="font-size:x-small;color:#0000ff">PROCEDURE</span><span style="font-size:x-small"> [dbo]</span><span style="font-size:x-small;color:#808080">.</span><span style="font-size:x-small">[test_user] </span><span style="font-size:x-small;color:#808080">(</span><span style="font-size:x-small">@login </span><span style="font-size:x-small;color:#0000ff">varchar</span><span style="font-size:x-small;color:#808080">(</span><span style="font-size:x-small">100</span><span style="font-size:x-small;color:#808080">),</span><span style="font-size:x-small">@pwd </span><span style="font-size:x-small;color:#0000ff">varchar</span><span style="font-size:x-small;color:#808080">(</span><span style="font-size:x-small">100</span><span style="font-size:x-small;color:#808080">))</span></p> <span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p>AS</p> </span></span> <p> </p> <span style="font-size:x-small;color:#008000"> <p>--DECLARE @sql nvarchar(150)</p> </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p>BEGIN</p> </span></span> <p> </p> <span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p>CREATE</p> </span></span> <p><span style="font-size:x-small;color:#0000ff">LOGIN</span><span style="font-size:x-small"> @login </span><span style="font-size:x-small;color:#0000ff">with</span><span style="font-size:x-small"> password</span><span style="font-size:x-small;color:#808080">=</span><span style="font-size:x-small">@pwd </span><span style="font-size:x-small;color:#808080">,</span><span style="font-size:x-small"> DEFAULT_DATABASE</span><span style="font-size:x-small;color:#808080">=</span><span style="font-size:x-small">[sample]</span><span style="font-size:x-small;color:#808080">,</span><span style="font-size:x-small"> <span style="font-size:x-small"> <p>DEFAULT_LANGUAGE</p> </span></span> <p> </p> <p><span style="font-size:x-small;color:#808080">=</span><span style="font-size:x-small">[english]</span><span style="font-size:x-small;color:#808080">;</span><span style="font-size:x-small"> </span></p> <span style="font-size:x-small;color:#0000ff"> <p>END</p> <span style="font-size:x-small;color:#0000ff"> <p> </p> </span></span> <p><br/></p> </p> </p> </span>Tue, 24 Nov 2009 10:53:20 Z2009-11-25T05:00:43Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/7475110e-d56d-4b91-9a59-f95206c2dec1http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/7475110e-d56d-4b91-9a59-f95206c2dec1Yo__http://social.msdn.microsoft.com/Profile/en-US/?user=Yo__How to import data from Access2007 to SQL 2005<p align=left>Can anyone help me out with importing data from access2007 to sql 2005???</p> <p align=left> </p> <p align=left> </p> <p align=left>Thanks! I appreciate it!</p>Tue, 29 Jan 2008 19:53:21 Z2009-11-25T03:57:19Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b5059d65-cee7-4f03-b44d-b97a61dedd43http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b5059d65-cee7-4f03-b44d-b97a61dedd43DoorKnobshttp://social.msdn.microsoft.com/Profile/en-US/?user=DoorKnobsmonth/year datetime comparisons<p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">I have to get just the month and year from a datetime field and do comparisons.</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">It doesn’t seem like this works 100%. There are always records that slip through the cracks</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">I’ve tried this</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">when</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">cast</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">month</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;font-family:'Courier New'">due_date_next_payment<span style="color:gray">)</span> <span style="color:blue">as</span> <span style="color:blue">varchar</span><span style="color:gray">(</span>2<span style="color:gray">))</span> <span style="color:gray">+</span> <span style="color:red">'/'</span> <span style="color:gray">+</span> <span style="color:fuchsia">cast</span><span style="color:gray">(</span><span style="color:fuchsia">year</span><span style="color:gray">(</span>due_date_next_payment<span style="color:gray">)</span> <span style="color:blue">as</span> <span style="color:blue">varchar</span><span style="color:gray">(</span>4<span style="color:gray">))<span style="">    </span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style=""> </span><span style="color:gray">&gt;</span> <span style="color:gray">=</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">cast</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">month</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">DATEADD</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">month</span><span style="font-size:10pt;font-family:'Courier New'"> <span style="color:gray">,</span>1<span style="color:gray">,</span> <span style="color:fuchsia">GETDATE</span><span style="color:gray">()))</span> <span style="color:blue">as</span> <span style="color:blue">varchar</span><span style="color:gray">(</span>2<span style="color:gray">))</span> <span style="color:gray">+</span> <span style="color:red">'/'</span> <span style="color:gray">+</span> <span style="color:fuchsia">cast</span><span style="color:gray">(</span><span style="color:fuchsia">year</span><span style="color:gray">(</span><span style="color:fuchsia">DATEADD</span><span style="color:gray">(</span><span style="color:fuchsia">month</span> <span style="color:gray">,</span>1<span style="color:gray">,</span> <span style="color:fuchsia">GETDATE</span><span style="color:gray">()))</span> <span style="color:blue">as</span> <span style="color:blue">varchar</span><span style="color:gray">(</span>4<span style="color:gray">))</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">then etc..</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">and <span style=""> </span>this</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">when</span><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">DATENAME</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">month</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">,</span><span style="font-size:10pt;font-family:'Courier New'">due_date_next_payment<span style="color:gray">)</span> <span style="color:gray">+</span> <span style="color:red">' '</span> <span style="color:gray">+</span> <span style="color:fuchsia">DATENAME</span><span style="color:gray">(</span><span style="color:fuchsia">year</span><span style="color:gray">,</span> cs_loan_information<span style="color:gray">.</span>due_date_next_payment<span style="color:gray">)</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style=""> </span><span style="color:gray">&gt;=</span> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">datename</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:fuchsia;font-family:'Courier New'">month</span><span style="font-size:10pt;color:gray;font-family:'Courier New'">,</span><span style="font-size:10pt;font-family:'Courier New'"> <span style="color:fuchsia">dateadd</span><span style="color:gray">(</span><span style="color:fuchsia">month</span> <span style="color:gray">,</span>1<span style="color:gray">,</span> <span style="color:fuchsia">Getdate</span><span style="color:gray">()))+</span> <span style="color:red">' '</span><span style="color:gray">+</span> <span style="color:fuchsia">datename</span><span style="color:gray">(</span><span style="color:fuchsia">year</span><span style="color:gray">,</span> <span style="color:fuchsia">dateadd</span><span style="color:gray">(</span><span style="color:fuchsia">month</span> <span style="color:gray">,</span>1<span style="color:gray">,</span> <span style="color:fuchsia">Getdate</span><span style="color:gray">()))</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">then</span><span style="font-size:10pt;font-family:'Courier New'"> 0</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"><span style=""> </span>Any suggestions?</span></span></p>Tue, 24 Nov 2009 20:28:46 Z2009-11-25T04:18:06Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/6800b875-bad1-4d22-a1ce-5334e2512618http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/6800b875-bad1-4d22-a1ce-5334e2512618rupertslandhttp://social.msdn.microsoft.com/Profile/en-US/?user=rupertslandException Error 5123 - Unable to attach database<p align=left>Hello.</p> <p align=left> </p> <p align=left>Our GIS group is working with SQL Server 2005 in conjunction with ESRI's ArcGIS software. SQL Server is being used as a test bed until we formally migrate our existing Microsoft Access mdb's over. So, at this time we are conducting various tests and have the freedom to &quot;blow up&quot; our databases.</p> <p align=left> </p> <p align=left>We are currently testing ArcSDE Workgroup edition, which will only work with SQL Server Express (SSE). SSE will let us create spatial joins and relationships, while maintaining data integrity. So one possible workaround was to detach the database from SQL Server 2005 and then attach it to SSE. Unfortunately, we ran into a problem in re-attaching the database back to SQL Server 2005.</p> <p align=left> </p> <p align=left>The exact steps are as follows:</p> <ol> <li> <div align=left>Back up database using back up tool in SQL Server 2005.</div> <li> <div align=left>Detached database from SQL Server 2005.</div> <li> <div align=left>Attached database to SQL Server Express (hereafter SSE).</div> <li> <div align=left>Created a table in database while it was attached to SSE.</div> <li> <div align=left>Detached database from SSE.</div> <li> <div align=left><font color="#ff0000">Attempt to attach database to SQL Server 2005 failed</font></div> <li> <div align=left><font color="#000000">Successfully restored database in SQL Server 2005 using Restore tool.</font></div></li></ol> <p align=left><font color="#000000">Step number 6 above failed and generated the following error:</font></p> <p align=left> </p> <p align=left> </p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>TITLE: Microsoft SQL Server Management Studio<br>------------------------------</p> <p align=left>Failed to retrieve data for this request. (Microsoft.SqlServer.SmoEnum)</p> <p align=left>For help, click: <a title="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;LinkId=20476" href="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;LinkId=20476">http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;LinkId=20476</a></p> <p align=left>------------------------------<br>ADDITIONAL INFORMATION:</p> <p align=left>An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)</p> <p align=left>------------------------------</p> <p align=left>CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\LNRIS_SQL_Test.mdf'. (Microsoft SQL Server, Error: 5123)</p> <p align=left>For help, click: <a title="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;ProdVer=09.00.1399&amp;EvtSrc=MSSQLServer&amp;EvtID=5123&amp;LinkId=20476" href="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;ProdVer=09.00.1399&amp;EvtSrc=MSSQLServer&amp;EvtID=5123&amp;LinkId=20476">http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;ProdVer=09.00.1399&amp;EvtSrc=MSSQLServer&amp;EvtID=5123&amp;LinkId=20476</a></p></blockquote> <p align=left><br></p> <p align=left> </p> <p align=left>Questions:</p> <ul> <li> <div align=left>Why did I get this error?</div> <li> <div align=left>Can incorrect or missing permission or ownership settings generate this kind of error?</div> <li> <div align=left>When should one detach/attach a database?</div></li></ul> <p align=left> </p> <p align=left>I welcome your assistance and suggestions.</p>Tue, 29 Jan 2008 20:52:30 Z2009-11-25T03:06:40Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9a3275c1-a9f4-4938-b375-7d91d53fe540http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9a3275c1-a9f4-4938-b375-7d91d53fe540dhariwal57http://social.msdn.microsoft.com/Profile/en-US/?user=dhariwal57fastest way to copy huge dataHi,<br/>Probably this question has been asked many time before. Reading up on the documentation and comments from people I find it difficuilt to conclude which way is the fastest to copy data within sql server. Below is the scenario i want to deal with, your input will be highly appreciated:<br/><br/>I want to copy huge chunk of data (around 60-70 GB of data) in one go. Data source as well as destination is sql server. Both source and destination are on same sql server instance (so network or linked servers). I will be copying data from many tables in to the same table structure in destination database e.g. sourcedb..table1 into destinationdb..table1, sourcedb..table2 into destinationdb..table2 etc. It will be incremental insert also going forward (so cant use SELECT ... INTO ... FROM). Most of the tables contain over and around 50 million records. Dont have DTS or SSIS, or I have to do it through .Net or t-sql.<br/><br/>I thought about following:<br/>BCP: fast but I am concerned about time, coz one have to take the data out and then bring it back in, it can effect time as well as space issue<br/>.NET SqlBulkCopy: While reading up on SqlBulkCopy I found that using INSERT....SELECT statement is faster if the source and destination both reside on the same sql server<br/>INSERT....SELECT : Can cause timeout issues and I dont know if it will be quite fast<br/><br/>Basically I want to avoid timeout issues as well as want to make sure that data copies as fast as possible (I dont have access to DTS or SSIS).<br/><br/>Any suggestions based on you experience.<br/><br/>Thanks<br/>Khalid<br/>Tue, 24 Nov 2009 15:26:23 Z2009-11-25T02:33:56Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/dd02b722-a760-4b2e-be87-c24b20b8341chttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/dd02b722-a760-4b2e-be87-c24b20b8341cchuckdawithttp://social.msdn.microsoft.com/Profile/en-US/?user=chuckdawitI was givin a quiz with 10 questionsHi,<br/> I was given a quiz with 10 questions to answer and I was wondering if anyone can help. I will post the code to generate the tables including the questions below.<br/> <br/> <br/> /*<br/> Test item #1: <br/> Return the First Name, Last Name, Product Name, and Sale Price for all products sold in the month of October 2005.<br/> <br/> Test item #2: <br/> Return the list of Customer that have made no purchases.<br/> <br/> Test item #3:<br/> Return the First Name, Last Name, Sale Price, Recommended Sale Price, and the difference between the Sale Price <br/> and Recommended Sale Price for all Sales. <br/> The difference must be returned as a positive number.<br/> <br/> Test item #4: <br/> Return the average Sale Price by Product Category.<br/> <br/> Test item #5: <br/> Add the following Customer and Sale information to the database.<br/> FirstName: Chris<br/> LastName: Kringle<br/> City: Henryville<br/> State: IN<br/> Zip: 47126<br/> ProductID: 3<br/> SalePrice: 205<br/> SaleDate: 12/31/2005<br/> <br/> Test item #6:<br/> Delete the customer(s) from the database who are from the state of Maine ('ME').<br/> <br/> Test item #7:<br/> Return the Product Category and the average Sale Price for those customers who have purchased two or more products.<br/> <br/> Test item #8: <br/> Update the Sale Price to the Recommended Sale Price of those Sales occurring between 6/10/2005 and 6/20/2005.<br/> <br/> Test item #9:<br/> Number of Sales by Product Category where the average Recommended Price is 10 or more dollars greater than the average Sale Price.<br/> <br/> Test item #10: <br/> Return the Sale Date, Sale Price, and the running total for all sales orders by Sale Date.<br/> */<br/> --CREATE DATABASE SqlTest<br/> --GO<br/> <br/> USE SqlTest<br/> GO<br/> <br/> <br/> IF OBJECT_ID('Sales') &gt; 0<br/>     DROP TABLE Sales<br/> <br/> GO<br/> <br/> IF OBJECT_ID('Customers') &gt; 0<br/>     DROP TABLE Customers<br/> <br/> GO<br/> <br/> IF OBJECT_ID('Products') &gt; 0<br/>     DROP TABLE Products<br/> <br/> GO<br/> CREATE TABLE Customers (<br/>     CustomerID INT IDENTITY PRIMARY KEY,<br/>     FirstName VARCHAR(50),<br/>     LastName VARCHAR(50),<br/>     City VARCHAR(50),<br/>     State CHAR(2),<br/>     Zip VARCHAR(10)<br/> )<br/> <br/> GO<br/> <br/> GRANT SELECT ON Customers TO public<br/> GO<br/> <br/> CREATE TABLE Products<br/> (<br/> ProductID TINYINT IDENTITY PRIMARY KEY,<br/> ProductName VARCHAR(20),<br/> RecommendedPrice MONEY,<br/> Category VARCHAR(10)<br/> )<br/> <br/> GO<br/> <br/> GRANT SELECT ON Products TO public<br/> GO<br/> <br/> CREATE TABLE Sales<br/> <br/> (<br/> SaleID INT IDENTITY PRIMARY KEY,<br/> ProductID TINYINT NOT NULL REFERENCES Products(ProductID),<br/> CustomerID INT NOT NULL REFERENCES Customers(CustomerID),<br/> SalePrice MONEY NOT NULL,<br/> SaleDate SMALLDATETIME NOT NULL<br/> )<br/> <br/> GO<br/> <br/> GRANT SELECT ON Sales TO public<br/> GO<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category)<br/> <br/> VALUES('DVD',105,'LivingRoom')<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category) VALUES('Microwave',98,'Kitchen')<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category)<br/> <br/> VALUES('Monitor',200,'Office')<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category)<br/> <br/> VALUES('Speakers',85,'Office')<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category) VALUES('Refrigerator',900,'Kitchen')<br/> <br/> INSERT INTO Products(ProductName, RecommendedPrice, Category)<br/> <br/> VALUES('VCR',165,'LivingRoom')<br/> INSERT INTO Products(ProductName, RecommendedPrice, Category) VALUES('CoffeePot',35,'Kitchen')<br/> <br/> GO<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('John','Miller','Asbury','NY','23433')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Fred','Hammill','Basham','AK','85675')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Stan','Mellish','Callahan','WY','38556')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Adrian','Caparzo','Denver','CO','12377')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Mike','Horvath','Easton','IN','47130')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Irwin','Wade','Frankfurt','KY','45902')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('George','Marshall','Gallipoli','ND','34908')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Frank','Costello','Honolulu','HI','23905')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Billy','Costigan','Immice','SC','75389')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Shelly','Sipes','Lights','AZ','35263')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Chirsty','Melton','Spade','CA','97505')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Amanda','Owens','Flask','CN','50386')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Brittany','Smits','Bourbon','KY','24207')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Kristy','Bryant','Tarp','FL','58960')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Kelly','Street','TableTop','ID','57732')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Tricia','Hill','Camera','ME','46738')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Holly','Raines','Compact','MS','35735')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Natalie','Woods','Woods','IN','87219')<br/> <br/> INSERT INTO Customers(FirstName, LastName, City, State, Zip) VALUES('Wendy','Hilton','Action','KY','47093')<br/> <br/> GO<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,1,130,'2/6/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,2,97,'1/7/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,3,200,'8/8/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(4,4,80,'4/9/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(5,5,899,'10/10/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(6,6,150,'10/11/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,7,209,'12/12/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(4,8,90,'5/13/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(6,9,130,'6/14/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,14,85,'6/19/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,15,240,'9/20/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,16,99,'7/21/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,17,87,'3/22/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,18,99,'1/23/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(6,19,150,'3/24/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(5,5,900,'3/10/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(4,6,86,'8/11/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,7,88,'8/12/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,8,198,'12/13/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,9,150,'5/14/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(6,14,99,'7/19/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(6,15,104,'9/20/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,16,270,'2/21/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(4,17,90,'7/22/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,1,130,'3/6/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,2,102,'4/7/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(1,3,114,'11/8/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(5,4,1000,'5/9/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(5,5,1100,'10/10/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,6,285,'6/11/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(2,7,87,'10/12/2005')<br/> <br/> INSERT INTO Sales(ProductID, CustomerID, SalePrice, SaleDate) VALUES(3,8,300,'7/13/2005')<br/> <br/> GO<hr class="sig">chuckdawitTue, 24 Nov 2009 22:35:15 Z2009-11-25T06:38:35Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/d904883f-423f-4120-b49e-1bb7fdfc3b69http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/d904883f-423f-4120-b49e-1bb7fdfc3b69SqlAgehttp://social.msdn.microsoft.com/Profile/en-US/?user=SqlAgeSelecting a float ValueHello,<br/><br/>I have a table with column A, data type float. There are some values in that, say 20.00 or 302.90<br/>When i do this query<br/><br/>Select * from tableA<br/>where ColumnA=20.00 <br/><br/>not getting any results , even value is there.... <br/>I did try like <br/>Select * from tableA<br/>where ColumnA='20.00'<br/><br/>Still No Results<br/><br/>I converted and tried again<br/><br/>Select * from TABLEA<br/>where Cast( ColumnA as varchar(50)='20.00'<br/><br/>Still no luck:(<br/><br/>What I am missing? any help!!Tue, 24 Nov 2009 20:08:56 Z2009-11-25T01:12:45Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/53869082-8376-4182-8cc0-143774c8cfc3http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/53869082-8376-4182-8cc0-143774c8cfc3SQLUSAhttp://social.msdn.microsoft.com/Profile/en-US/?user=SQLUSAT-SQL Solutions: Most Intuitive vs. Best Performing<p>I have noticed several times in this forum that a perfectly good T-SQL solution came under attack because was not the best performing solution. <br/><br/>Yes, performance is important, but that is not the only factor in the life of a DBA or database developer. Foremost they have to be able to deliver solutions, frequently &quot;under the gun&quot;.<br/><br/>Therefore, my view is, that a good DBA/developer has to have a <span style="line-height:115%;font-family:'Verdana','sans-serif';color:black;font-size:8pt">repertoire</span> of intuitive, easy-to-remember and reliable solutions, because SS performance not always important, but human performance is.  Also a good DBA/developer has to know performance enhancing techniques, because performance issues are important as well in operating a SQL Server system.<br/><br/>What do you think?<br/></p><hr class="sig">Kalman Toth, SQL Server &amp; BI Training, SSAS, SSIS, SSRS; <a href="http://sqlusa.com/">http://www.SQLUSA.com</a>Tue, 10 Nov 2009 22:27:49 Z2009-11-25T01:00:45Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a0abe46b-6dba-4e38-8d3b-8342a6757ea7http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a0abe46b-6dba-4e38-8d3b-8342a6757ea7sseerraajjhttp://social.msdn.microsoft.com/Profile/en-US/?user=sseerraajjusing where clause with Freetexttable, partition table<p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style="font-family:Calibri;font-size:small">Hello..</span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style="font-size:small"><span style="font-family:Calibri">I am using SQL Server 2005 and have a table with millions rows; this table contains data for more than one project (defined by 'projectID' tinyint<span style=""> column).</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style="font-size:small"><span style="font-family:Calibri"><span style=""><span style=""> </span>Also these data in multiple languages (defined by 'lang' </span>tinyint<span style=""> column).</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">I am using full text search (FreetextTable) for searching; the results are nice when searching in the whole of table.</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">But when I want to specify search in one project or one language, the query will be too slow and returns wrong results.</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-family:Calibri;font-size:small">That is because I use (where clause) with (freetexttable) and at the same time, I am using </span></span><span style="line-height:115%;font-family:'Verdana','sans-serif';font-size:10pt">the (top_n_by_rank) parameter of the </span><span style=""><span style="font-size:small"><span style="font-family:Calibri">(freetexttable).</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-family:Calibri;font-size:small">My problem </span></span><span style="line-height:115%;font-family:'Segoe UI','sans-serif';color:black;font-size:10pt">discussed</span><span style=""><span style="font-size:small"><span style="font-family:Calibri"> in these links:</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><a href="http://www.developmentnow.com/g/104_2004_11_0_0_399982/Containstable-problem.htm"><span style="font-family:Calibri;color:#800080;font-size:small">http://www.developmentnow.com/g/104_2004_11_0_0_399982/Containstable-problem.htm</span></a></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><a href="http://www.sql-server-performance.com/articles/all/tb_search_optimization_p1.aspx"><span style="font-family:Calibri;color:#800080;font-size:small">http://www.sql-server-performance.com/articles/all/tb_search_optimization_p1.aspx</span></a></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><a href="http://www.sql-server-performance.com/articles/all/tb_search_optimization_p2.aspx"><span style="font-family:Calibri;color:#800080;font-size:small">http://www.sql-server-performance.com/articles/all/tb_search_optimization_p2.aspx</span></a></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">but that solution is not convince.</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">So I hope to find the best solution for this! I am thinking for partitioning <span style=""> </span>the table and searching in specific petitions.</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">I overhear that ORACLE DB supporting virtual partitioning for the tables to solve such a problem.</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">Any help please..</span></span></span></p> <p class=MsoNormal style="text-align:left;margin:0in 0in 10pt;unicode-bidi:embed;direction:ltr"><span style=""><span style="font-size:small"><span style="font-family:Calibri">Thanks.</span></span></span></p>Mon, 23 Nov 2009 14:37:08 Z2009-11-24T23:29:07Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/565e7c2e-f2b6-49fa-8f58-b355ace8d69ehttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/565e7c2e-f2b6-49fa-8f58-b355ace8d69eAndrew Goodnighthttp://social.msdn.microsoft.com/Profile/en-US/?user=Andrew%20Goodnighthow to implement procedure for end-day batch processing ?<p>i would like to roughly arrange the code as following</p> <pre lang=x-sql>create proc end_day_proc as begin --begin of end-day processing --some sql statements ... exec sub_task1 ... exec sub_task2 ... exec sub_task3 ... exec log end --end of end-day processing</pre> each task reads and writes to DB,and i hope all the things done in ONE transaction,which means if an error occur in any of the sub_tasks,the transaction should be rollbacked.<br/>The log procedure invoked in the end_day_proc writes the result-success or failure,to DB<br/>In the other hand,if an error occurs in any sub_task,the error should be logged(by calling a log procedure in the sub_task).<br/><br/>Now, if  an error in a sub_task is logged in DB,while in outer proc-end_day_proc, an subsequential action rollbacks the transaction.<br/>how can the logging action in the subtask avoid be rollback?<br/><br/>Any suggestion will be appreciated.Thanks. <br/><br/><br/>Andrew<hr class="sig">Interested in BI related technologiesTue, 24 Nov 2009 02:56:16 Z2009-11-24T23:23:54Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/eee6628c-6032-409c-ae80-f8b9dac7b28bhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/eee6628c-6032-409c-ae80-f8b9dac7b28bkvanithuraihttp://social.msdn.microsoft.com/Profile/en-US/?user=kvanithuraihelp with joining two query<p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">I have two sets of query </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">First is as follows</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">SELECT</span><span style="font-size:10pt;font-family:'Courier New'"> FULLKEY<span style="color:gray">,</span> LOAD_ID<span style="color:gray">,</span> PREMISE_NO<span style="color:gray">,</span> RISK_NO<span style="color:gray">,</span> PACKAGE_CATEGORY<span style="color:gray">,</span>RowNum<span style="color:gray">=</span><span style="color:fuchsia">ROW_NUMBER</span><span style="color:gray">()</span><span style="color:blue">OVER</span><span style="color:gray">(</span><span style="color:blue">PARTITION</span> <span style="color:blue">BY</span> FullKey <span style="color:blue">ORDER</span> <span style="color:blue">BY</span> PREMISE_NO<span style="color:gray">)</span> <span style="color:blue">FROM</span><span style="">  </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="">      </span>COVERAGE <span style="color:blue">WHERE</span> LOAD_ID <span style="color:gray">=</span> <span style="color:red">'20090630'</span> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="">      </span><span style="color:gray">AND</span> Status_sw <span style="color:gray">=</span> <span style="color:red">'L'</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="">      </span><span style="color:gray">AND</span> RISK_NO <span style="color:gray">=</span> 1 <span style="color:gray">AND</span> PACKAGE_CATEGORY <span style="color:gray">IN</span><span style="color:blue"> </span><span style="color:gray">(</span><span style="color:red">'P'</span><span style="color:gray">,</span> <span style="color:red">'S'</span><span style="color:gray">)</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="">      </span><span style="color:gray">AND</span> Status_sw <span style="color:gray">=</span> <span style="color:red">'L'</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">            </span><span style="">      </span><span style="color:gray">And</span> Form_type <span style="color:gray">in</span><span style="color:blue"> </span><span style="color:gray">(</span><span style="color:red">'A'</span><span style="color:gray">,</span><span style="color:red">'B'</span><span style="color:gray">,</span><span style="color:red">'C'</span><span style="color:gray">,</span><span style="color:red">'D'</span><span style="color:gray">,</span><span style="color:red">'E'</span><span style="color:gray">,</span><span style="color:red">'F'</span><span style="color:gray">,</span><span style="color:red">'G'</span><span style="color:gray">)</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">Second is as follows</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">select</span><span style="font-size:10pt;font-family:'Courier New'"> FULLKEY<span style="color:gray">,</span> LOAD_ID<span style="color:gray">,</span> POLICY_NO<span style="color:gray">,</span> STATUS_SW<span style="color:gray">,</span> AMEND_DATE<span style="color:gray">,</span> PREMISE_NO<span style="color:gray">,</span> STREET_NO<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">      </span><span style="">      </span>STREET_NAME<span style="color:gray">,</span> TERRITORY<span style="color:gray">,</span> CITY<span style="color:gray">,</span> PROVINCE<span style="color:gray">,</span> POSTAL_CODE<span style="color:gray">,</span> RATING_TERRITORY<span style="color:gray">,</span> PCODE_PROTECTION<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">      </span><span style="">      </span>FIREHALL_LOCATION<span style="color:gray">,</span> RISK_NO<span style="color:gray">,</span> FORM_CODE<span style="color:gray">,</span> PACKAGE_CATEGORY<span style="color:gray">,</span> PACKAGE_TYPE<span style="color:gray">,</span> FLT_COUNT<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">      </span><span style="">      </span>RISK_START_DATE<span style="color:gray">,</span> PREFERRED_RATE<span style="color:gray">,</span> OCCUPANCY<span style="color:gray">,</span> PROTECTION<span style="color:gray">,</span> SMOKE_DETECTOR<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">      </span><span style="">      </span>RESIDENCE_TYPE<span style="color:gray">,</span> YEAR_BUILT<span style="color:gray">,</span> STRUCTURE<span style="color:gray">,</span>CONSTRUCTION_CODE<span style="color:gray">,</span> TOTAL_NET<span style="color:gray">,</span> ROOF_CONST<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style="">      </span><span style="">      </span>HEAT_PRIMARY<span style="color:gray">,</span> HEAT_SECONDARY<span style="color:gray">,</span>RowNum <span style="color:blue">from</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:blue;font-family:'Courier New'">select</span><span style="font-size:10pt;font-family:'Courier New'"> cte<span style="color:gray">.</span>fullkey<span style="color:gray">,</span>cte<span style="color:gray">.</span>premise_no<span style="color:gray">,</span>cte<span style="color:gray">.</span>load_id<span style="color:gray">,</span>cte<span style="color:gray">.</span>policy_no<span style="color:gray">,</span>cte<span style="color:gray">.</span>status_sw<span style="color:gray">,</span>cte<span style="color:gray">.</span>amend_date<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">cte<span style="color:gray">.</span>street_no<span style="color:gray">,</span>cte<span style="color:gray">.</span>street_name<span style="color:gray">,</span>cte<span style="color:gray">.</span>territory<span style="color:gray">,</span>cte<span style="color:gray">.</span>city<span style="color:gray">,</span>cte<span style="color:gray">.</span>province<span style="color:gray">,</span>cte<span style="color:gray">.</span>postal_code<span style="color:gray">,</span>cte<span style="color:gray">.</span>rating_territory<span style="color:gray">,</span>cte<span style="color:gray">.</span>pcode_protection<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">cte<span style="color:gray">.</span>firehall_location<span style="color:gray">,</span>cte<span style="color:gray">.</span>RISK_NO<span style="color:gray">,</span>cte<span style="color:gray">.</span>FORM_CODE<span style="color:gray">,</span>cte<span style="color:gray">.</span>package_category<span style="color:gray">,</span>cte<span style="color:gray">.</span>PACKAGE_TYPE<span style="color:gray">,</span>cte<span style="color:gray">.</span>FLT_COUNT<span style="color:gray">,</span>cte<span style="color:gray">.</span>RISK_START_DATE<span style="color:gray">,</span>cte<span style="color:gray">.</span>PREFERRED_RATE<span style="color:gray">,</span>cte<span style="color:gray">.</span>OCCUPANCY<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">cte<span style="color:gray">.</span>PROTECTION<span style="color:gray">,</span>cte<span style="color:gray">.</span>SMOKE_DETECTOR<span style="color:gray">,</span>cte<span style="color:gray">.</span>RESIDENCE_TYPE<span style="color:gray">,</span>cte<span style="color:gray">.</span>YEAR_BUILT<span style="color:gray">,</span>cte<span style="color:gray">.</span>STRUCTURE<span style="color:gray">,</span>cte<span style="color:gray">.</span>CONSTRUCTION_CODE<span style="color:gray">,</span>cte<span style="color:gray">.</span>TOTAL_NET<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">cte<span style="color:gray">.</span>ROOF_CONST<span style="color:gray">,</span>cte<span style="color:gray">.</span>HEAT_PRIMARY<span style="color:gray">,</span>cte<span style="color:gray">.</span>HEAT_SECONDARY<span style="color:gray">,</span>cte<span style="color:gray">.</span>RowNum <span style="color:blue">from</span> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">SELECT</span><span style="font-size:10pt;font-family:'Courier New'"> B<span style="color:gray">.</span>fullkey<span style="color:gray">,</span>B<span style="color:gray">.</span>premise_no<span style="color:gray">,</span>B<span style="color:gray">.</span>load_id<span style="color:gray">,left(</span>B<span style="color:gray">.</span>fullkey<span style="color:gray">,</span>10<span style="color:gray">)</span> <span style="color:blue">as</span> policy_no<span style="color:gray">,</span>B<span style="color:gray">.</span>status_sw<span style="color:gray">,</span><span style="color:fuchsia">convert</span><span style="color:blue"> </span><span style="color:gray">(</span><span style="color:blue">varchar</span><span style="color:gray">(</span>8<span style="color:gray">),</span>B<span style="color:gray">.</span>Amend_date<span style="color:gray">,</span>112<span style="color:gray">)</span> <span style="color:blue">as</span> amend_date<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">B<span style="color:gray">.</span>street_no<span style="color:gray">,</span>B<span style="color:gray">.</span>street_name<span style="color:gray">,</span>B<span style="color:gray">.</span>territory<span style="color:gray">,</span>B<span style="color:gray">.</span>city<span style="color:gray">,</span>B<span style="color:gray">.</span>province<span style="color:gray">,</span>B<span style="color:gray">.</span>postal_code<span style="color:gray">,</span>B<span style="color:gray">.</span>rating_territory<span style="color:gray">,</span>B<span style="color:gray">.</span>pcode_protection<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">B<span style="color:gray">.</span>firehall_location<span style="color:gray">,</span>A<span style="color:gray">.</span>RISK_NO<span style="color:gray">,</span>A<span style="color:gray">.</span>FORM_CODE<span style="color:gray">,</span>A<span style="color:gray">.</span>Package_Category<span style="color:gray">,</span>A<span style="color:gray">.</span>PACKAGE_TYPE<span style="color:gray">,</span>A<span style="color:gray">.</span>FLT_COUNT<span style="color:gray">,</span><span style="color:fuchsia">CONVERT</span><span style="color:gray">(</span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span>8<span style="color:gray">),</span>A<span style="color:gray">.</span>RISK_START_DATE<span style="color:gray">,</span>112<span style="color:gray">)</span> <span style="color:blue">AS</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">RISK_START_DATE<span style="color:gray">,</span>A<span style="color:gray">.</span>PREFERRED_RATE<span style="color:gray">,</span>A<span style="color:gray">.</span>OCCUPANCY<span style="color:gray">,</span>A<span style="color:gray">.</span>PROTECTION<span style="color:gray">,</span>A<span style="color:gray">.</span>SMOKE_DETECTOR<span style="color:gray">,</span>A<span style="color:gray">.</span>RESIDENCE_TYPE<span style="color:gray">,</span>A<span style="color:gray">.</span>YEAR_BUILT<span style="color:gray">,</span>A<span style="color:gray">.</span>STRUCTURE<span style="color:gray">,</span>A<span style="color:gray">.</span>CONSTRUCTION_CODE<span style="color:gray">,</span>A<span style="color:gray">.</span>TOTAL_NET<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">A<span style="color:gray">.</span>ROOF_CONST<span style="color:gray">,</span>A<span style="color:gray">.</span>HEAT_PRIMARY<span style="color:gray">,</span>A<span style="color:gray">.</span>HEAT_SECONDARY<span style="color:gray">,</span>RowNum<span style="color:gray">=</span><span style="color:fuchsia">ROW_NUMBER</span><span style="color:gray">()</span><span style="color:blue">OVER</span><span style="color:gray">(</span><span style="color:blue">PARTITION</span> <span style="color:blue">BY</span> A<span style="color:gray">.</span>FullKey <span style="color:blue">ORDER</span> <span style="color:blue">BY</span> B<span style="color:gray">.</span>PREMISE_NO<span style="color:gray">)</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">FROM</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">RISK A <span style="color:gray">INNER</span> <span style="color:gray">JOIN</span> PREMISE B </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">ON</span><span style="font-size:10pt;font-family:'Courier New'"> A<span style="color:gray">.</span>FULLKEY <span style="color:gray">=</span> B<span style="color:gray">.</span>FULLKEY</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">AND</span><span style="font-size:10pt;font-family:'Courier New'"> A<span style="color:gray">.</span>LOAD_ID <span style="color:gray">=</span> <span style="color:red">'20090630'</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">AND</span><span style="font-size:10pt;font-family:'Courier New'"> A<span style="color:gray">.</span>LOAD_ID <span style="color:gray">=</span> B<span style="color:gray">.</span>LOAD_ID</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">AND</span><span style="font-size:10pt;font-family:'Courier New'"> A<span style="color:gray">.</span>RISK_NO <span style="color:gray">=</span> <span style="color:red">'1'</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">AND</span><span style="font-size:10pt;font-family:'Courier New'"> A<span style="color:gray">.</span>PACKAGE_CATEGORY <span style="color:gray">IN</span><span style="color:blue"> </span><span style="color:gray">(</span><span style="color:red">'P'</span><span style="color:gray">,</span><span style="color:red">'S'</span><span style="color:gray">))</span>cte</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"><span style=""> </span><span style="color:gray">right</span> <span style="color:gray">outer</span> <span style="color:gray">JOIN</span> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">(</span><span style="font-size:10pt;color:blue;font-family:'Courier New'">select</span><span style="font-size:10pt;font-family:'Courier New'"> fullkey<span style="color:gray">,</span>premise_no<span style="color:gray">,</span>load_id<span style="color:gray">,left(</span>fullkey<span style="color:gray">,</span>10<span style="color:gray">)</span> <span style="color:blue">as</span> policy_no<span style="color:gray">,</span>status_sw<span style="color:gray">,</span><span style="color:fuchsia">convert</span><span style="color:blue"> </span><span style="color:gray">(</span><span style="color:blue">varchar</span><span style="color:gray">(</span>8<span style="color:gray">),</span>Amend_date<span style="color:gray">,</span>112<span style="color:gray">)</span> <span style="color:blue">as</span> amend_date<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">street_no<span style="color:gray">,</span>street_name<span style="color:gray">,</span>territory<span style="color:gray">,</span>city<span style="color:gray">,</span>province<span style="color:gray">,</span>postal_code<span style="color:gray">,</span>rating_territory<span style="color:gray">,</span>pcode_protection<span style="color:gray">,</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">firehall_location<span style="color:gray">,</span>RowNum<span style="color:gray">=</span><span style="color:fuchsia">ROW_NUMBER</span><span style="color:gray">()</span><span style="color:blue">OVER</span><span style="color:gray">(</span><span style="color:blue">PARTITION</span> <span style="color:blue">BY</span> FullKey <span style="color:blue">ORDER</span> <span style="color:blue">BY</span> PREMISE_NO<span style="color:gray">)</span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">from</span><span style="font-size:10pt;font-family:'Courier New'"> premise <span style="color:blue">where</span> load_id <span style="color:gray">=</span> <span style="color:red">'20090630'</span><span style="color:gray">)</span> X</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:blue;font-family:'Courier New'">on</span><span style="font-size:10pt;font-family:'Courier New'"> cte<span style="color:gray">.</span>fullkey <span style="color:gray">=</span> X<span style="color:gray">.</span>fullkey</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;color:gray;font-family:'Courier New'">and</span><span style="font-size:10pt;font-family:'Courier New'"> cte<span style="color:gray">.</span>RowNum <span style="color:gray">=</span> X<span style="color:gray">.</span>RowNum<span style="color:gray">)</span>common</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'"> </span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">The first query returns 259237 rows</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:10pt;font-family:'Courier New'">Where as the secont query returns 42820 rows</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small;font-family:Times New Roman">Now I want to put a inner join between both of these query and get the 42820 rows.</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="font-size:small"><span style="font-family:Times New Roman"> </span></span></p> <span style="font-size:12pt;font-family:'Times New Roman'">Any suggestion on joining these queries will be appreciated</span>Tue, 24 Nov 2009 21:30:43 Z2009-11-24T22:04:22Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/16e617b2-3322-42f8-930c-ff52df4cea0bhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/16e617b2-3322-42f8-930c-ff52df4cea0bJassim Rahmahttp://social.msdn.microsoft.com/Profile/en-US/?user=Jassim%20Rahmaselect from two tables and add a categoryI have two different tables: clients &amp; companies.<br/><br/>I want to select<br/>client_id and client_name from clients<br/>company_id and company_nam from companies<br/><br/>and i want to add a category column so if the row was from clients tables it should should as L but if it's from companies tables then it should show as C<br/><br/>also client_name &amp; companiy_name must be listed under one column, let's say full_name<br/><br/>can any one help plz...<hr class="sig">Jassim RahmaMon, 23 Nov 2009 21:49:08 Z2009-11-24T22:17:49Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a506eb17-b0a2-4338-ba56-cf8f216beb5ehttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a506eb17-b0a2-4338-ba56-cf8f216beb5eMattChandlerhttp://social.msdn.microsoft.com/Profile/en-US/?user=MattChandler2 versions of case, 1 works, 1 doesn't, why???We have a problem here at work, and trying to figure out the difference.  I have used sample data below...Here's the setup for a sample table to run:<br/><br/> <pre lang=x-sql>if object_id ('tempdb..#animal') is not null drop table #animal if object_id ('tempdb..#tool') is not null drop table #tool create table #animal ( animal_id int, animal_type varchar(25) ) create table #tool ( tool_id int, tool_type varchar(25), tool_animalid int ) insert into #animal (animal_id, animal_type) select 1,'horse' union select 2,'cat' union select 3,'bird' union select 4,'dog' insert into #tool (tool_id, tool_type, tool_animalid) select 1,'leash',4 union select 2,'saddle',1 /* Select * from #animal Select * from #tool */</pre> <p>Now when I run the following 2 statements, 1 works and one doesnt - the problem is with the case - I thought they were essentially the same thing??<br/><br/>This does NOT work (give correct results)</p> <pre lang=x-sql>Select a.animal_type, t.tool_type, case t.tool_type when null then 'No Match' else 'Match' end as IsMatched from #animal as a left join #tool as t on a.animal_id = t.tool_animalid</pre> <br/><br/>this DOES INDEED work:<br/><br/> <pre lang=x-sql>Select a.animal_type, t.tool_type, case when t.tool_type is null then 'No Match' else 'Match' end as IsMatched from #animal as a left join #tool as t on a.animal_id = t.tool_animalid</pre> <br/><br/>Can someone please explain the difference in the 2 versions of the case statement?  Thanks.<br/><br/><br/>Im using SQL Server 2005, Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86)   Dec 17 2008 15:19:45   Copyright (c) 1988-2005 Microsoft Corporation  Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)Tue, 24 Nov 2009 21:21:43 Z2009-11-24T21:38:33Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9b93074c-119a-4bfb-9a3e-9467d106275dhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/9b93074c-119a-4bfb-9a3e-9467d106275dAltawanhttp://social.msdn.microsoft.com/Profile/en-US/?user=Altawannon-ANSI compliant join operatorsI have moved a database from SQL Server 2000 to SQL Server 2005. Currently it is running in SQL 2000 compatiblitiy level (80). I need to switch it to SQL Server 2005 (90) compatibility level. Before I an do that, any code that currently has non_ANSI compliant join operators must be coverted to be ANSI compliant. My problem is that where the optional table has conditions set to it, nothing gets picked up. How can I restrict the predicate on the optional table, but still retrieve data about the main table? <br/><br/>The attached code is an example of code that works using the *= operator but does not work using left outer join.<br/> <pre>SELECT n.ID,n.COMPANY,c.PRODUCT_CODE FROM Name n , Activity c where n.ID *= c.ID and n.ID='ABCDEF' and c.ACTIVITY_TYPE = 'CONTACT' and c.PRODUCT_CODE = 'RECRUIT' SELECT n.ID,n.COMPANY,c.PRODUCT_CODE FROM Name n left outer join Activity c on n.ID = c.ID where n.ID='ABCDEF' and c.ACTIVITY_TYPE = 'CONTACT' and c.PRODUCT_CODE = 'RECRUIT' </pre>Tue, 24 Nov 2009 21:17:03 Z2009-11-24T21:37:35Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/98b2e061-0763-438f-a2e3-5d4e478263a3http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/98b2e061-0763-438f-a2e3-5d4e478263a3lorifajosehttp://social.msdn.microsoft.com/Profile/en-US/?user=lorifajoseProblem With OPENROWSET<span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <p> I'm having a problem with a openrowset statement, in a database some tables return the openrowset and some don't <br/><br/>This Statement Works perfectly<br/><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"></span></span></span></span></p> <p><strong>select <span style="font-size:x-small">b</span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">.*</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">into</span></span><span style="font-size:x-small"> #users </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">from</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">OPENROWSET</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">(</span></span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'SQLOLEDB'</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">,</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'Trusted_Connection=yes;Integrated Security=SSPI;Data Source=MS-BI-02;Initial Catalog=dwhtanque'</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">,</span></span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'Select * from dbo.usuarios'</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">)</span></span></strong><span style="font-size:x-small"><strong> b<br/><br/></strong>This doesn't work<br/><br/><strong><span style="color:#0000ff">select</span><span style="font-size:x-small"> b</span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">.*</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">into</span></span><span style="font-size:x-small"> #users </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">from</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff">OPENROWSET</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">(</span></span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'SQLOLEDB'</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">,</span></span><span style="font-size:x-small"> </span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'Trusted_Connection=yes;Integrated Security=SSPI;Data Source=MS-BI-02;Initial Catalog=dwhtanque'</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">,</span></span><span style="font-size:x-small;color:#ff0000"><span style="font-size:x-small;color:#ff0000">'Select * from dbo.td_cliente</span></span><span style="font-size:x-small;color:#808080"><span style="font-size:x-small;color:#808080">)</span></span><span style="font-size:x-small"> b</span><br/><br/></strong>and return the following error<br/></span></p> <p><strong>OLE DB provider &quot;SQLNCLI&quot; for linked server &quot;(null)&quot; returned message &quot;Deferred prepare could not be completed.&quot;.<br/>Msg 8180, Level 16, State 1, Line 1<br/>Statement(s) could not be prepared.<br/>Msg 208, Level 16, State 1, Line 1<br/>Invalid object name 'dbo.td_cliente_tc'<br/><br/></strong>the database server it's a SQL Server 2005, I'm not using linked servers, both table are in the same database<br/>it's driving me crazy, Any ideas ????</p> </span></span></span></span>Tue, 24 Nov 2009 18:52:36 Z2009-11-24T21:03:36Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/4671b8db-cedd-4059-8761-f50483f09bffhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/4671b8db-cedd-4059-8761-f50483f09bffMrFlinstonehttp://social.msdn.microsoft.com/Profile/en-US/?user=MrFlinstoneRun index fragmentation on all databasesI'm trying to run my index defrag process on all databases, I can easily write the query to pull out fragmentation information, but to run the defragmentation process is rather tricky on all databases, one way is to create a stored procedure on each and every database and run it against all the databases.<br/> <br/> I am trying to wtite the query  below so that it will run on every database using msforeachDB. It keeps failing due to the variables.<br/> <br/> <pre lang=x-sql> SET NOCOUNT ON DECLARE @fragrate float SET @fragrate = 30.0 DECLARE @rbrirate float SET @rbrirate = 50.0 DECLARE @indexinfo TABLE(objectid int, indexid int, partitionnum bigint, frag float) DECLARE @objectid int DECLARE @indexid int DECLARE @partitioncount bigint DECLARE @schemaname nvarchar(130) DECLARE @objectname nvarchar(130) DECLARE @indexname nvarchar(130) DECLARE @partitionnum bigint DECLARE @partitions bigint DECLARE @frag float DECLARE @command nvarchar(4000) INSERT INTO @indexinfo SELECT object_id AS objectid, index_id AS indexid, partition_number AS partitionnum, avg_fragmentation_in_percent AS frag FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') WHERE avg_fragmentation_in_percent &gt; @fragrate AND index_id &gt; 0 DECLARE partitions CURSOR FOR SELECT * FROM @indexinfo OPEN partitions FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag WHILE (@@fetch_status &lt;&gt; -1) BEGIN IF (@@fetch_status &lt;&gt; -2) BEGIN SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name) FROM sys.objects AS o JOIN sys.schemas as s ON s.schema_id = o.schema_id WHERE o.object_id = @objectid SELECT @indexname = QUOTENAME(name) FROM sys.indexes WHERE object_id = @objectid AND index_id = @indexid SELECT @partitioncount = count (*) FROM sys.partitions WHERE object_id = @objectid AND index_id = @indexid -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding. IF @frag &lt; @rbrirate SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE' IF @frag &gt;= @rbrirate SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD' IF @partitioncount &gt; 1 SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10)) EXEC (@command) END FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag END CLOSE partitions DEALLOCATE partitions </pre> <br/> I tried<br/> <br/> <pre lang=x-sql> declare @sql varchar(max) set @sql = ' -- define index defrag rate for this run DECLARE @fragrate float SET @fragrate = 30.0 -- set to do something if defrag rate HIGH DECLARE @rbrirate float SET @rbrirate = 30.0 -- set to rebuild if defrag rate EXTREME -- define table holding index information DECLARE @indexinfo TABLE(objectid int, indexid int, partitionnum bigint, frag float) DECLARE @objectid int DECLARE @indexid int DECLARE @partitioncount bigint DECLARE @schemaname nvarchar(130) DECLARE @objectname nvarchar(130) DECLARE @indexname nvarchar(130) DECLARE @partitionnum bigint DECLARE @partitions bigint DECLARE @frag float DECLARE @command nvarchar(4000) -- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function INSERT INTO @indexinfo SELECT object_id AS objectid, index_id AS indexid, partition_number AS partitionnum, avg_fragmentation_in_percent AS frag FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ''LIMITED'') WHERE avg_fragmentation_in_percent &gt; @fragrate AND index_id &gt; 0 -- Declare the cursor for the list of partitions to be processed. DECLARE partitions CURSOR FOR SELECT * FROM @indexinfo OPEN partitions -- Loop through the partitions. FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag WHILE (@@fetch_status &lt;&gt; -1) BEGIN IF (@@fetch_status &lt;&gt; -2) BEGIN SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name) FROM sys.objects AS o JOIN sys.schemas as s ON s.schema_id = o.schema_id WHERE o.object_id = @objectid SELECT @indexname = QUOTENAME(name) FROM sys.indexes WHERE object_id = @objectid AND index_id = @indexid SELECT @partitioncount = count (*) FROM sys.partitions WHERE object_id = @objectid AND index_id = @indexid -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding. IF @frag &lt; @rbrirate SET @command = N''ALTER INDEX '' + @indexname + N'' ON '' + @schemaname + N''.'' + @objectname + N'' REORGANIZE'' IF @frag &gt;= @rbrirate SET @command = N''ALTER INDEX '' + @indexname + N'' ON '' + @schemaname + N''.'' + @objectname + N'' REBUILD'' IF @partitioncount &gt; 1 SET @command = @command + N'' PARTITION='' + CAST(@partitionnum AS nvarchar(10)) EXEC (@command) PRINT N''Executed (Defrag '' + CAST(@frag AS varchar(12)) + ''): '' + @command END -- Get the next record to process FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag END -- Close and deallocate the cursor. CLOSE partitions DEALLOCATE partitions ' exec sp_MSforeachdb @sql </pre> <br/>Thu, 12 Nov 2009 12:51:10 Z2009-11-24T20:43:55Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/afa42264-2e6b-44b4-a811-920ea8808739http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/afa42264-2e6b-44b4-a811-920ea8808739Abdshallhttp://social.msdn.microsoft.com/Profile/en-US/?user=AbdshallClusterd indexHello<br/><br/>I'm running the following query<br/><br/><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"><span style="font-size:x-small;color:#0000ff"> <div style="color:black;background-color:white"> <pre><span style="color:blue">select</span> id ,participantid ,accountid <span style="color:blue">from</span> ParticipantAccountx </pre> </div> <p> ID is a primary key and there is a clusterd index on it. ParticipantID and AccountID are foreign keys, and there is a nonclustered index on each one of them. <br/><br/>I just reorganized the clustered index, but when I run the query, the optimizer is choosing to do am Index Scan on the nonclustered index on ParticipantID. <br/><br/>Why is that?</p> <hr class=sig> Abdallah, PMP, ITIL, MCTS</span></span></span></span><span style="font-size:x-small"><br/></span>Tue, 24 Nov 2009 18:05:29 Z2009-11-24T20:33:17Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5e09d227-cb53-4040-a669-8edf3c019241http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5e09d227-cb53-4040-a669-8edf3c019241douglasehowellhttp://social.msdn.microsoft.com/Profile/en-US/?user=douglasehowellPIVOT questionI have a simple query that gives me the correct data.....<br/><br/>I just need to &quot;pivot&quot; the data and after reading the MSDN article on PIVOT I am still not having any luck.<br/><br/>The simple query is:<br/><br/>SELECT [Process Data].[_TIMESTAMP], [Process Data].[_NAME], [Process Data].[_VALUE]<br/>FROM [Process Data]<br/>WHERE ((([Process Data].[_NAME]) Like '%TANK30%') AND (([Process Data].[_TIMESTAMP]) Between #11/9/2009# And #11/11/2009#))<br/>ORDER BY [Process Data].[_TIMESTAMP] DESC;<br/><br/>Query yields data like this:<br/><br/> <table border=1 cellspacing=0 bgcolor="#ffffff"> <span style="color:#000000;font-family:Calibri"><caption><strong>Process Data Query1</strong></caption></span><thead> <tr> <th><span style="font-size:8pt;color:#000000;font-family:Calibri">_TIMESTAMP</span></th><th><span style="font-size:8pt;color:#000000;font-family:Calibri">_NAME</span></th><th><span style="font-size:8pt;color:#000000;font-family:Calibri">_VALUE</span></th> </tr> </thead> <tbody> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.Main</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">126</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint1</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">160</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint2</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">0</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.Main</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">126</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint1</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">160</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:59:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint2</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">0</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:58:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.Main</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">126</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:58:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint1</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">160</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:58:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint2</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">0</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:57:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.Main</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">126</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:57:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint1</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">160</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:57:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint2</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">0</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:56:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.Main</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">127</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:56:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint1</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">160</span></td> </tr> <tr valign=top> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">11/10/2009 11:56:56 PM</span></td> <td bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">TANK30TC1.TANK30TC1.192_168_15_204.iSeries01.SetPoint2</span></td> <td align=right bordercolor="#d0d7e5"><span style="font-size:8pt;color:#000000;font-family:Calibri">0</span></td> </tr> </tbody> <tfoot></tfoot> </table> <br/>What I need is for it to use the grouped &quot;_TIMESTAMP&quot; for the rows, the &quot;_NAME&quot; for the column headings and &quot;_VALUE&quot; for the value.<br/><br/>IE<br/><br/>                         _NAME1     _NAME2     _NAME3<br/>_TIMESTAMP1     _VALUE1   _VALUE2   _VALUE3<br/>_TIMESTAMP2     _VALUE1   _VALUE2   _VALUE3<br/>_TIMESTAMP3     _VALUE1   _VALUE2   _VALUE3<br/><br/><br/>I can't seem to get the syntax right.......Thu, 12 Nov 2009 14:02:32 Z2009-11-24T19:31:39Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a302cfca-8795-4257-9d1b-3c7dfb0fbdb7http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a302cfca-8795-4257-9d1b-3c7dfb0fbdb7jyllandhttp://social.msdn.microsoft.com/Profile/en-US/?user=jyllandIs it posible to combine an delete and insert statement in the same stored procedurehi <br/><br/>Is it posible to combine an delete and insert statement in the same stored procedure.<br/><br/>What I want is to delete first and then insert new.<br/><br/>if it is posible how ?<br/><br/>best regard<br/><br/>jyllandTue, 24 Nov 2009 14:20:02 Z2009-11-25T03:09:28Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ace6e75f-ad81-4ddc-bd97-ca80a915fd90http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/ace6e75f-ad81-4ddc-bd97-ca80a915fd90Naomhttp://social.msdn.microsoft.com/Profile/en-US/?user=NaomLoading a complex XML (no schema) into a tableHi everybody,<br/><br/>I have SQL Server Express 2008, so I can not really run SSIS :( I want to import XML file like this<br/><br/><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"> <p> </p> </span></span></span></span><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"> <p> </p> </span></span></span><span style="font-family:Consolas;color:#0000ff;font-size:x-small"> <p> </p> </span></span> <p><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"> <pre lang=x-xml>&lt;?xml version=&quot;1.0&quot;?&gt; &lt;!--Timesheets and Jobs created at E I Day Tripping on 16-Apr-2009 at 3:00:07 PM--&gt; &lt;Time siteName=&quot;E I Day Tripping&quot; currPeriodEnd=&quot;03-Apr-2009&quot;&gt; &lt;Timesheet levelCode=&quot;SE0033&quot; team=&quot;EI&quot; periodEndDate=&quot;03-Apr-2009&quot; periodForDate=&quot;03-Apr-2009&quot; tsType=&quot;W&quot; tsNumber=&quot;SYSTEM&quot;&gt; &lt;Booking bookingDate=&quot;03-Apr-2009&quot; project=&quot;SKSWOOD&quot; jobCard=&quot;SS-CPCA070801&quot; workCode=&quot;D01&quot; skillCode=&quot;EL03&quot; shiftCode=&quot;DT&quot; locationCode=&quot;OFFSHORE EI&quot; normal=&quot;12&quot;/&gt; &lt;/Timesheet&gt; &lt;/Time&gt;</pre> <p>into a new table(s). What are my options?<br/><br/>Thanks in advance.</p> </span></span></span></p><hr class="sig"><font color=blue>Premature optimization is the root of all evil in programming.</font> (c) by Donald Knuth<br/><br/> Naomi Nosonovsky, Sr. Programmer-Analyst <br/><br/><b> <center><a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=218">My blog<a></center></b>Tue, 24 Nov 2009 04:52:30 Z2009-11-24T18:43:19Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/551cecab-6990-42bb-8289-8118d6864dd9http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/551cecab-6990-42bb-8289-8118d6864dd9Karenroshttp://social.msdn.microsoft.com/Profile/en-US/?user=KarenrosHow do i check if the top 3 values and bottom two values = 3 in a column<p>Hi,</p> <p>I have table that has following fields</p> <p>T_Id   client_Id    Submit_dt     asam_values</p> <p>1          15              1/1/2009      1,2,3,4,5,6</p> <p>2           15             1/1/2009       1,1,1,1,1,1</p> <p>etc.</p> <p>I want to select those rows if the asam_values 1 or top 3 or first value is 3 and 2 of the bottom 3 is a 3 then i want to select that row else omit it?</p> <p>How can i do it..</p> <p>Any help will appreciated.</p> <p>Thanks,</p> <p>Karen</p> <p> </p>Tue, 24 Nov 2009 15:49:39 Z2009-11-24T18:30:28Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/74000ee0-5f05-441e-a1db-abae92038bf6http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/74000ee0-5f05-441e-a1db-abae92038bf6Parthik Parikhhttp://social.msdn.microsoft.com/Profile/en-US/?user=Parthik%20ParikhDTS package Dynamically usedHow Can I transfer Tables from One Server, Database to another server,database using Dynamic Properties task (Configuration files) or Global Variables. The table schema needs to copy first from the source server to Destination Server and then the data needs to be copied.<br/><br/>I tried two different way but I couldn't figure out how to dynamically get the table schema and create the table definition to destination server?Mon, 23 Nov 2009 23:23:02 Z2009-11-24T17:59:36Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/3bcc7c33-ee26-44d8-9415-9fb29152a657http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/3bcc7c33-ee26-44d8-9415-9fb29152a657Jonel Amranhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jonel%20AmranDate calendar 1 yearExpert,<br/>I need date calendar a year by using SQL, please..<br/><br/>Regards,<br/><br/>JonelTue, 24 Nov 2009 13:15:45 Z2009-11-24T17:49:42Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/377350ce-0408-4a4c-8895-e8d5f8364430http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/377350ce-0408-4a4c-8895-e8d5f8364430devlssishttp://social.msdn.microsoft.com/Profile/en-US/?user=devlssisSELECT NOT NULL COLUMNHi, I know this might be a simple task for many of you.<br/>I want to pick the value from column CODE where its not null<br/>but it should pick the first of the kind record looking at the column RECNO.<br/><br/><br/>RECNO                 CODE                     SEQ_NO     ID<br/>1                            PEK                    1985             1<br/>2                            NULL                   1986             1<br/>1                            NULL                   1987             2<br/>2                            KTI                     1988             2<br/>1                            kkk                     1989             3<br/>2                            ppp                     1990             3<br/><br/><br/>i need to pick PEK,KTI,KKK<br/><br/>THANKSTue, 24 Nov 2009 15:25:33 Z2009-11-24T16:31:05Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/e24d4d8a-c792-4f12-9cf7-10af0afd4077http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/e24d4d8a-c792-4f12-9cf7-10af0afd4077Jinglenosehttp://social.msdn.microsoft.com/Profile/en-US/?user=JinglenoseLinked Servers Slow<p><font style="background-color:#ffffff">Hi Everyone,</font></p> <p align=left><br><font style="background-color:#ffffff">I'm in the UK and I'm connecting to a server in Canada.  <br>On my London machine can connect directly to my server in Canada through Management Studio and execute a stored proc instantly (16ms).  If I make the same call through a four part name logged into my local server in London, it takes 500ms.  This is too slow for me.  Is there some way to speed up my linked server given the way I've set it up above.  <br>I can't beleive it's adding 0.5s to every call.<br> </font></p> <p><font style="background-color:#ffffff">The linked server has the following settings...</font></p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">Collation compatible True</font></p> <p><font style="background-color:#ffffff">Data Access True</font></p> <p><font style="background-color:#ffffff">RPC True</font></p> <p><font style="background-color:#ffffff">RPC Out True</font></p> <p><font style="background-color:#ffffff">Use Remote Collation True</font></p> <p><font style="background-color:#ffffff">Collation Name &lt;Blank&gt;</font></p> <p><font style="background-color:#ffffff">Connection Timeout 0</font></p> <p><font style="background-color:#ffffff">Query Timeout 0</font></p> <p align=left> </p> <p align=left>Changing these setting made no difference.</p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">The security is set up to &quot;Be made using this security context:&quot;</font></p> <p align=left> </p> <p><font style="background-color:#ffffff">When I try and set it up using any other option I get problems...</font><font style="background-color:#ffffff"><br></font></p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">I've tried setting up my authentication for my linked server differently but it just won't work.  In the linked server properties window, I add a user mapping in the SQL Server Mappings Table.  This is a user I can log in as and I've tried checking the impersonate check box and entering the same login and password in the remote login boxes as the local login.</font></p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">I get the following error messages when I select the following options...</font></p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff"><strong>Not to be made</strong> Gave the following error:</font></p> <p><font style="background-color:#ffffff">&quot;Access to the remote server is denied because no login-mapping exists&quot;  This clearly isn't true.</font></p> <p><font style="background-color:#ffffff"><strong>Be made without using a security context</strong> Gave the following error:</font></p> <p><font style="background-color:#ffffff">OLE DB provider &quot;SQLNCLI&quot; for linked server &quot;orbcasql02&quot; returned message &quot;Invalid authorization specification&quot;.</font></p> <p><font style="background-color:#ffffff">Msg 7399, Level 16, State 1, Line 1</font></p> <p><font style="background-color:#ffffff">The OLE DB provider &quot;SQLNCLI&quot; for linked server &quot;orbcasql02&quot; reported an error. Authentication failed.</font></p> <p><font style="background-color:#ffffff">Msg 7303, Level 16, State 1, Line 1</font></p> <p><font style="background-color:#ffffff">Cannot initialize the data source object of OLE DB provider &quot;SQLNCLI&quot; for linked server &quot;orbcasql02&quot;.</font></p> <p><font style="background-color:#ffffff"><strong>Be made using the login’s current security context</strong> Gave the following error:</font></p> <p><font style="background-color:#ffffff">Msg 18456, Level 14, State 1, Line 1</font></p> <p><font style="background-color:#ffffff">Login failed for user 'db'.</font></p> <p align=left> </p> <p align=left>I've tripple checked the user name and password.</p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">This last one is particularly confusing because there has been no mention of the user db and I was logged in as OPSWriter. I did change the setting when logged in as db though so I guess it must have rembered that????<br> <br></font></p> <p><font style="background-color:#ffffff">I'd be very grateful of some help since this kind of latency is going to cause real problems.</font></p> <p><font style="background-color:#ffffff"> </font></p> <p><font style="background-color:#ffffff">Nigel Jones<br></font></p> <p align=left><font style="background-color:#ffffff" face=Arial size=2></font> </p>Fri, 24 Oct 2008 11:41:33 Z2009-11-25T03:11:59Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5da9f6a3-093d-43ca-8358-f2e30e464d7ehttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5da9f6a3-093d-43ca-8358-f2e30e464d7ebb_aa123http://social.msdn.microsoft.com/Profile/en-US/?user=bb_aa123update openquery remote table with join<p><font><font><font>Hi All,<br><br>I have a problem with OPENQUERY update on remote table. I've googled for days, and can't solve the problem. <br>Here is the problem:<br>Local server MS SQL 2005.<br>Remote server: MySQL.<br>The linked server is communicating through MySQL ODBC 5.1<br><br>I created the following code for update (tried many other version too, this is the last one):<br><br><font color="#0000ff" size=2><font color="#0000ff" size=2>update</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>openquery</font></font><font color="#808080" size=2><font color="#808080" size=2>(Remoteserver</font></font><font color="#808080" size=2><font color="#808080" size=2>,</font></font><font color="#ff0000" size=2><font color="#ff0000" size=2>'select products_id, products_price, products_last_modified, products_stock from products1'</font></font><font color="#808080" size=2><font color="#808080" size=2>)<br></font></font><font color="#0000ff" size=2><font color="#0000ff" size=2>set<br></font></font><font size=2>products_price</font><font color="#808080" size=2><font color="#808080" size=2>=</font></font><font size=2>A</font><font color="#808080" size=2><font color="#808080" size=2>.</font></font><font size=2>products_price</font><font color="#808080" size=2><font color="#808080" size=2>,<br></font></font><font size=2>products_stock</font><font color="#808080" size=2><font color="#808080" size=2>=</font></font><font size=2>A</font><font color="#808080" size=2><font color="#808080" size=2>.</font></font><font size=2>products_stock</font><font color="#808080" size=2><font color="#808080" size=2>,<br></font></font><font size=2>products_last_modified</font><font color="#808080" size=2><font color="#808080" size=2>=</font></font><font color="#ff00ff" size=2><font color="#ff00ff" size=2>getdate</font></font><font color="#808080" size=2><font color="#808080" size=2>()<br></font></font><font color="#0000ff" size=2><font color="#0000ff" size=2>FROM<br></font></font><font color="#808080" size=2><font color="#808080" size=2>(</font></font><font color="#0000ff" size=2><font color="#0000ff" size=2>select</font></font> <font color="#808080" size=2><font color="#808080" size=2>*</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>from</font></font><font size=2> vi_products_update</font><font color="#808080" size=2><font color="#808080" size=2>)</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>AS</font></font><font size=2> A INNER</font> <font size=2><font color="#808080">JOIN<br></font></font><font size=2><font color="#0000ff"><font color="#0000ff">openquery</font></font><font color="#808080"><font color="#808080">(</font></font>octopus<font color="#808080"><font color="#808080">,</font></font></font> <font color="#ff0000" size=2><font color="#ff0000" size=2>'select products_id from products1'</font></font><font color="#808080" size=2><font color="#808080" size=2>)</font></font><font color="#0000ff" size=2><font color="#0000ff" size=2>AS</font></font><font size=2> B<br></font><font color="#0000ff" size=2><font color="#0000ff" size=2>ON</font></font><font size=2> A</font><font color="#808080" size=2><font color="#808080" size=2>.</font></font><font size=2>products_id</font><font color="#808080" size=2><font color="#808080" size=2>=</font></font><font size=2>B</font><font color="#808080" size=2><font color="#808080" size=2>.</font></font><font size=2>products_id<br><br>When I run the query the columns are updated with the same value, for each record. It looks the value is the first which match the criteria. Each (remote and local) table has as primary key products_id. In this example I used as result set for local a view, but I have the same result if I use a table.<br>the products_last_modified column is updated in order, and if I update for e.g. the products_price with a constant that is ok too. The problem should be somewhere with join, but I can' get it where.<br>THX for any help<br><br></p></font></font></font></font><hr class="sig">abThu, 26 Mar 2009 14:54:29 Z2009-11-24T16:04:19Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/f1f8cd7e-6127-4dd9-8ec0-59d355edcb14http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/f1f8cd7e-6127-4dd9-8ec0-59d355edcb14Brokenlynxhttp://social.msdn.microsoft.com/Profile/en-US/?user=BrokenlynxHighest unique entryShort simple question, what SQL keyword should I be using in my where clause to get only a single unique NCR number but the highest version for each one? This needs to be done without a group by as there are many more fields than just the two shown and they're not aggregated (causes an error).  <div><br/></div> <div><br/></div> <div><br/></div> <div> <pre lang=x-html>ID Version NCR09-0001 1 NCR09-0001 2 NCR09-0001 3 NCR09-0002 1 NCR09-0002 5 NCR09-0002 6 NCR09-0002 3 NCR09-0002 4 NCR09-0002 2 NCR09-0003 1 NCR09-0003 45 NCR09-0003 46 NCR09-0003 47 NCR09-0003 48 NCR09-0003 49 NCR09-0003 50 </pre> <br/></div>Tue, 24 Nov 2009 14:57:50 Z2009-11-24T16:44:16Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b82ebd94-9549-430b-bd67-e37198bf431bhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b82ebd94-9549-430b-bd67-e37198bf431bDaveDBhttp://social.msdn.microsoft.com/Profile/en-US/?user=DaveDBQuerying inserted and deleted tables using triggers<p>Hello,<br/><br/>In regards to triggers...<br/><br/>Where are the Inserted and deleted tables located and how do I query those tables?<br/><br/>Any help would be appreciated.<br/><br/>Dave<br/> </p><hr class="sig">Dave SQL DeveloperTue, 24 Nov 2009 15:15:35 Z2009-11-24T15:34:04Zhttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/67287fe9-10ad-4705-97c9-35e63180591ehttp://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/67287fe9-10ad-4705-97c9-35e63180591erock007http://social.msdn.microsoft.com/Profile/en-US/?user=rock007Designing<span class=Apple-style-span style="word-spacing:0px;font-family:Verdana;font-style:normal;font-variant:normal;font-weight:normal;font-size:11px;line-height:normal;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:collapse"><span class=Apple-style-span style="word-spacing:0px;font-family:Verdana;font-style:normal;font-variant:normal;font-weight:normal;font-size:11px;line-height:normal;text-transform:none;text-indent:0px;white-space:normal;letter-spacing:normal;border-collapse:collapse">one dropdownlist is of some city names and other one is of locations in that city.<span class=Apple-converted-space> </span> <br/> <br/> so if user select any city then all the locations of that city should be come into 2nd dropdownlist (DropDownListLocation).<span class=Apple-converted-space> </span> <br/> <br/> how to design the database?<span class=Apple-converted-space><br/> </span> </span> </span>Tue, 24 Nov 2009 10:20:43 Z2009-11-24T15:07:39Z