From marklbreen at gmail.com Fri Sep 3 02:52:09 2010 From: marklbreen at gmail.com (Mark Breen) Date: Fri, 3 Sep 2010 08:52:09 +0100 Subject: [dba-SQLServer] Exporting to Excel when Excel is not installed Message-ID: Hello All, I have a server running a DotNetNuke application. I want to export data to Excel format from a sql source. I can write a DLL to do that, but I suspect that if Excel is not installed on the box, it will not work. Is that so? If I uploaded some of my Excel DLL's would it work then? The db is SQL Express so I suspect I cannot use SSIS - which is a pity. It is running in a shared hosting environment and I have what I think is named "Medium Trust" privillages. AFAIK, this means that I would not be able to run an Exe on the box. I can schedule a DLL to be executed however through the in built scheduling too. any thoughts on how to achieve this? Currently I am learning towards writing to a text file and naming is data.xls which will trick excel into opening a text tab delimited file. Any better ideas? Thanks Mark From ebarro at roadrunner.com Fri Sep 3 03:02:46 2010 From: ebarro at roadrunner.com (Eric Barro) Date: Fri, 3 Sep 2010 01:02:46 -0700 Subject: [dba-SQLServer] Exporting to Excel when Excel is not installed In-Reply-To: References: Message-ID: <002401cb4b3e$656c8f20$3045ad60$@com> Mark, I would grab the data from sql and write it out to a file with a CSV or even XLS extension. Excel will be perfectly happy to open that file. If you want to write code you can use the response.contentype to specify an Excel output. HttpContext.Current.Response.ContentType = "Application/x-msexcel" Eric -----Original Message----- From: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Mark Breen Sent: Friday, September 03, 2010 12:52 AM To: Discussion of Hardware and Software issues; Discussion concerning MS SQL Server Subject: [dba-SQLServer] Exporting to Excel when Excel is not installed Hello All, I have a server running a DotNetNuke application. I want to export data to Excel format from a sql source. I can write a DLL to do that, but I suspect that if Excel is not installed on the box, it will not work. Is that so? If I uploaded some of my Excel DLL's would it work then? The db is SQL Express so I suspect I cannot use SSIS - which is a pity. It is running in a shared hosting environment and I have what I think is named "Medium Trust" privillages. AFAIK, this means that I would not be able to run an Exe on the box. I can schedule a DLL to be executed however through the in built scheduling too. any thoughts on how to achieve this? Currently I am learning towards writing to a text file and naming is data.xls which will trick excel into opening a text tab delimited file. Any better ideas? Thanks Mark _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From marklbreen at gmail.com Sat Sep 4 11:54:19 2010 From: marklbreen at gmail.com (Mark Breen) Date: Sat, 4 Sep 2010 17:54:19 +0100 Subject: [dba-SQLServer] [dba-Tech] Exporting to Excel when Excel is not installed In-Reply-To: References: Message-ID: Hello to everyone that replied to this, Thanks for your responses. You have all answered my question which is - export it as text and name is xls thanks again Mark On 3 September 2010 14:53, Heenan, Lambert < Lambert.Heenan at chartisinsurance.com> wrote: > If you only want to export to a single worksheet then export to a CSV file, > which is just text, but Excel will happily load it. > > Lambert > > -----Original Message----- > From: dba-tech-bounces at databaseadvisors.com [mailto: > dba-tech-bounces at databaseadvisors.com] On Behalf Of Mark Breen > Sent: Friday, September 03, 2010 3:52 AM > To: Discussion of Hardware and Software issues; Discussion concerning MS > SQL Server > Subject: [dba-Tech] Exporting to Excel when Excel is not installed > > Hello All, > > I have a server running a DotNetNuke application. > > I want to export data to Excel format from a sql source. I can write a DLL > to do that, but I suspect that if Excel is not installed on the box, it will > not work. Is that so? > > If I uploaded some of my Excel DLL's would it work then? > > The db is SQL Express so I suspect I cannot use SSIS - which is a pity. > > It is running in a shared hosting environment and I have what I think is > named "Medium Trust" privillages. AFAIK, this means that I would not be > able to run an Exe on the box. > > I can schedule a DLL to be executed however through the in built scheduling > too. > > any thoughts on how to achieve this? > > Currently I am learning towards writing to a text file and naming is > data.xls which will trick excel into opening a text tab delimited file. > > Any better ideas? > > Thanks > > Mark > _______________________________________________ > dba-Tech mailing list > dba-Tech at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-tech > Website: http://www.databaseadvisors.com > > _______________________________________________ > dba-Tech mailing list > dba-Tech at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-tech > Website: http://www.databaseadvisors.com > From ha at phulse.com Sat Sep 4 11:55:49 2010 From: ha at phulse.com (Hans-Christian Andersen) Date: Sat, 4 Sep 2010 09:55:49 -0700 Subject: [dba-SQLServer] Cloud computing never ceases to amaze me Message-ID: Amazing write only cloud service. Now, if only I could find a use for this. SuperSimple StorageService http://www.supersimplestorageservice.com/ ( Highlight the following black block below for a spoiler ). This site is a parody Hans-Christian Web Application Development, London, UK e: hans.andersen at phulse.com t: +44 (0)782 894 5456 w: http://nokenode.com l: http://uk.linkedin.com/in/andersenhc ------------------------------------------------------ Unique Gifts, Collectables, Artwork ------------------------------------------------------ Come one Come all to www.corinnajasmine.com ------------------------------------------------------ From gustav at cactus.dk Sat Sep 4 12:26:56 2010 From: gustav at cactus.dk (Gustav Brock) Date: Sat, 04 Sep 2010 19:26:56 +0200 Subject: [dba-SQLServer] Cloud computing never ceases to amaze me Message-ID: Hi Hans-Christian I like the proof of concept: http://www.supersimplestorageservice.com/secure/s4/pipeline/cache/throughput/proof.aspx As for the usage, you actually sometimes may need a "black hole" or NULL device when testing, thus is site is only 99% insane. /gustav >>> ha at phulse.com 04-09-2010 18:55 >>> Amazing write only cloud service. Now, if only I could find a use for this. SuperSimple StorageService http://www.supersimplestorageservice.com/ Hans-Christian Web Application Development, London, UK e: hans.andersen at phulse.com t: +44 (0)782 894 5456 w: http://nokenode.com l: http://uk.linkedin.com/in/andersenhc ------------------------------------------------------ Unique Gifts, Collectables, Artwork ------------------------------------------------------ Come one Come all to www.corinnajasmine.com From ha at phulse.com Sat Sep 4 13:10:00 2010 From: ha at phulse.com (Hans-Christian Andersen) Date: Sat, 4 Sep 2010 11:10:00 -0700 Subject: [dba-SQLServer] Cloud computing never ceases to amaze me In-Reply-To: References: Message-ID: Ah, are you referring to the /dev/null database? http://adrianotto.com/2010/08/dev-null-unlimited-scale/ Hans-Christian Web Application Development, London, UK e: hans.andersen at phulse.com t: +44 (0)782 894 5456 w: http://nokenode.com l: http://uk.linkedin.com/in/andersenhc ------------------------------------------------------ Unique Gifts, Collectables, Artwork ------------------------------------------------------ Come one Come all to www.corinnajasmine.com ------------------------------------------------------ On 4 September 2010 10:26, Gustav Brock wrote: > Hi Hans-Christian > > I like the proof of concept: > > > http://www.supersimplestorageservice.com/secure/s4/pipeline/cache/throughput/proof.aspx > > As for the usage, you actually sometimes may need a "black hole" or NULL > device when testing, thus is site is only 99% insane. > > /gustav > > > >>> ha at phulse.com 04-09-2010 18:55 >>> > Amazing write only cloud service. Now, if only I could find a use for this. > > SuperSimple StorageService > http://www.supersimplestorageservice.com/ > > Hans-Christian > Web Application Development, London, UK > > e: hans.andersen at phulse.com > t: +44 (0)782 894 5456 > w: http://nokenode.com > l: http://uk.linkedin.com/in/andersenhc > > ------------------------------------------------------ > Unique Gifts, Collectables, Artwork > ------------------------------------------------------ > Come one > Come all to > www.corinnajasmine.com > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From gustav at cactus.dk Sat Sep 4 13:27:37 2010 From: gustav at cactus.dk (Gustav Brock) Date: Sat, 04 Sep 2010 20:27:37 +0200 Subject: [dba-SQLServer] Cloud computing never ceases to amaze me Message-ID: Hi Hans-Christian Well, actually no - but yes, the Blackhole Storage Engine is one such implementation. /gustav >>> ha at phulse.com 04-09-2010 20:10 >>> Ah, are you referring to the /dev/null database? http://adrianotto.com/2010/08/dev-null-unlimited-scale/ Hans-Christian Web Application Development, London, UK e: hans.andersen at phulse.com t: +44 (0)782 894 5456 w: http://nokenode.com l: http://uk.linkedin.com/in/andersenhc ------------------------------------------------------ Unique Gifts, Collectables, Artwork ------------------------------------------------------ Come one Come all to www.corinnajasmine.com ------------------------------------------------------ On 4 September 2010 10:26, Gustav Brock wrote: > Hi Hans-Christian > > I like the proof of concept: > > > http://www.supersimplestorageservice.com/secure/s4/pipeline/cache/throughput/proof.aspx > > As for the usage, you actually sometimes may need a "black hole" or NULL > device when testing, thus is site is only 99% insane. > > /gustav > > > >>> ha at phulse.com 04-09-2010 18:55 >>> > Amazing write only cloud service. Now, if only I could find a use for this. > > SuperSimple StorageService > http://www.supersimplestorageservice.com/ > > Hans-Christian > Web Application Development, London, UK > > e: hans.andersen at phulse.com > t: +44 (0)782 894 5456 > w: http://nokenode.com > l: http://uk.linkedin.com/in/andersenhc From jwcolby at colbyconsulting.com Fri Sep 10 13:09:03 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 10 Sep 2010 14:09:03 -0400 Subject: [dba-SQLServer] SQL Server Express - true skinny Message-ID: <4C8A743F.2080808@colbyconsulting.com> Does anyone know the true skinny on the limitations placed on SQL Server express in order to "throttle" it.? I found a blog that claims that outside of CPU / Memory / DB size limitations, there is not other "governor". So it appears that the limitations are: 10 gig db file size. This does not discuss additional database files. 1 CPU. However it appears that it can use 4 cores if available 1 gig of memory. This appears to be the killer. But there appears to be no "user" limitations etc. So the question is, would SQL Server express be capable of replacing Jet for a complex bound application for 30 users? -- John W. Colby www.ColbyConsulting.com From jwcolby at colbyconsulting.com Fri Sep 17 13:17:30 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 17 Sep 2010 14:17:30 -0400 Subject: [dba-SQLServer] updating records in SQL Server with a clustered index Message-ID: <4C93B0BA.3020706@colbyconsulting.com> I am performing a "big picture" operation where records in an "address" table are copied to an "old address" table when an address changes. The table has a clustered index on it, using the PK as the key for the index. I had intended to do a copy, delete and append. IOW delete the old address records to "get them out of the way" and then append the new records back in in the "holes created". Obviously this is the brute force way, and simply updating every field except the PK makes more sense. this makes more sense due to how the clustered index works and all the work that has to be done to delete a record "in the middle" of a table with a clustered index. So the delete / add operation becomes an update operation. The update syntax gets ugly only because the field names are identical and the table comes from a temp database, joined to the data in the current database. Uggg. I don't want to used a stored view because this has to happen in a dynamic process on any selected database. Any thoughts on how to go about this? -- John W. Colby www.ColbyConsulting.com From jwcolby at colbyconsulting.com Fri Sep 17 13:28:31 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 17 Sep 2010 14:28:31 -0400 Subject: [dba-SQLServer] I found this Message-ID: <4C93B34F.6020705@colbyconsulting.com> UPDATE TABLEA SET FIELD1 = (SELECT TABLEB.FIELD1 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID), FIELD2= (SELECT TABLEB.FIELD2 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID) It seems darned ugly but I can see the logic. However I have about 16 fields to update for (in some cases) a subset of 2 million records in a 65 million record table. It seems like this is going to be very inefficient. -- John W. Colby www.ColbyConsulting.com From davidmcafee at gmail.com Fri Sep 17 13:51:22 2010 From: davidmcafee at gmail.com (David McAfee) Date: Fri, 17 Sep 2010 11:51:22 -0700 Subject: [dba-SQLServer] [AccessD] I found this In-Reply-To: <4C93B34F.6020705@colbyconsulting.com> References: <4C93B34F.6020705@colbyconsulting.com> Message-ID: Have you tried it this way? UPDATE TABLEA SET A.FIELD1 = B.FIELD1, A.FIELD2 = B.FIELD2, A.FIELD3 = B.FIELD3, A.FIELD4 = B.FIELD4, A.FIELD5 = B.FIELD5, A.FIELD6 = B.FIELD6, A.FIELD7 = B.FIELD7, A.FIELD8 = B.FIELD8, A.FIELD9 = B.FIELD9, A.FIELD10 = B.FIELD10, A.FIELD11 = B.FIELD11, A.FIELD12 = B.FIELD12, A.FIELD13 = B.FIELD13, A.FIELD14 = B.FIELD14, A.FIELD15 = B.FIELD15, A.FIELD16 = B.FIELD16, FROM TABLEA AS A INNER JOIN TABLEB AS B ON A.ID = B.ID On Fri, Sep 17, 2010 at 11:28 AM, jwcolby wrote: > UPDATE TABLEA SET > FIELD1 = (SELECT TABLEB.FIELD1 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID), > FIELD2= (SELECT TABLEB.FIELD2 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID) > > It seems darned ugly but I can see the logic. ?However I have about 16 fields to update for (in some > cases) a subset of 2 million records in a 65 million record table. > > It seems like this is going to be very inefficient. > > -- > John W. Colby > www.ColbyConsulting.com > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com > From jwcolby at colbyconsulting.com Fri Sep 17 14:37:12 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 17 Sep 2010 15:37:12 -0400 Subject: [dba-SQLServer] [AccessD] I found this In-Reply-To: References: <4C93B34F.6020705@colbyconsulting.com> Message-ID: <4C93C368.6000707@colbyconsulting.com> David, Works like a dream. I have used field aliases forever, but table aliases are new to me with SQL Server. John W. Colby www.ColbyConsulting.com On 9/17/2010 2:51 PM, David McAfee wrote: > Have you tried it this way? > > UPDATE TABLEA > SET > A.FIELD1 = B.FIELD1, > A.FIELD2 = B.FIELD2, > A.FIELD3 = B.FIELD3, > A.FIELD4 = B.FIELD4, > A.FIELD5 = B.FIELD5, > A.FIELD6 = B.FIELD6, > A.FIELD7 = B.FIELD7, > A.FIELD8 = B.FIELD8, > A.FIELD9 = B.FIELD9, > A.FIELD10 = B.FIELD10, > A.FIELD11 = B.FIELD11, > A.FIELD12 = B.FIELD12, > A.FIELD13 = B.FIELD13, > A.FIELD14 = B.FIELD14, > A.FIELD15 = B.FIELD15, > A.FIELD16 = B.FIELD16, > FROM TABLEA AS A > INNER JOIN TABLEB AS B > ON A.ID = B.ID > > > On Fri, Sep 17, 2010 at 11:28 AM, jwcolby wrote: >> UPDATE TABLEA SET >> FIELD1 = (SELECT TABLEB.FIELD1 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID), >> FIELD2= (SELECT TABLEB.FIELD2 FROM TABLEB WHERE TABLEB.ID = TABLEA.ID) >> >> It seems darned ugly but I can see the logic. However I have about 16 fields to update for (in some >> cases) a subset of 2 million records in a 65 million record table. >> >> It seems like this is going to be very inefficient. >> >> -- >> John W. Colby >> www.ColbyConsulting.com >> -- >> AccessD mailing list >> AccessD at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/accessd >> Website: http://www.databaseadvisors.com >> > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From marklbreen at gmail.com Sun Sep 19 09:49:56 2010 From: marklbreen at gmail.com (Mark Breen) Date: Sun, 19 Sep 2010 15:49:56 +0100 Subject: [dba-SQLServer] updating records in SQL Server with a clustered index In-Reply-To: <4C93B0BA.3020706@colbyconsulting.com> References: <4C93B0BA.3020706@colbyconsulting.com> Message-ID: Hello John, Not really sure that I understand from the email what you are trying to do, and not sure what you are asking advice about? What I am assuming your asking is how best to do the following 1) You have an existing address with PK 2) you have in a temp table a new address 3) you want to take the existing address, and save it in a address archive table 4) You want to overwrite the existing address with the new details 5) you want to move to the next record in the temp table ? Is that right? What do we need to consider relating to the clustered index (as you mentioned it, I guess there must be something to consider)? on about ten occasions in the last ten years, I have resorted to cursors. I found them easy to work with, and especially when I wanted to track my steps and perform my work in a sequential fashion. When I used cursors speed of updates went from 1-2 seconds to minutes sometimes, but on the other hand, by using cursors, I was able to handcraft the exact sql I wanted applied, and I could monitor progress. I could even work in batchs eg 1000 records and stop. Have you ever played with them? I think with your enormous volumes of data, you might be able to make use of cursors IOW, open a cursors for the addresses that are in the temp table, and then you can neatly apply updates and archive statements to the main db tables. Of course if there is 52 million records in the temp table then forget this approach :) Mark On 17 September 2010 19:17, jwcolby wrote: > I am performing a "big picture" operation where records in an "address" > table are copied to an "old > address" table when an address changes. The table has a clustered index on > it, using the PK as the > key for the index. > > I had intended to do a copy, delete and append. IOW delete the old address > records to "get them out > of the way" and then append the new records back in in the "holes created". > > Obviously this is the brute force way, and simply updating every field > except the PK makes more > sense. this makes more sense due to how the clustered index works and all > the work that has to be > done to delete a record "in the middle" of a table with a clustered index. > > So the delete / add operation becomes an update operation. > > The update syntax gets ugly only because the field names are identical and > the table comes from a > temp database, joined to the data in the current database. > > Uggg. > > I don't want to used a stored view because this has to happen in a dynamic > process on any selected > database. > > Any thoughts on how to go about this? > > -- > John W. Colby > www.ColbyConsulting.com > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From jnatola at hotmail.com Tue Sep 21 15:17:35 2010 From: jnatola at hotmail.com (Jean-Paul natola) Date: Tue, 21 Sep 2010 16:17:35 -0400 Subject: [dba-SQLServer] OT:DFSR db's Message-ID: Does anyone know , or has tried windows DFSR functionality with a sql db? TIA J From fhtapia at gmail.com Tue Sep 21 21:08:07 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Tue, 21 Sep 2010 19:08:07 -0700 Subject: [dba-SQLServer] OT:DFSR db's In-Reply-To: References: Message-ID: Have not... I'm not sure what you are attempt to do, do you just want high availability? If so SQL has built in tools for that such as replication, transaction log shipping, and database mirroring to name a few. You can also do a full blown clustered environment. Sent from my mobile On Sep 21, 2010, at 1:17 PM, Jean-Paul natola wrote: > > Does anyone know , or has tried windows DFSR functionality with a > sql db? > > TIA > > J > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From jwcolby at colbyconsulting.com Tue Sep 21 23:28:33 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Wed, 22 Sep 2010 00:28:33 -0400 Subject: [dba-SQLServer] You'll love this! Message-ID: <4C9985F1.2010705@colbyconsulting.com> My client asked me to sum (give him a count of) the records in zips where the count in each zip is > 20. No problem, do a count group by zip, where the count > 20. This gives me a list of such zip codes. Then build a sum on that base query. No comes the not so cool part. I will have to select my records only from zips where the count of records in a zip is >=20. This may be easy, join the base query back to the table and update a field to a code, then select from the table where that code is set. My fear is that the join will make the view non-updateable. Which will leave me using a cursor? We shall see. -- John W. Colby www.ColbyConsulting.com From Gustav at cactus.dk Wed Sep 22 01:23:07 2010 From: Gustav at cactus.dk (Gustav Brock) Date: Wed, 22 Sep 2010 08:23:07 +0200 Subject: [dba-SQLServer] You'll love this! Message-ID: Hi John Why not write the list of those 20+ zip items to a temp table, then join this with your table to update? /gustav >>> jwcolby at colbyconsulting.com 22-09-2010 06:28 >>> My client asked me to sum (give him a count of) the records in zips where the count in each zip is > 20. No problem, do a count group by zip, where the count > 20. This gives me a list of such zip codes. Then build a sum on that base query. No comes the not so cool part. I will have to select my records only from zips where the count of records in a zip is >=20. This may be easy, join the base query back to the table and update a field to a code, then select from the table where that code is set. My fear is that the join will make the view non-updateable. Which will leave me using a cursor? We shall see. -- John W. Colby www.ColbyConsulting.com From jnatola at hotmail.com Wed Sep 22 06:27:15 2010 From: jnatola at hotmail.com (Jean-Paul natola) Date: Wed, 22 Sep 2010 07:27:15 -0400 Subject: [dba-SQLServer] OT:DFSR db's In-Reply-To: References: , Message-ID: we have a field office with less than reliable connectivity, and it would be nice if they can work locally on th DB when the internet is down Jean-Paul Natola > From: fhtapia at gmail.com > To: dba-sqlserver at databaseadvisors.com > Date: Tue, 21 Sep 2010 19:08:07 -0700 > CC: dba-sqlserver at databaseadvisors.com > Subject: Re: [dba-SQLServer] OT:DFSR db's > > Have not... I'm not sure what you are attempt to do, do you just want > high availability? If so SQL has built in tools for that such as > replication, transaction log shipping, and database mirroring to name > a few. You can also do a full blown clustered environment. > > Sent from my mobile > > On Sep 21, 2010, at 1:17 PM, Jean-Paul natola > wrote: > > > > > Does anyone know , or has tried windows DFSR functionality with a > > sql db? > > > > TIA > > > > J > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From jwcolby at colbyconsulting.com Wed Sep 22 07:48:50 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Wed, 22 Sep 2010 08:48:50 -0400 Subject: [dba-SQLServer] You'll love this! In-Reply-To: References: Message-ID: <4C99FB32.1020300@colbyconsulting.com> Gustav, I solved it using common table expressions (CTE), which I am growing to love. In my tblOrderData I have a set of fields which I use to hold codes for specific selections. In a previous stored procedure I had set the sequence field of two sets of records to either a 22 or an 11. Using a CTE I created a tLargeZip common table where I pulled all of the Zip5 for records with Sequence = '22', grouped by Zip5 where the count(PK) >= 20. I created another CTE OrderData which joined back to TLargeZips on Zip5, and then updated the Selection to a code '22GTE20' - meaning Sequence 22 with Zip count GTE 20. UPDATE [tblOrderData] SET [Selection] = NULL; With TLargeZips (Zip5) as ( SELECT TOP (100) PERCENT zip5 FROM dbo.vOrder WHERE (Sequence = '22') GROUP BY zip5 HAVING (COUNT(PK) >= 20) ), OrderData (Selection) as ( SELECT Selection from dbo.vOrder INNER JOIN TLargeZips ON dbo.vOrder.Zip5 = TLargeZips.zip5 WHERE (Selection IS NULL) AND (Sequence = '22') ) UPDATE OrderData SET Selection = '22GTE20'; I am using CTEs more and more as it allows me to create logical data sets which can then be manipulated in another sequel statement. Essentially the CTE replaces the temp table that you suggested with an "in memory" table, created on-the-fly. John W. Colby www.ColbyConsulting.com On 9/22/2010 2:23 AM, Gustav Brock wrote: > Hi John > > Why not write the list of those 20+ zip items to a temp table, then join this with your table to update? > > /gustav > > >>>> jwcolby at colbyconsulting.com 22-09-2010 06:28>>> > My client asked me to sum (give him a count of) the records in zips where the count in each zip is> 20. > > No problem, do a count group by zip, where the count> 20. This gives me a list of such zip codes. > Then build a sum on that base query. > > No comes the not so cool part. I will have to select my records only from zips where the count of > records in a zip is>=20. > > This may be easy, join the base query back to the table and update a field to a code, then select > from the table where that code is set. > > My fear is that the join will make the view non-updateable. Which will leave me using a cursor? > > We shall see. > From Gustav at cactus.dk Wed Sep 22 07:59:57 2010 From: Gustav at cactus.dk (Gustav Brock) Date: Wed, 22 Sep 2010 14:59:57 +0200 Subject: [dba-SQLServer] You'll love this! Message-ID: Ho John Oh. Looks great! I had no idea of CTEs ... /gustav >>> jwcolby at colbyconsulting.com 22-09-2010 14:48 >>> Gustav, I solved it using common table expressions (CTE), which I am growing to love. In my tblOrderData I have a set of fields which I use to hold codes for specific selections. In a previous stored procedure I had set the sequence field of two sets of records to either a 22 or an 11. Using a CTE I created a tLargeZip common table where I pulled all of the Zip5 for records with Sequence = '22', grouped by Zip5 where the count(PK) >= 20. I created another CTE OrderData which joined back to TLargeZips on Zip5, and then updated the Selection to a code '22GTE20' - meaning Sequence 22 with Zip count GTE 20. UPDATE [tblOrderData] SET [Selection] = NULL; With TLargeZips (Zip5) as ( SELECT TOP (100) PERCENT zip5 FROM dbo.vOrder WHERE (Sequence = '22') GROUP BY zip5 HAVING (COUNT(PK) >= 20) ), OrderData (Selection) as ( SELECT Selection from dbo.vOrder INNER JOIN TLargeZips ON dbo.vOrder.Zip5 = TLargeZips.zip5 WHERE (Selection IS NULL) AND (Sequence = '22') ) UPDATE OrderData SET Selection = '22GTE20'; I am using CTEs more and more as it allows me to create logical data sets which can then be manipulated in another sequel statement. Essentially the CTE replaces the temp table that you suggested with an "in memory" table, created on-the-fly. John W. Colby www.ColbyConsulting.com On 9/22/2010 2:23 AM, Gustav Brock wrote: > Hi John > > Why not write the list of those 20+ zip items to a temp table, then join this with your table to update? > > /gustav > > >>>> jwcolby at colbyconsulting.com 22-09-2010 06:28>>> > My client asked me to sum (give him a count of) the records in zips where the count in each zip is> 20. > > No problem, do a count group by zip, where the count> 20. This gives me a list of such zip codes. > Then build a sum on that base query. > > No comes the not so cool part. I will have to select my records only from zips where the count of > records in a zip is>=20. > > This may be easy, join the base query back to the table and update a field to a code, then select > from the table where that code is set. > > My fear is that the join will make the view non-updateable. Which will leave me using a cursor? > > We shall see. From fhtapia at gmail.com Wed Sep 22 11:55:27 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Wed, 22 Sep 2010 09:55:27 -0700 Subject: [dba-SQLServer] OT:DFSR db's In-Reply-To: References: Message-ID: Because you are going to have two sites looking at the same data you will want to look into "Merge" replication I think that makes the most sense. the Database Mirroring is more for disaster recovery than independent sites. Merge replication will still have collisions if both sites work on updating similar records and there will need to be someone in the middle authorizing which collision of data is the winner... Review SqlServerCentral.com for merge replication articles OR review the books online to give you a base idea on how it works. dbjournal.com also has great articles as well... -Francisco http://bit.ly/sqlthis | Tsql and More... On Wed, Sep 22, 2010 at 4:27 AM, Jean-Paul natola wrote: > > we have a field office with less than reliable connectivity, and it would > be nice if they can work locally on th DB when the internet is down > > > > > > > > > > > > Jean-Paul Natola > > > > > > > From: fhtapia at gmail.com > > To: dba-sqlserver at databaseadvisors.com > > Date: Tue, 21 Sep 2010 19:08:07 -0700 > > CC: dba-sqlserver at databaseadvisors.com > > Subject: Re: [dba-SQLServer] OT:DFSR db's > > > > Have not... I'm not sure what you are attempt to do, do you just want > > high availability? If so SQL has built in tools for that such as > > replication, transaction log shipping, and database mirroring to name > > a few. You can also do a full blown clustered environment. > > > > Sent from my mobile > > > > On Sep 21, 2010, at 1:17 PM, Jean-Paul natola > > wrote: > > > > > > > > Does anyone know , or has tried windows DFSR functionality with a > > > sql db? > > > > > > TIA > > > > > > J > > > > > > _______________________________________________ > > > dba-SQLServer mailing list > > > dba-SQLServer at databaseadvisors.com > > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > > http://www.databaseadvisors.com > > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From jnatola at hotmail.com Wed Sep 22 15:14:25 2010 From: jnatola at hotmail.com (Jean-Paul natola) Date: Wed, 22 Sep 2010 16:14:25 -0400 Subject: [dba-SQLServer] OT:DFSR db's In-Reply-To: References: , , , Message-ID: Thanks for the tip, quick question if I may before i start research, is this possible with the express or compact, or does it have to be Full Version of SQL, becuase I know i dont have the funding for 8 sql licenses. tia > From: fhtapia at gmail.com > Date: Wed, 22 Sep 2010 09:55:27 -0700 > To: dba-sqlserver at databaseadvisors.com > Subject: Re: [dba-SQLServer] OT:DFSR db's > > Because you are going to have two sites looking at the same data you will > want to look into "Merge" replication I think that makes the most sense. > the Database Mirroring is more for disaster recovery than independent > sites. Merge replication will still have collisions if both sites work on > updating similar records and there will need to be someone in the middle > authorizing which collision of data is the winner... > > Review SqlServerCentral.com for merge replication articles OR review the > books online to give you a base idea on how it works. dbjournal.com also > has great articles as well... > > -Francisco > http://bit.ly/sqlthis | Tsql and More... > > > > > On Wed, Sep 22, 2010 at 4:27 AM, Jean-Paul natola wrote: > > > > > we have a field office with less than reliable connectivity, and it would > > be nice if they can work locally on th DB when the internet is down > > > > > > > > > > > > > > > > > > > > > > > > Jean-Paul Natola > > > > > > > > > > > > > From: fhtapia at gmail.com > > > To: dba-sqlserver at databaseadvisors.com > > > Date: Tue, 21 Sep 2010 19:08:07 -0700 > > > CC: dba-sqlserver at databaseadvisors.com > > > Subject: Re: [dba-SQLServer] OT:DFSR db's > > > > > > Have not... I'm not sure what you are attempt to do, do you just want > > > high availability? If so SQL has built in tools for that such as > > > replication, transaction log shipping, and database mirroring to name > > > a few. You can also do a full blown clustered environment. > > > > > > Sent from my mobile > > > > > > On Sep 21, 2010, at 1:17 PM, Jean-Paul natola > > > wrote: > > > > > > > > > > > Does anyone know , or has tried windows DFSR functionality with a > > > > sql db? > > > > > > > > TIA > > > > > > > > J > > > > > > > > _______________________________________________ > > > > dba-SQLServer mailing list > > > > dba-SQLServer at databaseadvisors.com > > > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > > > http://www.databaseadvisors.com > > > > > > > _______________________________________________ > > > dba-SQLServer mailing list > > > dba-SQLServer at databaseadvisors.com > > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > > http://www.databaseadvisors.com > > > > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From paul.hartland at googlemail.com Thu Sep 23 09:53:25 2010 From: paul.hartland at googlemail.com (Paul Hartland) Date: Thu, 23 Sep 2010 15:53:25 +0100 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors Message-ID: To all, We have employees that can do more than one job in a day on our database, when an employee is assigned to a job, the application takes the persons home postcode and the jobs location postcode and stores them in a table, we are now creating an estimated mileage. I can do this based on the postcodes supplied but it will be inaccurate as the start postcodes will always be the employees home, when the start postcode for the second job should really be the postcode of the previous job location. At the moment I have a select query which returns results for an employee where they are doing more than one job in a day, result example below: PersonID Date StartCode EndCode 111111 29/09/2010 AA1 1AA BB1 1BB 111111 29/09/2010 AA2 2AA BB2 2BB 111111 29/09/2010 AA3 3AA BB3 3BB 111111 29/09/2010 AA4 4AA BB4 4BB I am thinking of putting these results into a cursor and looping through the records updating the startcode with the previous records endcode, so that AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc Is this the best way to do this, or could someone point me in a better direction please. Many thanks in advance for all your help. -- Paul Hartland paul.hartland at googlemail.com From jwcolby at colbyconsulting.com Thu Sep 23 10:07:27 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 11:07:27 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: Message-ID: <4C9B6D2F.7040209@colbyconsulting.com> Paul, As long as this isn't doing tens of thousands of these at a shot I think this is the perfect application of a cursor. Cursors are slow but who cares if it is just a dozen at a time? John W. Colby www.ColbyConsulting.com On 9/23/2010 10:53 AM, Paul Hartland wrote: > To all, > > We have employees that can do more than one job in a day on our database, > when an employee is assigned to a job, the application takes the persons > home postcode and the jobs location postcode and stores them in a table, we > are now creating an estimated mileage. I can do this based on the postcodes > supplied but it will be inaccurate as the start postcodes will always be the > employees home, when the start postcode for the second job should really be > the postcode of the previous job location. > > At the moment I have a select query which returns results for an employee > where they are doing more than one job in a day, result example below: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > > I am thinking of putting these results into a cursor and looping through the > records updating the startcode with the previous records endcode, so that > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > Is this the best way to do this, or could someone point me in a better > direction please. > > Many thanks in advance for all your help. > From fhtapia at gmail.com Thu Sep 23 10:15:43 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 08:15:43 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9B6D2F.7040209@colbyconsulting.com> References: <4C9B6D2F.7040209@colbyconsulting.com> Message-ID: i disagree... you should use cursors if there is no other viable way to solve your problem. In your scenario, you can certainly build up a subquery to join to based on date / time stamp that would yield the previous job's location's postcode. you will need to somehow know which job was 1st 2nd etc. in order for your join to work in either scenario (subquery or cursor). -Francisco http://bit.ly/sqlthis | Tsql and More... On Thu, Sep 23, 2010 at 8:07 AM, jwcolby wrote: > Paul, > > As long as this isn't doing tens of thousands of these at a shot I think > this is the perfect > application of a cursor. > > Cursors are slow but who cares if it is just a dozen at a time? > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 10:53 AM, Paul Hartland wrote: > > To all, > > > > We have employees that can do more than one job in a day on our database, > > when an employee is assigned to a job, the application takes the persons > > home postcode and the jobs location postcode and stores them in a table, > we > > are now creating an estimated mileage. I can do this based on the > postcodes > > supplied but it will be inaccurate as the start postcodes will always be > the > > employees home, when the start postcode for the second job should really > be > > the postcode of the previous job location. > > > > At the moment I have a select query which returns results for an employee > > where they are doing more than one job in a day, result example below: > > > > PersonID Date StartCode EndCode > > 111111 29/09/2010 AA1 1AA BB1 1BB > > 111111 29/09/2010 AA2 2AA BB2 2BB > > 111111 29/09/2010 AA3 3AA BB3 3BB > > 111111 29/09/2010 AA4 4AA BB4 4BB > > > > I am thinking of putting these results into a cursor and looping through > the > > records updating the startcode with the previous records endcode, so that > > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > > > Is this the best way to do this, or could someone point me in a better > > direction please. > > > > Many thanks in advance for all your help. > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From fhtapia at gmail.com Thu Sep 23 10:19:02 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 08:19:02 -0700 Subject: [dba-SQLServer] OT:DFSR db's In-Reply-To: References: Message-ID: with express you can perform merge or transactional replication however your express installs can only be subscribers.. you still need at least ONE standard/or enterprise install... -Francisco http://bit.ly/sqlthis | Tsql and More... On Wed, Sep 22, 2010 at 1:14 PM, Jean-Paul natola wrote: > is this possible with the express or compact, or does it have to be Full > Version of SQL, becuase I know i dont have the funding for 8 sql licenses. From marklbreen at gmail.com Thu Sep 23 10:24:07 2010 From: marklbreen at gmail.com (Mark Breen) Date: Thu, 23 Sep 2010 16:24:07 +0100 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9B6D2F.7040209@colbyconsulting.com> References: <4C9B6D2F.7040209@colbyconsulting.com> Message-ID: Hello Paul, I agree with John also. After years of being told to keep away from Cursors, I use them a little and find them quick and easy to work with. You can probably handle your (CurrentRec-1) situation with straight SQL, but the nice thing with cursors is they are easy to debug, and you can even use print statements to record progress. Remember, in the oracle world, they use cursors extensively and are proud to do so. I have always been perplexed at the these two approaches in MS SQL versus Oracle. While writing this email, I see that Francisco has replied also. I know that Francisco is one of our best SQL guru's, so in my opinion, his opinion trumps mine, but nevertheless, my comments remain, and John's comments about performance remain also. Maybe another way to say this is a) the cursor approach will take you about 15 minutes to write and another 5-10 to test it, if it does not work first time. b) if you can write a set theory based approach i.e. straight sql statement, in around that time, then do it with SQL, otherwise use a quick cursor. Good luck and enjoy it which ever approach you take, Mark Enjoy Mark On 23 September 2010 16:07, jwcolby wrote: > Paul, > > As long as this isn't doing tens of thousands of these at a shot I think > this is the perfect > application of a cursor. > > Cursors are slow but who cares if it is just a dozen at a time? > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 10:53 AM, Paul Hartland wrote: > > To all, > > > > We have employees that can do more than one job in a day on our database, > > when an employee is assigned to a job, the application takes the persons > > home postcode and the jobs location postcode and stores them in a table, > we > > are now creating an estimated mileage. I can do this based on the > postcodes > > supplied but it will be inaccurate as the start postcodes will always be > the > > employees home, when the start postcode for the second job should really > be > > the postcode of the previous job location. > > > > At the moment I have a select query which returns results for an employee > > where they are doing more than one job in a day, result example below: > > > > PersonID Date StartCode EndCode > > 111111 29/09/2010 AA1 1AA BB1 1BB > > 111111 29/09/2010 AA2 2AA BB2 2BB > > 111111 29/09/2010 AA3 3AA BB3 3BB > > 111111 29/09/2010 AA4 4AA BB4 4BB > > > > I am thinking of putting these results into a cursor and looping through > the > > records updating the startcode with the previous records endcode, so that > > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > > > Is this the best way to do this, or could someone point me in a better > > direction please. > > > > Many thanks in advance for all your help. > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From jwcolby at colbyconsulting.com Thu Sep 23 10:29:31 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 11:29:31 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <4C9B6D2F.7040209@colbyconsulting.com> Message-ID: <4C9B725B.1070708@colbyconsulting.com> >you should use cursors if there is no other viable way to solve your problem. And why is that? My take is "you should NOT use cursors if it is going to severely impact the server". I am all about learning new things Francisco, but it doesn't make sense to me to do it the "hard way" (which you solution is for SQL language babies like myself) when there is a solution that I can understand, program and debug easily. Cursors are just a tool, one which we understand. John W. Colby www.ColbyConsulting.com On 9/23/2010 11:15 AM, Francisco Tapia wrote: > i disagree... > you should use cursors if there is no other viable way to solve your > problem. In your scenario, you can certainly build up a subquery to join to > based on date / time stamp that would yield the previous job's location's > postcode. you will need to somehow know which job was 1st 2nd etc. in order > for your join to work in either scenario (subquery or cursor). > > > > > -Francisco > http://bit.ly/sqlthis | Tsql and More... > > > > > On Thu, Sep 23, 2010 at 8:07 AM, jwcolbywrote: > >> Paul, >> >> As long as this isn't doing tens of thousands of these at a shot I think >> this is the perfect >> application of a cursor. >> >> Cursors are slow but who cares if it is just a dozen at a time? >> >> John W. Colby >> www.ColbyConsulting.com >> >> On 9/23/2010 10:53 AM, Paul Hartland wrote: >>> To all, >>> >>> We have employees that can do more than one job in a day on our database, >>> when an employee is assigned to a job, the application takes the persons >>> home postcode and the jobs location postcode and stores them in a table, >> we >>> are now creating an estimated mileage. I can do this based on the >> postcodes >>> supplied but it will be inaccurate as the start postcodes will always be >> the >>> employees home, when the start postcode for the second job should really >> be >>> the postcode of the previous job location. >>> >>> At the moment I have a select query which returns results for an employee >>> where they are doing more than one job in a day, result example below: >>> >>> PersonID Date StartCode EndCode >>> 111111 29/09/2010 AA1 1AA BB1 1BB >>> 111111 29/09/2010 AA2 2AA BB2 2BB >>> 111111 29/09/2010 AA3 3AA BB3 3BB >>> 111111 29/09/2010 AA4 4AA BB4 4BB >>> >>> I am thinking of putting these results into a cursor and looping through >> the >>> records updating the startcode with the previous records endcode, so that >>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >>> >>> Is this the best way to do this, or could someone point me in a better >>> direction please. >>> >>> Many thanks in advance for all your help. >>> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From ab-mi at post3.tele.dk Thu Sep 23 11:13:32 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Thu, 23 Sep 2010 18:13:32 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: Message-ID: <2C102B923D2645A6ADC59AB978ACEF96@abpc> Paul, You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): WITH cte (RowNumber, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber > 1 AND cte.RowNumber = cte2.RowNumber+1) Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland Sendt: 23. september 2010 16:53 Til: SQLServerList Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors To all, We have employees that can do more than one job in a day on our database, when an employee is assigned to a job, the application takes the persons home postcode and the jobs location postcode and stores them in a table, we are now creating an estimated mileage. I can do this based on the postcodes supplied but it will be inaccurate as the start postcodes will always be the employees home, when the start postcode for the second job should really be the postcode of the previous job location. At the moment I have a select query which returns results for an employee where they are doing more than one job in a day, result example below: PersonID Date StartCode EndCode 111111 29/09/2010 AA1 1AA BB1 1BB 111111 29/09/2010 AA2 2AA BB2 2BB 111111 29/09/2010 AA3 3AA BB3 3BB 111111 29/09/2010 AA4 4AA BB4 4BB I am thinking of putting these results into a cursor and looping through the records updating the startcode with the previous records endcode, so that AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc Is this the best way to do this, or could someone point me in a better direction please. Many thanks in advance for all your help. -- Paul Hartland paul.hartland at googlemail.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From fhtapia at gmail.com Thu Sep 23 12:08:46 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 10:08:46 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9B725B.1070708@colbyconsulting.com> References: <4C9B6D2F.7040209@colbyconsulting.com> <4C9B725B.1070708@colbyconsulting.com> Message-ID: John, the problem with stating that cursors are "ok" to use as a general rule is that people DO start using them and in a heavy way (kind of like bound forms in Access ;-)) in all seriousness, Cursors Will deplete system resources and are generally not very scalable. So what initially started as a small dataset turns into a huge database from hell... additionally, in my personal experience we've removed cursors from nearly all our processes except for certain operations that demand them, the reason was that under certain MS conditions our server would crash for no apparent reason... when we changed a few jobs to run with set based operations and temp tables, we found that these processes not only ran faster, but our uptime increased with our systems making them more reliable. also note that I never said never... of course there are situations that demand a cursor, but those in my opinion are few and far between, this imho does not warrant the use of a cursor at least not yet. -Francisco http://bit.ly/sqlthis | Tsql and More... On Thu, Sep 23, 2010 at 8:29 AM, jwcolby wrote: > >you should use cursors if there is no other viable way to solve your > problem. > > And why is that? > > My take is "you should NOT use cursors if it is going to severely impact > the server". > > I am all about learning new things Francisco, but it doesn't make sense to > me to do it the "hard > way" (which you solution is for SQL language babies like myself) when there > is a solution that I can > understand, program and debug easily. > > Cursors are just a tool, one which we understand. > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 11:15 AM, Francisco Tapia wrote: > > i disagree... > > you should use cursors if there is no other viable way to solve your > > problem. In your scenario, you can certainly build up a subquery to join > to > > based on date / time stamp that would yield the previous job's location's > > postcode. you will need to somehow know which job was 1st 2nd etc. in > order > > for your join to work in either scenario (subquery or cursor). > > > > > > > > > > -Francisco > > http://bit.ly/sqlthis | Tsql and More... > > > > > > > > > > On Thu, Sep 23, 2010 at 8:07 AM, jwcolby >wrote: > > > >> Paul, > >> > >> As long as this isn't doing tens of thousands of these at a shot I think > >> this is the perfect > >> application of a cursor. > >> > >> Cursors are slow but who cares if it is just a dozen at a time? > >> > >> John W. Colby > >> www.ColbyConsulting.com > >> > >> On 9/23/2010 10:53 AM, Paul Hartland wrote: > >>> To all, > >>> > >>> We have employees that can do more than one job in a day on our > database, > >>> when an employee is assigned to a job, the application takes the > persons > >>> home postcode and the jobs location postcode and stores them in a > table, > >> we > >>> are now creating an estimated mileage. I can do this based on the > >> postcodes > >>> supplied but it will be inaccurate as the start postcodes will always > be > >> the > >>> employees home, when the start postcode for the second job should > really > >> be > >>> the postcode of the previous job location. > >>> > >>> At the moment I have a select query which returns results for an > employee > >>> where they are doing more than one job in a day, result example below: > >>> > >>> PersonID Date StartCode EndCode > >>> 111111 29/09/2010 AA1 1AA BB1 1BB > >>> 111111 29/09/2010 AA2 2AA BB2 2BB > >>> 111111 29/09/2010 AA3 3AA BB3 3BB > >>> 111111 29/09/2010 AA4 4AA BB4 4BB > >>> > >>> I am thinking of putting these results into a cursor and looping > through > >> the > >>> records updating the startcode with the previous records endcode, so > that > >>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > >>> > >>> Is this the best way to do this, or could someone point me in a better > >>> direction please. > >>> > >>> Many thanks in advance for all your help. > >>> > >> _______________________________________________ > >> dba-SQLServer mailing list > >> dba-SQLServer at databaseadvisors.com > >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > >> http://www.databaseadvisors.com > >> > >> > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From fhtapia at gmail.com Thu Sep 23 12:09:02 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 10:09:02 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <2C102B923D2645A6ADC59AB978ACEF96@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> Message-ID: excellent example... -Francisco http://bit.ly/sqlthis | Tsql and More... On Thu, Sep 23, 2010 at 9:13 AM, Asger Blond wrote: > Paul, > You can use a subquery to make this update. Following example uses a Common > Table Expression and the Rank function Row_Number(): > > WITH cte (RowNumber, StartCode, EndCode) AS > (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM > YourQuery) > UPDATE cte SET StartCode = > (SELECT EndCode FROM cte AS cte2 > WHERE cte.RowNumber > 1 AND cte.RowNumber = cte2.RowNumber+1) > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: > dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland > Sendt: 23. september 2010 16:53 > Til: SQLServerList > Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > To all, > > We have employees that can do more than one job in a day on our database, > when an employee is assigned to a job, the application takes the persons > home postcode and the jobs location postcode and stores them in a table, we > are now creating an estimated mileage. I can do this based on the > postcodes > supplied but it will be inaccurate as the start postcodes will always be > the > employees home, when the start postcode for the second job should really be > the postcode of the previous job location. > > At the moment I have a select query which returns results for an employee > where they are doing more than one job in a day, result example below: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > > I am thinking of putting these results into a cursor and looping through > the > records updating the startcode with the previous records endcode, so that > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > Is this the best way to do this, or could someone point me in a better > direction please. > > Many thanks in advance for all your help. > > -- > Paul Hartland > paul.hartland at googlemail.com > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From fhtapia at gmail.com Thu Sep 23 12:23:43 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 10:23:43 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> Message-ID: for those of you who might want to take an extra read on sql server cursors (non BOL) http://www.sqlteam.com/article/cursors-an-overview -Francisco http://bit.ly/sqlthis | Tsql and More... On Thu, Sep 23, 2010 at 9:13 AM, Asger Blond wrote: > >> Paul, >> You can use a subquery to make this update. Following example uses a >> Common Table Expression and the Rank function Row_Number(): >> >> WITH cte (RowNumber, StartCode, EndCode) AS >> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM >> YourQuery) >> UPDATE cte SET StartCode = >> (SELECT EndCode FROM cte AS cte2 >> WHERE cte.RowNumber > 1 AND cte.RowNumber = cte2.RowNumber+1) >> >> Asger >> >> -----Oprindelig meddelelse----- >> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: >> dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland >> Sendt: 23. september 2010 16:53 >> Til: SQLServerList >> Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >> >> To all, >> >> We have employees that can do more than one job in a day on our database, >> when an employee is assigned to a job, the application takes the persons >> home postcode and the jobs location postcode and stores them in a table, >> we >> are now creating an estimated mileage. I can do this based on the >> postcodes >> supplied but it will be inaccurate as the start postcodes will always be >> the >> employees home, when the start postcode for the second job should really >> be >> the postcode of the previous job location. >> >> At the moment I have a select query which returns results for an employee >> where they are doing more than one job in a day, result example below: >> >> PersonID Date StartCode EndCode >> 111111 29/09/2010 AA1 1AA BB1 1BB >> 111111 29/09/2010 AA2 2AA BB2 2BB >> 111111 29/09/2010 AA3 3AA BB3 3BB >> 111111 29/09/2010 AA4 4AA BB4 4BB >> >> I am thinking of putting these results into a cursor and looping through >> the >> records updating the startcode with the previous records endcode, so that >> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >> >> Is this the best way to do this, or could someone point me in a better >> direction please. >> >> Many thanks in advance for all your help. >> >> -- >> Paul Hartland >> paul.hartland at googlemail.com >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > From ab-mi at post3.tele.dk Thu Sep 23 12:57:44 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Thu, 23 Sep 2010 19:57:44 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <2C102B923D2645A6ADC59AB978ACEF96@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> Message-ID: <21BBA9420A09438A8A1F96C389C5F44E@abpc> Paul, Probably you want to perform the updates inside each group of PersonID's and prevent a NULL for StartCode of the first row in each group. To group PersonID's and to prevent updates of the first row in each PersonID group use this query: WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber = cte2.RowNumber+1 AND cte.PersonID = cte2.PersonID) WHERE RowNumber > 1 Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Asger Blond Sendt: 23. september 2010 18:14 Til: 'Discussion concerning MS SQL Server' Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors Paul, You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): WITH cte (RowNumber, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber > 1 AND cte.RowNumber = cte2.RowNumber+1) Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland Sendt: 23. september 2010 16:53 Til: SQLServerList Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors To all, We have employees that can do more than one job in a day on our database, when an employee is assigned to a job, the application takes the persons home postcode and the jobs location postcode and stores them in a table, we are now creating an estimated mileage. I can do this based on the postcodes supplied but it will be inaccurate as the start postcodes will always be the employees home, when the start postcode for the second job should really be the postcode of the previous job location. At the moment I have a select query which returns results for an employee where they are doing more than one job in a day, result example below: PersonID Date StartCode EndCode 111111 29/09/2010 AA1 1AA BB1 1BB 111111 29/09/2010 AA2 2AA BB2 2BB 111111 29/09/2010 AA3 3AA BB3 3BB 111111 29/09/2010 AA4 4AA BB4 4BB I am thinking of putting these results into a cursor and looping through the records updating the startcode with the previous records endcode, so that AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc Is this the best way to do this, or could someone point me in a better direction please. Many thanks in advance for all your help. -- Paul Hartland paul.hartland at googlemail.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at colbyconsulting.com Thu Sep 23 13:53:56 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 14:53:56 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <2C102B923D2645A6ADC59AB978ACEF96@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> Message-ID: <4C9BA244.9090904@colbyconsulting.com> OK Asger (and Francisco), but this is a perfect example of... WTF over? I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code does. If you are going to recommend code like this you need to tell us how it works, otherwise I for one will go "ok, now where did I put that cursor..." John W. Colby www.ColbyConsulting.com On 9/23/2010 12:13 PM, Asger Blond wrote: > Paul, > You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): > > WITH cte (RowNumber, StartCode, EndCode) AS > (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) > UPDATE cte SET StartCode = > (SELECT EndCode FROM cte AS cte2 > WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland > Sendt: 23. september 2010 16:53 > Til: SQLServerList > Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > To all, > > We have employees that can do more than one job in a day on our database, > when an employee is assigned to a job, the application takes the persons > home postcode and the jobs location postcode and stores them in a table, we > are now creating an estimated mileage. I can do this based on the postcodes > supplied but it will be inaccurate as the start postcodes will always be the > employees home, when the start postcode for the second job should really be > the postcode of the previous job location. > > At the moment I have a select query which returns results for an employee > where they are doing more than one job in a day, result example below: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > > I am thinking of putting these results into a cursor and looping through the > records updating the startcode with the previous records endcode, so that > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > Is this the best way to do this, or could someone point me in a better > direction please. > > Many thanks in advance for all your help. > From jwcolby at colbyconsulting.com Thu Sep 23 13:58:37 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 14:58:37 -0400 Subject: [dba-SQLServer] Clustered Index first or last Message-ID: <4C9BA35D.8060504@colbyconsulting.com> When I build my 65 million record table, I almost always create a clustered index, with the key being the PK which is an autonumber integer. In the past I always inserted the records, then built the index. However if the records are inserted in sorted order on the PKID, which is in ascending order, is there any reason not to create the clustered index and then insert the records. Since the key is ascending each new record should go at the end, no shuffling around to put things in the middle. It seems I would actually *save* time doing it that way. Yes, or no? -- John W. Colby www.ColbyConsulting.com From fhtapia at gmail.com Thu Sep 23 15:58:15 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 13:58:15 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BA244.9090904@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> Message-ID: John, in your environment and because you are a single user, your experiences are more unique than most people who work with SQL Server, you can make decisions to use cursors on a whim that will impact your server performance, however, most other SQL programmers and DBA's generally need to address server performance and availability. Because of that I recommend that you (John) continue to use the tool of your choice, however, when offering advice on cursors, would refrain from stating when a cursor is valid. I would hate to have someone's server crash or become unresponsive when it deals with a community of users. it's also quite another thing to state that information wasn't provided, Asger, clearly stated that his example was a subquery example with common table expressions (CTE) and Rank function, all terms that can be quickly be researched at on Books online (BOL). -Francisco http://bit.ly/sqlthis | Tsql and More... On Thu, Sep 23, 2010 at 11:53 AM, jwcolby wrote: > OK Asger (and Francisco), but this is a perfect example of... WTF over? > > I for one haven't a clue what this code does, nor do I have a clue how to > figure out what this code > does. > > If you are going to recommend code like this you need to tell us how it > works, otherwise I for one > will go "ok, now where did I put that cursor..." > > > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 12:13 PM, Asger Blond wrote: > > Paul, > > You can use a subquery to make this update. Following example uses a > Common Table Expression and the Rank function Row_Number(): > > > > WITH cte (RowNumber, StartCode, EndCode) AS > > (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM > YourQuery) > > UPDATE cte SET StartCode = > > (SELECT EndCode FROM cte AS cte2 > > WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) > > > > Asger > > > > -----Oprindelig meddelelse----- > > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: > dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland > > Sendt: 23. september 2010 16:53 > > Til: SQLServerList > > Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > > > To all, > > > > We have employees that can do more than one job in a day on our database, > > when an employee is assigned to a job, the application takes the persons > > home postcode and the jobs location postcode and stores them in a table, > we > > are now creating an estimated mileage. I can do this based on the > postcodes > > supplied but it will be inaccurate as the start postcodes will always be > the > > employees home, when the start postcode for the second job should really > be > > the postcode of the previous job location. > > > > At the moment I have a select query which returns results for an employee > > where they are doing more than one job in a day, result example below: > > > > PersonID Date StartCode EndCode > > 111111 29/09/2010 AA1 1AA BB1 1BB > > 111111 29/09/2010 AA2 2AA BB2 2BB > > 111111 29/09/2010 AA3 3AA BB3 3BB > > 111111 29/09/2010 AA4 4AA BB4 4BB > > > > I am thinking of putting these results into a cursor and looping through > the > > records updating the startcode with the previous records endcode, so that > > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > > > Is this the best way to do this, or could someone point me in a better > > direction please. > > > > Many thanks in advance for all your help. > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From ab-mi at post3.tele.dk Thu Sep 23 16:12:48 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Thu, 23 Sep 2010 23:12:48 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BA244.9090904@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> Message-ID: <0312DC43470649218CACAB408333CE62@abpc> John, > a clue what this code does It simply does what Paul was asking for. If answering a question with an example of solution is inappropriate then I don't know what the purpose of this list is. Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby Sendt: 23. september 2010 20:54 Til: Discussion concerning MS SQL Server Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors OK Asger (and Francisco), but this is a perfect example of... WTF over? I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code does. If you are going to recommend code like this you need to tell us how it works, otherwise I for one will go "ok, now where did I put that cursor..." John W. Colby www.ColbyConsulting.com On 9/23/2010 12:13 PM, Asger Blond wrote: > Paul, > You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): > > WITH cte (RowNumber, StartCode, EndCode) AS > (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) > UPDATE cte SET StartCode = > (SELECT EndCode FROM cte AS cte2 > WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland > Sendt: 23. september 2010 16:53 > Til: SQLServerList > Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > To all, > > We have employees that can do more than one job in a day on our database, > when an employee is assigned to a job, the application takes the persons > home postcode and the jobs location postcode and stores them in a table, we > are now creating an estimated mileage. I can do this based on the postcodes > supplied but it will be inaccurate as the start postcodes will always be the > employees home, when the start postcode for the second job should really be > the postcode of the previous job location. > > At the moment I have a select query which returns results for an employee > where they are doing more than one job in a day, result example below: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > > I am thinking of putting these results into a cursor and looping through the > records updating the startcode with the previous records endcode, so that > AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc > > Is this the best way to do this, or could someone point me in a better > direction please. > > Many thanks in advance for all your help. > _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at colbyconsulting.com Thu Sep 23 18:06:01 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 19:06:01 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <0312DC43470649218CACAB408333CE62@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <0312DC43470649218CACAB408333CE62@abpc> Message-ID: <4C9BDD59.5060403@colbyconsulting.com> >> a clue what this code does I am not playing games, thanks. > If answering a question with an example of solution is inappropriate LOL, Asger I wasn't saying that at all. Providing an example solution is fine, providing knowledge is better. >then I don't know what the purpose of this list is. Helping people *learn*. A paragraph or two would have allowed those for who SQL is not our first language to begin to understand what you are doing. It is not like you are just doing CTEs (I am starting to use them, I love them, but it took me awhile to get comfortable with just that piece). You are also throwing in Over (I have not use that before) and row numbers (I have not used that before). All things that it would be wicked to know! But it reaches the point where there is so much new stuff that I can't get a handle on the what you are trying to do. If we understood the example why would we need the example? Could I go figure it out? Of course. Or I could throw together a cursor and move on. I would truly like to understand what you are doing. OTOH today I re-engineered a slew of C# code to break a bunch of updates (on 65 million record tables) into batches, wrapped in transactions. I also worked on a client's Access database to add filtering to a form, and I worked on another client's database to investigate a slowdown that occurred (probably hardware) that occurred back in July and they are just now complaining about. I did three zip code counts for a third client and after a 10 hour day I am not done yet. Without somebody who cares enough to explain their solution, the solution will go in the shitcan. Which would be a pity, but I don't have the time to spend hours figuring out something that I could learn in 5 minutes if you were kind enough to explain. John W. Colby www.ColbyConsulting.com On 9/23/2010 5:12 PM, Asger Blond wrote: > John, >> a clue what this code does > It simply does what Paul was asking for. > If answering a question with an example of solution is inappropriate then I don't know what the purpose of this list is. > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 23. september 2010 20:54 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > OK Asger (and Francisco), but this is a perfect example of... WTF over? > > I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code > does. > > If you are going to recommend code like this you need to tell us how it works, otherwise I for one > will go "ok, now where did I put that cursor..." > > > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 12:13 PM, Asger Blond wrote: >> Paul, >> You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): >> >> WITH cte (RowNumber, StartCode, EndCode) AS >> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) >> UPDATE cte SET StartCode = >> (SELECT EndCode FROM cte AS cte2 >> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >> >> Asger From jwcolby at colbyconsulting.com Thu Sep 23 18:19:36 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 19:19:36 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> Message-ID: <4C9BE088.9040805@colbyconsulting.com> LOL, Francisco you crack me up sometimes. From the SQL Server Team... (and YOU) http://www.sqlteam.com/article/cursors-an-overview Where in there does it say anything about unstable or unresponsive servers. In the real world (yours too I assume) we often need to just get it done and move on. Throwing out a solution *with no explanation at all* that is obviously classy, but a bit dense, to someone asking if a cursor is OK, is... less than helpful. I understand from past experiences with this list that there is a very definite "read BOL" mentality. If it don't hurt, it ain't good for ya. ;) So Francisco, I will pretty much make whatever damned recommendations I want. Including "use a cursor when it fits" which is what I said originally. It's a tool for God's sake. Provided by Microsoft for God's sake. Discussed in a blog by none other than SQLTeam.com for God's sake. If you have a problem with them, go bitch at somebody over there. John W. Colby www.ColbyConsulting.com On 9/23/2010 4:58 PM, Francisco Tapia wrote: > John, > in your environment and because you are a single user, your experiences > are more unique than most people who work with SQL Server, you can make > decisions to use cursors on a whim that will impact your server performance, > however, most other SQL programmers and DBA's generally need to address > server performance and availability. Because of that I recommend that you > (John) continue to use the tool of your choice, however, when offering > advice on cursors, would refrain from stating when a cursor is valid. I > would hate to have someone's server crash or become unresponsive when it > deals with a community of users. > > it's also quite another thing to state that information wasn't provided, > Asger, clearly stated that his example was a subquery example with common > table expressions (CTE) and Rank function, all terms that can be quickly be > researched at on Books online (BOL). > > > > -Francisco > http://bit.ly/sqlthis | Tsql and More... > > > > > On Thu, Sep 23, 2010 at 11:53 AM, jwcolbywrote: > >> OK Asger (and Francisco), but this is a perfect example of... WTF over? >> >> I for one haven't a clue what this code does, nor do I have a clue how to >> figure out what this code >> does. >> >> If you are going to recommend code like this you need to tell us how it >> works, otherwise I for one >> will go "ok, now where did I put that cursor..." >> >> >> >> John W. Colby >> www.ColbyConsulting.com >> >> On 9/23/2010 12:13 PM, Asger Blond wrote: >>> Paul, >>> You can use a subquery to make this update. Following example uses a >> Common Table Expression and the Rank function Row_Number(): >>> >>> WITH cte (RowNumber, StartCode, EndCode) AS >>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM >> YourQuery) >>> UPDATE cte SET StartCode = >>> (SELECT EndCode FROM cte AS cte2 >>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>> >>> Asger >>> >>> -----Oprindelig meddelelse----- >>> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: >> dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland >>> Sendt: 23. september 2010 16:53 >>> Til: SQLServerList >>> Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >>> >>> To all, >>> >>> We have employees that can do more than one job in a day on our database, >>> when an employee is assigned to a job, the application takes the persons >>> home postcode and the jobs location postcode and stores them in a table, >> we >>> are now creating an estimated mileage. I can do this based on the >> postcodes >>> supplied but it will be inaccurate as the start postcodes will always be >> the >>> employees home, when the start postcode for the second job should really >> be >>> the postcode of the previous job location. >>> >>> At the moment I have a select query which returns results for an employee >>> where they are doing more than one job in a day, result example below: >>> >>> PersonID Date StartCode EndCode >>> 111111 29/09/2010 AA1 1AA BB1 1BB >>> 111111 29/09/2010 AA2 2AA BB2 2BB >>> 111111 29/09/2010 AA3 3AA BB3 3BB >>> 111111 29/09/2010 AA4 4AA BB4 4BB >>> >>> I am thinking of putting these results into a cursor and looping through >> the >>> records updating the startcode with the previous records endcode, so that >>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >>> >>> Is this the best way to do this, or could someone point me in a better >>> direction please. >>> >>> Many thanks in advance for all your help. >>> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From ab-mi at post3.tele.dk Thu Sep 23 18:25:02 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Fri, 24 Sep 2010 01:25:02 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BDD59.5060403@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com><0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> Message-ID: <97F08F50ACEC491F997EA5363A2F8596@abpc> John, To get you a clue what the code does, and what I did. First, when answering a question I normally make effort recreating the scenario in question. Which was what I did in this case: created a table and a query with the data provided by Paul. If you have done the same you can easily test and play around with the code example - soon you should notice what's going on. Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. I didn't consider your first comment a polite request for explanation, rather a rant just reinforcing that you adhere to cursors. Being more reasonable in your second reply I'll supply explanation using the example from my second posting to Paul: WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber = cte2.RowNumber+1 AND cte.PersonID = cte2.PersonID) WHERE RowNumber > 1 The first part - before UPDATE - creates a Common Table Expression which you know is a kind of virtual table that can be used instead of a temporary table. In this cte I make use of the ranking function ROW_NUMBER() with the qualification PARTION BY PersonID ORDER BY PersonID to get a numbering sequence for each group (or "partition") of PersonID's. Suppose you have the following rows in the query, where I have deliberately added two more rows to Pauls query for the purpose of showing different PersonID's: PersonID Date StartCode EndCode 111111 29/09/2010 AA1 1AA BB1 1BB 111111 29/09/2010 AA2 2AA BB2 2BB 111111 29/09/2010 AA3 3AA BB3 3BB 111111 29/09/2010 AA4 4AA BB4 4BB 222222 29/09/2010 XX1 1XX YY1 1YY 222222 29/09/2010 XX2 2XX YY2 1YY With this recordset you can query the supplied cte using: WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) SELECT * FROM cte The result will be: RowNumber StartCode EndCode 1 AA1 1AA BB1 1BB 2 AA2 2AA BB2 2BB 3 AA3 3AA BB3 3BB 4 AA4 4AA BB4 4BB 1 XX1 1XX YY1 1YY 2 XX2 2XX YY2 2YY Now the last UPDATE part of the code use a correlated subquery that selects the EndCode of the previous row inside each PersonID and use it to replace the value of StartCode. It does this for all rows except for rows where RowNumber = 1. If this last where clause is omitted then all rows with RowNumber = 1 will get a NULL as StartCode. This might actually be wanted and can easily be achieved omitting the clause. Running the code as is will give the following records: PersonID Date StartCode EndCode 11111 29/09/2010 AA1 1AA BB1 1BB 11111 29/09/2010 BB1 1BB BB2 2BB 11111 29/09/2010 BB2 2BB BB3 3BB 11111 29/09/2010 BB3 3BB BB4 4BB 22222 29/09/2010 XX1 1XX YY1 1YY 22222 29/09/2010 YY1 1YY YY2 2YY As I read Paul's question this is what's wanted. PS: If an operation can be achieved both by a cursor and by a set based query then IMO the cursor implementation will be by far the slowest. If you disagree, John, then please supply an example. Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby Sendt: 24. september 2010 01:06 Til: Discussion concerning MS SQL Server Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >> a clue what this code does I am not playing games, thanks. > If answering a question with an example of solution is inappropriate LOL, Asger I wasn't saying that at all. Providing an example solution is fine, providing knowledge is better. >then I don't know what the purpose of this list is. Helping people *learn*. A paragraph or two would have allowed those for who SQL is not our first language to begin to understand what you are doing. It is not like you are just doing CTEs (I am starting to use them, I love them, but it took me awhile to get comfortable with just that piece). You are also throwing in Over (I have not use that before) and row numbers (I have not used that before). All things that it would be wicked to know! But it reaches the point where there is so much new stuff that I can't get a handle on the what you are trying to do. If we understood the example why would we need the example? Could I go figure it out? Of course. Or I could throw together a cursor and move on. I would truly like to understand what you are doing. OTOH today I re-engineered a slew of C# code to break a bunch of updates (on 65 million record tables) into batches, wrapped in transactions. I also worked on a client's Access database to add filtering to a form, and I worked on another client's database to investigate a slowdown that occurred (probably hardware) that occurred back in July and they are just now complaining about. I did three zip code counts for a third client and after a 10 hour day I am not done yet. Without somebody who cares enough to explain their solution, the solution will go in the shitcan. Which would be a pity, but I don't have the time to spend hours figuring out something that I could learn in 5 minutes if you were kind enough to explain. John W. Colby www.ColbyConsulting.com On 9/23/2010 5:12 PM, Asger Blond wrote: > John, >> a clue what this code does > It simply does what Paul was asking for. > If answering a question with an example of solution is inappropriate then I don't know what the purpose of this list is. > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 23. september 2010 20:54 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > OK Asger (and Francisco), but this is a perfect example of... WTF over? > > I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code > does. > > If you are going to recommend code like this you need to tell us how it works, otherwise I for one > will go "ok, now where did I put that cursor..." > > > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 12:13 PM, Asger Blond wrote: >> Paul, >> You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): >> >> WITH cte (RowNumber, StartCode, EndCode) AS >> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) >> UPDATE cte SET StartCode = >> (SELECT EndCode FROM cte AS cte2 >> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >> >> Asger _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at colbyconsulting.com Thu Sep 23 19:12:59 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 20:12:59 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <97F08F50ACEC491F997EA5363A2F8596@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com><0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> Message-ID: <4C9BED0B.1050101@colbyconsulting.com> > I didn't consider your first comment a polite request for explanation, rather a rant 1) A rant it was. I have work to do, I am not here to play silly games. Clues? I think not. >just reinforcing that you adhere to cursors. 2) I don't "adhere" to cursors, I use the tools I know and can understand. I have probably used a cursor a handful of times over the years. This KIND of things is exactly where I would use it, getting information from a different row to use in the current row. If you will teach me a better tool, I will most certainly use that instead. But your example (sans explanation) did not teach me anything. > Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. And why is that. I have been on this and the Access and VB lists for 15 years and we do give step by step explanations of code. Why would you not? This *is* a classroom. Why do you think people come here? BTW, just an FYI, a year or two ago (over on the Access list) I wrote an entire series of emails on Access classes and withevents. It is a complex subject and needed more than an example. And I never once told the reader to go RTFM. I put my money where my mouth is. >If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. I was not being rude, I was saying what I needed to say. I repeat, if I understood the example, then I wouldn't need you to write the example. By definition if you have to provide me an example then you need to explain it. Or if you know of examples on the internet that explain it provide those. Look at any book on technical things like SQL or C#. They don't just throw out examples, they discuss the principals, then they provide examples, then they explain how the example applies to the principal. Asger, I absolutely appreciate your taking the time. I really do. What I *don't* appreciate is the "Read the BOL" attitude. I probably own as many books as anyone on this list, SHELVES full of them. But I work for a living, and my job is NOT as a DBA. The DBA is about 5% of my job. I am a sole proprietor who routinely uses, in any given day, SQL Server, C# and Access. I build my own computers, install software, and everything else you can think of that has to be done to keep my business running, *just me* doing the whole damned thing. I don't have time to solve "clues". I need as much help as you can give so that I can learn it from you and your examples *quickly*. If you don't want to provide that help, just say so, I can understand that. Whatever you do, *don't* say "here's an example, now go figure it out". SHITCAN! <\rant mode> And Asger, thank you very much for the detailed explanation. I will study that and understand that. John W. Colby www.ColbyConsulting.com On 9/23/2010 7:25 PM, Asger Blond wrote: > John, > To get you a clue what the code does, and what I did. > First, when answering a question I normally make effort recreating the scenario in question. Which was what I did in this case: created a table and a query with the data provided by Paul. If you have done the same you can easily test and play around with the code example - soon you should notice what's going on. > Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. > I didn't consider your first comment a polite request for explanation, rather a rant just reinforcing that you adhere to cursors. > Being more reasonable in your second reply I'll supply explanation using the example from my second posting to Paul: > > WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber = cte2.RowNumber+1 AND cte.PersonID = cte2.PersonID) WHERE RowNumber> 1 > > The first part - before UPDATE - creates a Common Table Expression which you know is a kind of virtual table that can be used instead of a temporary table. In this cte I make use of the ranking function ROW_NUMBER() with the qualification PARTION BY PersonID ORDER BY PersonID to get a numbering sequence for each group (or "partition") of PersonID's. > Suppose you have the following rows in the query, where I have deliberately added two more rows to Pauls query for the purpose of showing different PersonID's: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > 222222 29/09/2010 XX1 1XX YY1 1YY > 222222 29/09/2010 XX2 2XX YY2 1YY > > With this recordset you can query the supplied cte using: > WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) SELECT * FROM cte > The result will be: > > RowNumber StartCode EndCode > 1 AA1 1AA BB1 1BB > 2 AA2 2AA BB2 2BB > 3 AA3 3AA BB3 3BB > 4 AA4 4AA BB4 4BB > 1 XX1 1XX YY1 1YY > 2 XX2 2XX YY2 2YY > > Now the last UPDATE part of the code use a correlated subquery that selects the EndCode of the previous row inside each PersonID and use it to replace the value of StartCode. It does this for all rows except for rows where RowNumber = 1. If this last where clause is omitted then all rows with RowNumber = 1 will get a NULL as StartCode. This might actually be wanted and can easily be achieved omitting the clause. > Running the code as is will give the following records: > > PersonID Date StartCode EndCode > 11111 29/09/2010 AA1 1AA BB1 1BB > 11111 29/09/2010 BB1 1BB BB2 2BB > 11111 29/09/2010 BB2 2BB BB3 3BB > 11111 29/09/2010 BB3 3BB BB4 4BB > 22222 29/09/2010 XX1 1XX YY1 1YY > 22222 29/09/2010 YY1 1YY YY2 2YY > > As I read Paul's question this is what's wanted. > > PS: If an operation can be achieved both by a cursor and by a set based query then IMO the cursor implementation will be by far the slowest. If you disagree, John, then please supply an example. > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 24. september 2010 01:06 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > >> a clue what this code does > > I am not playing games, thanks. > > > If answering a question with an example of solution is inappropriate > > LOL, Asger I wasn't saying that at all. Providing an example solution is fine, providing knowledge > is better. > > >then I don't know what the purpose of this list is. > > Helping people *learn*. > > A paragraph or two would have allowed those for who SQL is not our first language to begin to > understand what you are doing. > > It is not like you are just doing CTEs (I am starting to use them, I love them, but it took me > awhile to get comfortable with just that piece). You are also throwing in Over (I have not use that > before) and row numbers (I have not used that before). All things that it would be wicked to know! > > But it reaches the point where there is so much new stuff that I can't get a handle on the what you > are trying to do. If we understood the example why would we need the example? > > Could I go figure it out? Of course. Or I could throw together a cursor and move on. > > I would truly like to understand what you are doing. OTOH today I re-engineered a slew of C# code > to break a bunch of updates (on 65 million record tables) into batches, wrapped in transactions. I > also worked on a client's Access database to add filtering to a form, and I worked on another > client's database to investigate a slowdown that occurred (probably hardware) that occurred back in > July and they are just now complaining about. I did three zip code counts for a third client and > after a 10 hour day I am not done yet. > > Without somebody who cares enough to explain their solution, the solution will go in the shitcan. > Which would be a pity, but I don't have the time to spend hours figuring out something that I could > learn in 5 minutes if you were kind enough to explain. > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 5:12 PM, Asger Blond wrote: >> John, >>> a clue what this code does >> It simply does what Paul was asking for. >> If answering a question with an example of solution is inappropriate then I don't know what the purpose of this list is. >> Asger >> >> -----Oprindelig meddelelse----- >> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby >> Sendt: 23. september 2010 20:54 >> Til: Discussion concerning MS SQL Server >> Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >> >> OK Asger (and Francisco), but this is a perfect example of... WTF over? >> >> I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code >> does. >> >> If you are going to recommend code like this you need to tell us how it works, otherwise I for one >> will go "ok, now where did I put that cursor..." >> >> >> >> John W. Colby >> www.ColbyConsulting.com >> >> On 9/23/2010 12:13 PM, Asger Blond wrote: >>> Paul, >>> You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): >>> >>> WITH cte (RowNumber, StartCode, EndCode) AS >>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) >>> UPDATE cte SET StartCode = >>> (SELECT EndCode FROM cte AS cte2 >>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>> >>> Asger > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From ab-mi at post3.tele.dk Thu Sep 23 19:16:19 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Fri, 24 Sep 2010 02:16:19 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BE088.9040805@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <4C9BE088.9040805@colbyconsulting.com> Message-ID: John, > *with no explanation at all* And from your previous posting: > I don't have the time to spend hours figuring out something that I could > learn in 5 minutes if you were kind enough to explain Excuse me, but I made a typo in my last reply to you. The last row in the testing recordset should of course not be: 222222 29/09/2010 XX2 2XX YY2 1YY It should be: 222222 29/09/2010 XX2 2XX YY2 2YY Did you get my point? We all have work to do and don't want to spend hours making long explanations and then proof-reading our explanations which sure will have pleanty of slpelling flauts. Asger -----Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby Sendt: 24. september 2010 01:20 Til: Discussion concerning MS SQL Server Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors LOL, Francisco you crack me up sometimes. From the SQL Server Team... (and YOU) http://www.sqlteam.com/article/cursors-an-overview Where in there does it say anything about unstable or unresponsive servers. In the real world (yours too I assume) we often need to just get it done and move on. Throwing out a solution *with no explanation at all* that is obviously classy, but a bit dense, to someone asking if a cursor is OK, is... less than helpful. I understand from past experiences with this list that there is a very definite "read BOL" mentality. If it don't hurt, it ain't good for ya. ;) So Francisco, I will pretty much make whatever damned recommendations I want. Including "use a cursor when it fits" which is what I said originally. It's a tool for God's sake. Provided by Microsoft for God's sake. Discussed in a blog by none other than SQLTeam.com for God's sake. If you have a problem with them, go bitch at somebody over there. John W. Colby www.ColbyConsulting.com On 9/23/2010 4:58 PM, Francisco Tapia wrote: > John, > in your environment and because you are a single user, your experiences > are more unique than most people who work with SQL Server, you can make > decisions to use cursors on a whim that will impact your server performance, > however, most other SQL programmers and DBA's generally need to address > server performance and availability. Because of that I recommend that you > (John) continue to use the tool of your choice, however, when offering > advice on cursors, would refrain from stating when a cursor is valid. I > would hate to have someone's server crash or become unresponsive when it > deals with a community of users. > > it's also quite another thing to state that information wasn't provided, > Asger, clearly stated that his example was a subquery example with common > table expressions (CTE) and Rank function, all terms that can be quickly be > researched at on Books online (BOL). > > > > -Francisco > http://bit.ly/sqlthis | Tsql and More... > > > > > On Thu, Sep 23, 2010 at 11:53 AM, jwcolbywrote: > >> OK Asger (and Francisco), but this is a perfect example of... WTF over? >> >> I for one haven't a clue what this code does, nor do I have a clue how to >> figure out what this code >> does. >> >> If you are going to recommend code like this you need to tell us how it >> works, otherwise I for one >> will go "ok, now where did I put that cursor..." >> >> >> >> John W. Colby >> www.ColbyConsulting.com >> >> On 9/23/2010 12:13 PM, Asger Blond wrote: >>> Paul, >>> You can use a subquery to make this update. Following example uses a >> Common Table Expression and the Rank function Row_Number(): >>> >>> WITH cte (RowNumber, StartCode, EndCode) AS >>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM >> YourQuery) >>> UPDATE cte SET StartCode = >>> (SELECT EndCode FROM cte AS cte2 >>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>> >>> Asger >>> >>> -----Oprindelig meddelelse----- >>> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: >> dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland >>> Sendt: 23. september 2010 16:53 >>> Til: SQLServerList >>> Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >>> >>> To all, >>> >>> We have employees that can do more than one job in a day on our database, >>> when an employee is assigned to a job, the application takes the persons >>> home postcode and the jobs location postcode and stores them in a table, >> we >>> are now creating an estimated mileage. I can do this based on the >> postcodes >>> supplied but it will be inaccurate as the start postcodes will always be >> the >>> employees home, when the start postcode for the second job should really >> be >>> the postcode of the previous job location. >>> >>> At the moment I have a select query which returns results for an employee >>> where they are doing more than one job in a day, result example below: >>> >>> PersonID Date StartCode EndCode >>> 111111 29/09/2010 AA1 1AA BB1 1BB >>> 111111 29/09/2010 AA2 2AA BB2 2BB >>> 111111 29/09/2010 AA3 3AA BB3 3BB >>> 111111 29/09/2010 AA4 4AA BB4 4BB >>> >>> I am thinking of putting these results into a cursor and looping through >> the >>> records updating the startcode with the previous records endcode, so that >>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >>> >>> Is this the best way to do this, or could someone point me in a better >>> direction please. >>> >>> Many thanks in advance for all your help. >>> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From fhtapia at gmail.com Thu Sep 23 19:55:46 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 17:55:46 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BED0B.1050101@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> <4C9BED0B.1050101@colbyconsulting.com> Message-ID: Damn you John... You always have to throw in the number of years you've been on this list as if that means anything... Nobody stops you from asking additional questions. Asger is polite and responds despite your rants... There are plenty of places on The net that address the problems with cursors... Another such post states http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx It's not just that a cursor taxes your system to yield poor performance...any piece of code can break due to unforeseen error handling, such an occurrence could render your production system unstable due to a cursor that simply will not close. Eating ram, and holding on to locks endlessly. This list is not all about you John, just like the OP whom probably need sot ensure not only results but also maintain an operating database as well as one that performs. Ask on this list about post you don't understand. How? Why? On Thursday, September 23, 2010, jwcolby wrote: > ?> I didn't consider your first comment a polite request for explanation, rather a rant > > 1) A rant it was. ?I have work to do, I am not here to play silly games. ?Clues? ?I think not. > > ?>just reinforcing that you adhere to cursors. > > 2) I don't "adhere" to cursors, I use the tools I know and can understand. ?I have probably used a > cursor a handful of times over the years. ?This KIND of things is exactly where I would use it, > getting information from a different row to use in the current row. > > If you will teach me a better tool, I will most certainly use that instead. ?But your example (sans > explanation) did not teach me anything. > > ?> Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be > totally inadequate for a list like this. > > And why is that. ?I have been on this and the Access and VB lists for 15 years and we do give step > by step explanations of code. ?Why would you not? ?This *is* a classroom. ?Why do you think people > come here? > > BTW, just an FYI, a year or two ago (over on the Access list) I wrote an entire series of emails on > Access classes and withevents. ?It is a complex subject and needed more than an example. ?And I > never once told the reader to go RTFM. ?I put my money where my mouth is. > > ?>If someone does not understand the example or some parts of the example then I - and I'm sure most > listers - will readily give an answer if asked politely. > > > > I was not being rude, I was saying what I needed to say. ?I repeat, if I understood the example, > then I wouldn't need you to write the example. ?By definition if you have to provide me an example > then you need to explain it. ?Or if you know of examples on the internet that explain it provide those. > > Look at any book on technical things like SQL or C#. ?They don't just throw out examples, they > discuss the principals, then they provide examples, then they explain how the example applies to the > principal. > > Asger, I absolutely appreciate your taking the time. ?I really do. ?What I *don't* appreciate is the > "Read the BOL" attitude. ?I probably own as many books as anyone on this list, SHELVES full of them. > ?But I work for a living, and my job is NOT as a DBA. The DBA is about 5% of my job. ?I am a sole > proprietor who routinely uses, in any given day, SQL Server, C# and Access. ?I build my own > computers, install software, and everything else you can think of that has to be done to keep my > business running, *just me* doing the whole damned thing. ?I don't have time to solve "clues". > > I need as much help as you can give so that I can learn it from you and your examples *quickly*. ?If > you don't want to provide that help, just say so, I can understand that. ?Whatever you do, *don't* > say "here's an example, now go figure it out". > > SHITCAN! > > <\rant mode> > > And Asger, thank you very much for the detailed explanation. ?I will study that and understand that. > > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 7:25 PM, Asger Blond wrote: >> John, >> To get you a clue what the code does, and what I did. >> First, when answering a question I normally make effort recreating the scenario in question. Which was what I did in this case: created a table and a query with the data provided by Paul. If you have done the same you can easily test and play around with the code example - soon you should notice what's going on. >> Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. >> I didn't consider your first comment a polite request for explanation, rather a rant just reinforcing that you adhere to cursors. >> Being more reasonable in your second reply I'll supply explanation using the example from my second posting to Paul: >> >> WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber = cte2.RowNumber+1 AND cte.PersonID = cte2.PersonID) WHERE RowNumber> ?1 >> >> The first part - before UPDATE - creates a Common Table Expression which you know is a kind of virtual table that can be used instead of a temporary table. In this cte I make use of the ranking function ROW_NUMBER() with the qualification PARTION BY PersonID ORDER BY PersonID to get a numbering sequence for each group (or "partition") of PersonID's. >> Suppose you have the following rows in the query, where I have deliberately added two more rows to Pauls query for the purpose of showing different PersonID's: >> >> PersonID ? ? Date ? ? ? ? ? ? ? ? StartCode ? ? ? ? ? ?EndCode >> 111111 ? ? ? ?29/09/2010 ? ? ? ?AA1 1AA ? ? ? ? ? ? BB1 1BB >> 111111 ? ? ? ?29/09/2010 ? ? ? ?AA2 2AA ? ? ? ? ? ? BB2 2BB >> 111111 ? ? ? ?29/09/2010 ? ? ? ?AA3 3AA ? ? ? ? ? ? BB3 3BB >> 111111 ? ? ? ?29/09/2010 ? ? ? ?AA4 4AA ? ? ? ? ? ? BB4 4BB >> 222222 ? ? ? ?29/09/2010 ? ? ? ?XX1 1XX ? ? ? ? ? ? YY1 1YY >> 222222 ? ? ? ?29/09/2010 ? ? ? ?XX2 2XX ? ? ? ? ? ? YY2 1YY >> >> With this recordset you can query the supplied cte using: >> WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) SELECT * FROM cte >> The result will be: >> >> RowNumber ? ? StartCode ? ? ? EndCode >> 1 ? ? AA1 1AA BB1 1BB >> 2 ? ? AA2 2AA BB2 2BB >> 3 ? ? AA3 3AA BB3 3BB >> 4 ? ? AA4 4AA BB4 4BB >> 1 ? ? XX1 1XX YY1 1YY >> 2 ? ? XX2 2XX YY2 2YY >> >> Now the last UPDATE part of the code use a correlated subquery that selects the EndCode of the previous row inside each PersonID and use it to replace the value of StartCode. It does this for all rows except for rows where RowNumber = 1. If this last where clause is omitted then all rows with RowNumber = 1 will get a NULL as StartCode. This might actually be wanted and can easily be achieved omitting the clause. >> Running the code as is will give the following records: >> >> PersonID ? ? ?Date ? ? ? ? ? ?StartCode ? ? ? ? ? ? ? EndCode >> 11111 29/09/2010 ? ? ? ? ? ? ?AA1 1AA ? ? ? ? BB1 1BB >> 11111 29/09/2010 ? ? ? ? ? ? ?BB1 1BB ? ? ? ? BB2 2BB >> 11111 29/09/2010 ? ? ? ? ? ? ?BB2 2BB ? ? ? ? BB3 3BB >> 11111 29/09/2010 ? ? ? ? ? ? ?BB3 3BB ? ? ? ? BB4 4BB >> 22222 29/09/2010 ? ? ? ? ? ? ?XX1 1XX ? ? ? ? YY1 1YY >> 22222 29/09/2010 ? ? ? ? ? ? ?YY1 1YY ? ? ? ? YY2 2YY >> >> As I read Paul's question this is what's wanted. >> >> PS: If an operation can be achieved both by a cu -- -Francisco http://bit.ly/sqlthis | Tsql and More... From ab-mi at post3.tele.dk Thu Sep 23 20:01:40 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Fri, 24 Sep 2010 03:01:40 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9BED0B.1050101@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com><0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com><97F08F50ACEC491F997EA5363A2F8596@abpc> <4C9BED0B.1050101@colbyconsulting.com> Message-ID: > By definition if you have to provide me an example then you > need to explain it Totally disagree. For once I'm not directing you to BOL. Just read Plato's dialogue Meno (especially the part where Socrates is questioning the slave). Asger ----- Oprindelig meddelelse----- Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby Sendt: 24. september 2010 02:13 Til: Discussion concerning MS SQL Server Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > I didn't consider your first comment a polite request for explanation, rather a rant 1) A rant it was. I have work to do, I am not here to play silly games. Clues? I think not. >just reinforcing that you adhere to cursors. 2) I don't "adhere" to cursors, I use the tools I know and can understand. I have probably used a cursor a handful of times over the years. This KIND of things is exactly where I would use it, getting information from a different row to use in the current row. If you will teach me a better tool, I will most certainly use that instead. But your example (sans explanation) did not teach me anything. > Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. And why is that. I have been on this and the Access and VB lists for 15 years and we do give step by step explanations of code. Why would you not? This *is* a classroom. Why do you think people come here? BTW, just an FYI, a year or two ago (over on the Access list) I wrote an entire series of emails on Access classes and withevents. It is a complex subject and needed more than an example. And I never once told the reader to go RTFM. I put my money where my mouth is. >If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. I was not being rude, I was saying what I needed to say. I repeat, if I understood the example, then I wouldn't need you to write the example. By definition if you have to provide me an example then you need to explain it. Or if you know of examples on the internet that explain it provide those. Look at any book on technical things like SQL or C#. They don't just throw out examples, they discuss the principals, then they provide examples, then they explain how the example applies to the principal. Asger, I absolutely appreciate your taking the time. I really do. What I *don't* appreciate is the "Read the BOL" attitude. I probably own as many books as anyone on this list, SHELVES full of them. But I work for a living, and my job is NOT as a DBA. The DBA is about 5% of my job. I am a sole proprietor who routinely uses, in any given day, SQL Server, C# and Access. I build my own computers, install software, and everything else you can think of that has to be done to keep my business running, *just me* doing the whole damned thing. I don't have time to solve "clues". I need as much help as you can give so that I can learn it from you and your examples *quickly*. If you don't want to provide that help, just say so, I can understand that. Whatever you do, *don't* say "here's an example, now go figure it out". SHITCAN! <\rant mode> And Asger, thank you very much for the detailed explanation. I will study that and understand that. John W. Colby www.ColbyConsulting.com On 9/23/2010 7:25 PM, Asger Blond wrote: > John, > To get you a clue what the code does, and what I did. > First, when answering a question I normally make effort recreating the scenario in question. Which was what I did in this case: created a table and a query with the data provided by Paul. If you have done the same you can easily test and play around with the code example - soon you should notice what's going on. > Second, step-by-step explanation of code examples may be fine for a class room, but IMO would be totally inadequate for a list like this. If someone does not understand the example or some parts of the example then I - and I'm sure most listers - will readily give an answer if asked politely. > I didn't consider your first comment a polite request for explanation, rather a rant just reinforcing that you adhere to cursors. > Being more reasonable in your second reply I'll supply explanation using the example from my second posting to Paul: > > WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) UPDATE cte SET StartCode = (SELECT EndCode FROM cte AS cte2 WHERE cte.RowNumber = cte2.RowNumber+1 AND cte.PersonID = cte2.PersonID) WHERE RowNumber> 1 > > The first part - before UPDATE - creates a Common Table Expression which you know is a kind of virtual table that can be used instead of a temporary table. In this cte I make use of the ranking function ROW_NUMBER() with the qualification PARTION BY PersonID ORDER BY PersonID to get a numbering sequence for each group (or "partition") of PersonID's. > Suppose you have the following rows in the query, where I have deliberately added two more rows to Pauls query for the purpose of showing different PersonID's: > > PersonID Date StartCode EndCode > 111111 29/09/2010 AA1 1AA BB1 1BB > 111111 29/09/2010 AA2 2AA BB2 2BB > 111111 29/09/2010 AA3 3AA BB3 3BB > 111111 29/09/2010 AA4 4AA BB4 4BB > 222222 29/09/2010 XX1 1XX YY1 1YY > 222222 29/09/2010 XX2 2XX YY2 1YY > > With this recordset you can query the supplied cte using: > WITH cte (RowNumber, PersonID, StartCode, EndCode) AS (SELECT ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY PersonID), PersonID, StartCode, EndCode FROM YourQuery) SELECT * FROM cte > The result will be: > > RowNumber StartCode EndCode > 1 AA1 1AA BB1 1BB > 2 AA2 2AA BB2 2BB > 3 AA3 3AA BB3 3BB > 4 AA4 4AA BB4 4BB > 1 XX1 1XX YY1 1YY > 2 XX2 2XX YY2 2YY > > Now the last UPDATE part of the code use a correlated subquery that selects the EndCode of the previous row inside each PersonID and use it to replace the value of StartCode. It does this for all rows except for rows where RowNumber = 1. If this last where clause is omitted then all rows with RowNumber = 1 will get a NULL as StartCode. This might actually be wanted and can easily be achieved omitting the clause. > Running the code as is will give the following records: > > PersonID Date StartCode EndCode > 11111 29/09/2010 AA1 1AA BB1 1BB > 11111 29/09/2010 BB1 1BB BB2 2BB > 11111 29/09/2010 BB2 2BB BB3 3BB > 11111 29/09/2010 BB3 3BB BB4 4BB > 22222 29/09/2010 XX1 1XX YY1 1YY > 22222 29/09/2010 YY1 1YY YY2 2YY > > As I read Paul's question this is what's wanted. > > PS: If an operation can be achieved both by a cursor and by a set based query then IMO the cursor implementation will be by far the slowest. If you disagree, John, then please supply an example. > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 24. september 2010 01:06 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > >> a clue what this code does > > I am not playing games, thanks. > > > If answering a question with an example of solution is inappropriate > > LOL, Asger I wasn't saying that at all. Providing an example solution is fine, providing knowledge > is better. > > >then I don't know what the purpose of this list is. > > Helping people *learn*. > > A paragraph or two would have allowed those for who SQL is not our first language to begin to > understand what you are doing. > > It is not like you are just doing CTEs (I am starting to use them, I love them, but it took me > awhile to get comfortable with just that piece). You are also throwing in Over (I have not use that > before) and row numbers (I have not used that before). All things that it would be wicked to know! > > But it reaches the point where there is so much new stuff that I can't get a handle on the what you > are trying to do. If we understood the example why would we need the example? > > Could I go figure it out? Of course. Or I could throw together a cursor and move on. > > I would truly like to understand what you are doing. OTOH today I re-engineered a slew of C# code > to break a bunch of updates (on 65 million record tables) into batches, wrapped in transactions. I > also worked on a client's Access database to add filtering to a form, and I worked on another > client's database to investigate a slowdown that occurred (probably hardware) that occurred back in > July and they are just now complaining about. I did three zip code counts for a third client and > after a 10 hour day I am not done yet. > > Without somebody who cares enough to explain their solution, the solution will go in the shitcan. > Which would be a pity, but I don't have the time to spend hours figuring out something that I could > learn in 5 minutes if you were kind enough to explain. > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 5:12 PM, Asger Blond wrote: >> John, >>> a clue what this code does >> It simply does what Paul was asking for. >> If answering a question with an example of solution is inappropriate then I don't know what the purpose of this list is. >> Asger >> >> -----Oprindelig meddelelse----- >> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby >> Sendt: 23. september 2010 20:54 >> Til: Discussion concerning MS SQL Server >> Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >> >> OK Asger (and Francisco), but this is a perfect example of... WTF over? >> >> I for one haven't a clue what this code does, nor do I have a clue how to figure out what this code >> does. >> >> If you are going to recommend code like this you need to tell us how it works, otherwise I for one >> will go "ok, now where did I put that cursor..." >> >> >> >> John W. Colby >> www.ColbyConsulting.com >> >> On 9/23/2010 12:13 PM, Asger Blond wrote: >>> Paul, >>> You can use a subquery to make this update. Following example uses a Common Table Expression and the Rank function Row_Number(): >>> >>> WITH cte (RowNumber, StartCode, EndCode) AS >>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM YourQuery) >>> UPDATE cte SET StartCode = >>> (SELECT EndCode FROM cte AS cte2 >>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>> >>> Asger > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at colbyconsulting.com Thu Sep 23 20:24:46 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 21:24:46 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <4C9BE088.9040805@colbyconsulting.com> Message-ID: <4C9BFDDE.2060105@colbyconsulting.com> With no rant! ;) Thanks John W. Colby www.ColbyConsulting.com On 9/23/2010 8:16 PM, Asger Blond wrote: > > John, >> *with no explanation at all* > And from your previous posting: >> I don't have the time to spend hours figuring out something that I could >> learn in 5 minutes if you were kind enough to explain > > Excuse me, but I made a typo in my last reply to you. > The last row in the testing recordset should of course not be: > 222222 29/09/2010 XX2 2XX YY2 1YY > It should be: > 222222 29/09/2010 XX2 2XX YY2 2YY > > Did you get my point? We all have work to do and don't want to spend hours making long explanations and then proof-reading our explanations which sure will have pleanty of slpelling flauts. > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 24. september 2010 01:20 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > LOL, Francisco you crack me up sometimes. > > From the SQL Server Team... > > (and YOU) > > http://www.sqlteam.com/article/cursors-an-overview > > Where in there does it say anything about unstable or unresponsive servers. > > In the real world (yours too I assume) we often need to just get it done and move on. Throwing out > a solution *with no explanation at all* that is obviously classy, but a bit dense, to someone asking > if a cursor is OK, is... less than helpful. > > I understand from past experiences with this list that there is a very definite "read BOL" > mentality. If it don't hurt, it ain't good for ya. ;) > > So Francisco, I will pretty much make whatever damned recommendations I want. Including "use a > cursor when it fits" which is what I said originally. It's a tool for God's sake. Provided by > Microsoft for God's sake. Discussed in a blog by none other than SQLTeam.com for God's sake. If > you have a problem with them, go bitch at somebody over there. > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 4:58 PM, Francisco Tapia wrote: >> John, >> in your environment and because you are a single user, your experiences >> are more unique than most people who work with SQL Server, you can make >> decisions to use cursors on a whim that will impact your server performance, >> however, most other SQL programmers and DBA's generally need to address >> server performance and availability. Because of that I recommend that you >> (John) continue to use the tool of your choice, however, when offering >> advice on cursors, would refrain from stating when a cursor is valid. I >> would hate to have someone's server crash or become unresponsive when it >> deals with a community of users. >> >> it's also quite another thing to state that information wasn't provided, >> Asger, clearly stated that his example was a subquery example with common >> table expressions (CTE) and Rank function, all terms that can be quickly be >> researched at on Books online (BOL). >> >> >> >> -Francisco >> http://bit.ly/sqlthis | Tsql and More... >> >> >> >> >> On Thu, Sep 23, 2010 at 11:53 AM, jwcolbywrote: >> >>> OK Asger (and Francisco), but this is a perfect example of... WTF over? >>> >>> I for one haven't a clue what this code does, nor do I have a clue how to >>> figure out what this code >>> does. >>> >>> If you are going to recommend code like this you need to tell us how it >>> works, otherwise I for one >>> will go "ok, now where did I put that cursor..." >>> >>> >>> >>> John W. Colby >>> www.ColbyConsulting.com >>> >>> On 9/23/2010 12:13 PM, Asger Blond wrote: >>>> Paul, >>>> You can use a subquery to make this update. Following example uses a >>> Common Table Expression and the Rank function Row_Number(): >>>> >>>> WITH cte (RowNumber, StartCode, EndCode) AS >>>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM >>> YourQuery) >>>> UPDATE cte SET StartCode = >>>> (SELECT EndCode FROM cte AS cte2 >>>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>>> >>>> Asger >>>> >>>> -----Oprindelig meddelelse----- >>>> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: >>> dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland >>>> Sendt: 23. september 2010 16:53 >>>> Til: SQLServerList >>>> Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >>>> >>>> To all, >>>> >>>> We have employees that can do more than one job in a day on our database, >>>> when an employee is assigned to a job, the application takes the persons >>>> home postcode and the jobs location postcode and stores them in a table, >>> we >>>> are now creating an estimated mileage. I can do this based on the >>> postcodes >>>> supplied but it will be inaccurate as the start postcodes will always be >>> the >>>> employees home, when the start postcode for the second job should really >>> be >>>> the postcode of the previous job location. >>>> >>>> At the moment I have a select query which returns results for an employee >>>> where they are doing more than one job in a day, result example below: >>>> >>>> PersonID Date StartCode EndCode >>>> 111111 29/09/2010 AA1 1AA BB1 1BB >>>> 111111 29/09/2010 AA2 2AA BB2 2BB >>>> 111111 29/09/2010 AA3 3AA BB3 3BB >>>> 111111 29/09/2010 AA4 4AA BB4 4BB >>>> >>>> I am thinking of putting these results into a cursor and looping through >>> the >>>> records updating the startcode with the previous records endcode, so that >>>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >>>> >>>> Is this the best way to do this, or could someone point me in a better >>>> direction please. >>>> >>>> Many thanks in advance for all your help. >>>> >>> _______________________________________________ >>> dba-SQLServer mailing list >>> dba-SQLServer at databaseadvisors.com >>> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >>> http://www.databaseadvisors.com >>> >>> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From ab-mi at post3.tele.dk Thu Sep 23 20:29:33 2010 From: ab-mi at post3.tele.dk (Asger Blond) Date: Fri, 24 Sep 2010 03:29:33 +0200 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors - Solution Or Not Solution? In-Reply-To: <97F08F50ACEC491F997EA5363A2F8596@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com><0312DC43470649218CACAB408333CE62@abpc><4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> Message-ID: <8CE87AB4DE774F9DA037FDF9BECB44C5@abpc> Paul Curious: Did your question to this list give you a solution? Asgsr From jwcolby at colbyconsulting.com Thu Sep 23 20:53:21 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 21:53:21 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> <4C9BED0B.1050101@colbyconsulting.com> Message-ID: <4C9C0491.3060303@colbyconsulting.com> My goodness Francisco, don't you even read what you ask me to read? >Another cursor has been busted! Right? Actually ... no. You see, eliminating cursors is not about syntax. LOL. >If that is the case, what have we just gained by replacing our CURSOR? Honestly -- not much, if anything at all. Because of the task at hand, we may very well need to process rows in the Product table one-by-one to send our emails and generate the report, and the bottleneck here is not the cursor code at all ROTFL >Replacing a perfectly fine, simple cursor with a WHILE loop might even make your code longer, or more confusing, or even less efficient depending on circumstances. Perfectly fine cursor??? LMAOBTC Did he really say "perfectly fine"? PLEASE tell me he didn't say "simple". >how would we write this as a WHILE loop? Is it possible? Sure. Will it be as simple and clean as using a cursor? No, it won't. (Though ROW_COUNT() makes this much easier than it used to be) OMG, he said it again! "Simple"? "Clean"? >Now, I am not here to say that cursors are "good", but if you really need to process rows one by one, go ahead and proudly use a cursor. Uhh... am I missing something? So far nothing about system instability, nothing about crashing servers... Lots about "simple, clean and use them proudly". If they are crashing every server in sight why is this schmuck saying to use them? And why aren't you over there ragging on him? And why are you telling me to read his stuff? What an IDIOT! (to quote Hermione) OK, enough! I never said that in this situation there wasn't a better solution. What I said was that it is a tool, a simple tool, and one which will do this VERY SIMPLE job just fine if you want to do it that way. Would it scale to a million records? Did I ever claim that it would? Did I not specifically state that it shouldn't be used for that. Is that solution going to "tax his system". Seems rather unlikely. Is it going to crash his system? Seems unlikely. Does it have to be done with a cursor? Obviously not. As I said Fransicso, sometimes you crack me up. It's a tool for gods sake. It does simple things simply. For all you SQL Gods (yes you Francisco) there are better solutions, which you can code like I can code DAO. Unfortunately I am not a SQL (or any other kind of) god. And just because it seems to annoy you, I'll mention it again. I've been on this list for 15 years. I've been on this list for 15 years. I've been on this list for 15 years. I've been on this list for 15 years. I've been on this list for 15 years. I've been on this list for 15 years. Bows low to the west, in the general direction of the ranking SQL God. ;) John W. Colby www.ColbyConsulting.com On 9/23/2010 8:55 PM, Francisco Tapia wrote: > Damn you John... > You always have to throw in the number of years you've been on this > list as if that means anything... From jwcolby at colbyconsulting.com Thu Sep 23 20:54:42 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Thu, 23 Sep 2010 21:54:42 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors - Solution Or Not Solution? In-Reply-To: <8CE87AB4DE774F9DA037FDF9BECB44C5@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com><0312DC43470649218CACAB408333CE62@abpc><4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> <8CE87AB4DE774F9DA037FDF9BECB44C5@abpc> Message-ID: <4C9C04E2.3000906@colbyconsulting.com> Asger, It gave me one. Thanks! John W. Colby www.ColbyConsulting.com On 9/23/2010 9:29 PM, Asger Blond wrote: > Paul > Curious: Did your question to this list give you a solution? > Asgsr > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From fhtapia at gmail.com Thu Sep 23 21:34:41 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Thu, 23 Sep 2010 19:34:41 -0700 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9C0491.3060303@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> <4C9BED0B.1050101@colbyconsulting.com> <4C9C0491.3060303@colbyconsulting.com> Message-ID: Really? YES JOHN, everyone here knows you've been here for 15 yrs... hey wait... where the computers that far back? ;-) weren't they called typewriters? still... you missed the point of the article in that just because you avoid using the "term" cursor it does not solve the issue of avoiding cursors. additionally didn't I also state that there will be times when a cursor "might" be justified... sure you "COULD" google for examples but what would be the fun of that? http://www.sql-server-performance.com/tips/cursors_p1.aspx additonally...nobody here claims to be gods... maybe your time on here makes you think you are one but ... nope you're not... i might have been struck by lightning for mentiong the bound forms comment early on in the thread. I won't rant at anyone else because it's the poor guy's blog... I liked the point he was trying to make, just because you are writting your sql w/o the keyword CURSOR doesn't mean you've avoided a performance bottleneck... likewise... just because you've coded to deallocate your cursor and think it to be done well doesn't mean some data will corrupt or crash your sproc or job that doesn't deallocate your cursor, locking away tables, rows or memory needed for your server. in your scenario... you can just reboot and restore your read-only db if it goes into suspect mode... that's not as much an option for other people on the list that need to have high availability and reliable dependable performance. On Thursday, September 23, 2010, jwcolby wrote: > My goodness Francisco, don't you even read what you ask me to read? > > ?>Another cursor has been busted! Right? Actually ... no. You see, eliminating cursors is not about > syntax. > > LOL. > > ?>If that is the case, what have we just gained by replacing our CURSOR? ? Honestly -- not much, ?if > anything at all. ?Because of the task at hand, we may very well need to process rows in the Product > table one-by-one to send our emails and generate the report, and the bottleneck here is not the > cursor code at all > > ROTFL > > ?>Replacing a perfectly fine, simple cursor with a WHILE loop might even make your code longer, or > more confusing, or even less efficient depending on circumstances. > > Perfectly fine cursor??? ?LMAOBTC ?Did he really say "perfectly fine"? ?PLEASE tell me he didn't say > "simple". > > ?>how would we write this as a WHILE loop? ?Is it possible? ?Sure. ?Will it be as simple and clean > as using a cursor? ?No, it won't. (Though ROW_COUNT() makes this much easier than it used to be) > > OMG, he said it again! "Simple"? ?"Clean"? > > ?>Now, I am not here to say that cursors are "good", but if you really need to process rows one by > one, go ahead and proudly use a cursor. > > Uhh... am I missing something? ?So far nothing about system instability, nothing about crashing > servers... ?Lots about "simple, clean and use them proudly". ?If they are crashing every server in > sight why is this schmuck saying to use them? ?And why aren't you over there ragging on him? ?And > why are you telling me to read his stuff? ?What an IDIOT! (to quote Hermione) > > OK, enough! ?I never said that in this situation there wasn't a better solution. ?What I said was > that it is a tool, a simple tool, and one which will do this VERY SIMPLE job just fine if you want > to do it that way. > > Would it scale to a million records? ?Did I ever claim that it would? ?Did I not specifically state > that it shouldn't be used for that. ?Is that solution going to "tax his system". ?Seems rather > unlikely. ?Is it going to crash his system? ?Seems unlikely. ?Does it have to be done with a cursor? > Obviously not. > > As I said Fransicso, sometimes you crack me up. ?It's a tool for gods sake. ?It does simple things > simply. ?For all you SQL Gods (yes you Francisco) there are better solutions, which you can code > like I can code DAO. ?Unfortunately I am not a SQL (or any other kind of) god. > > And just because it seems to annoy you, I'll mention it again. > > I've been on this list for 15 years. > I've been on this list for 15 years. > I've been on this list for 15 years. > I've been on this list for 15 years. > I've been on this list for 15 years. > I've been on this list for 15 years. > > Bows low to the west, in the general direction of the ranking SQL God. > > ;) > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 8:55 PM, Francisco Tapia wrote: >> Damn you John... >> ? ?You always have to throw in the number of years you've been on this >> list as if that means anything... > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- -Francisco http://bit.ly/sqlthis | Tsql and More... From paul.hartland at googlemail.com Fri Sep 24 04:48:09 2010 From: paul.hartland at googlemail.com (Paul Hartland) Date: Fri, 24 Sep 2010 10:48:09 +0100 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors - Solution Or Not Solution? In-Reply-To: <8CE87AB4DE774F9DA037FDF9BECB44C5@abpc> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <0312DC43470649218CACAB408333CE62@abpc> <4C9BDD59.5060403@colbyconsulting.com> <97F08F50ACEC491F997EA5363A2F8596@abpc> <8CE87AB4DE774F9DA037FDF9BECB44C5@abpc> Message-ID: Not had time to put either approach into action as yet today, but will let you all know when I do. Thanks to everyone again. Paul On 24 September 2010 02:29, Asger Blond wrote: > Paul > Curious: Did your question to this list give you a solution? > Asgsr > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- Paul Hartland paul.hartland at googlemail.com From jwcolby at colbyconsulting.com Fri Sep 24 05:52:59 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 24 Sep 2010 06:52:59 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <4C9BE088.9040805@colbyconsulting.com> Message-ID: <4C9C830B.9070601@colbyconsulting.com> By none other than Andy Warren of SQL Server Central http://www.sqlservercentral.com/articles/Advanced+Querying/2785/ No mention of Servers around the world blue screening from instabilities. Arrghhh... my server just shut down... *(&^%@%$^ I knew I shouldn't have used that cursor! ;) John W. Colby www.ColbyConsulting.com On 9/23/2010 8:16 PM, Asger Blond wrote: > > John, >> *with no explanation at all* > And from your previous posting: >> I don't have the time to spend hours figuring out something that I could >> learn in 5 minutes if you were kind enough to explain > > Excuse me, but I made a typo in my last reply to you. > The last row in the testing recordset should of course not be: > 222222 29/09/2010 XX2 2XX YY2 1YY > It should be: > 222222 29/09/2010 XX2 2XX YY2 2YY > > Did you get my point? We all have work to do and don't want to spend hours making long explanations and then proof-reading our explanations which sure will have pleanty of slpelling flauts. > > Asger > > -----Oprindelig meddelelse----- > Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto:dba-sqlserver-bounces at databaseadvisors.com] P? vegne af jwcolby > Sendt: 24. september 2010 01:20 > Til: Discussion concerning MS SQL Server > Emne: Re: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors > > LOL, Francisco you crack me up sometimes. > > From the SQL Server Team... > > (and YOU) > > http://www.sqlteam.com/article/cursors-an-overview > > Where in there does it say anything about unstable or unresponsive servers. > > In the real world (yours too I assume) we often need to just get it done and move on. Throwing out > a solution *with no explanation at all* that is obviously classy, but a bit dense, to someone asking > if a cursor is OK, is... less than helpful. > > I understand from past experiences with this list that there is a very definite "read BOL" > mentality. If it don't hurt, it ain't good for ya. ;) > > So Francisco, I will pretty much make whatever damned recommendations I want. Including "use a > cursor when it fits" which is what I said originally. It's a tool for God's sake. Provided by > Microsoft for God's sake. Discussed in a blog by none other than SQLTeam.com for God's sake. If > you have a problem with them, go bitch at somebody over there. > > John W. Colby > www.ColbyConsulting.com > > On 9/23/2010 4:58 PM, Francisco Tapia wrote: >> John, >> in your environment and because you are a single user, your experiences >> are more unique than most people who work with SQL Server, you can make >> decisions to use cursors on a whim that will impact your server performance, >> however, most other SQL programmers and DBA's generally need to address >> server performance and availability. Because of that I recommend that you >> (John) continue to use the tool of your choice, however, when offering >> advice on cursors, would refrain from stating when a cursor is valid. I >> would hate to have someone's server crash or become unresponsive when it >> deals with a community of users. >> >> it's also quite another thing to state that information wasn't provided, >> Asger, clearly stated that his example was a subquery example with common >> table expressions (CTE) and Rank function, all terms that can be quickly be >> researched at on Books online (BOL). >> >> >> >> -Francisco >> http://bit.ly/sqlthis | Tsql and More... >> >> >> >> >> On Thu, Sep 23, 2010 at 11:53 AM, jwcolbywrote: >> >>> OK Asger (and Francisco), but this is a perfect example of... WTF over? >>> >>> I for one haven't a clue what this code does, nor do I have a clue how to >>> figure out what this code >>> does. >>> >>> If you are going to recommend code like this you need to tell us how it >>> works, otherwise I for one >>> will go "ok, now where did I put that cursor..." >>> >>> >>> >>> John W. Colby >>> www.ColbyConsulting.com >>> >>> On 9/23/2010 12:13 PM, Asger Blond wrote: >>>> Paul, >>>> You can use a subquery to make this update. Following example uses a >>> Common Table Expression and the Rank function Row_Number(): >>>> >>>> WITH cte (RowNumber, StartCode, EndCode) AS >>>> (SELECT ROW_NUMBER() OVER (ORDER BY PersonID), StartCode, EndCode FROM >>> YourQuery) >>>> UPDATE cte SET StartCode = >>>> (SELECT EndCode FROM cte AS cte2 >>>> WHERE cte.RowNumber> 1 AND cte.RowNumber = cte2.RowNumber+1) >>>> >>>> Asger >>>> >>>> -----Oprindelig meddelelse----- >>>> Fra: dba-sqlserver-bounces at databaseadvisors.com [mailto: >>> dba-sqlserver-bounces at databaseadvisors.com] P? vegne af Paul Hartland >>>> Sendt: 23. september 2010 16:53 >>>> Til: SQLServerList >>>> Emne: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors >>>> >>>> To all, >>>> >>>> We have employees that can do more than one job in a day on our database, >>>> when an employee is assigned to a job, the application takes the persons >>>> home postcode and the jobs location postcode and stores them in a table, >>> we >>>> are now creating an estimated mileage. I can do this based on the >>> postcodes >>>> supplied but it will be inaccurate as the start postcodes will always be >>> the >>>> employees home, when the start postcode for the second job should really >>> be >>>> the postcode of the previous job location. >>>> >>>> At the moment I have a select query which returns results for an employee >>>> where they are doing more than one job in a day, result example below: >>>> >>>> PersonID Date StartCode EndCode >>>> 111111 29/09/2010 AA1 1AA BB1 1BB >>>> 111111 29/09/2010 AA2 2AA BB2 2BB >>>> 111111 29/09/2010 AA3 3AA BB3 3BB >>>> 111111 29/09/2010 AA4 4AA BB4 4BB >>>> >>>> I am thinking of putting these results into a cursor and looping through >>> the >>>> records updating the startcode with the previous records endcode, so that >>>> AA2 2AA will become BB1 1BB, AA3 3AA will become BB2 2BB etc >>>> >>>> Is this the best way to do this, or could someone point me in a better >>>> direction please. >>>> >>>> Many thanks in advance for all your help. >>>> >>> _______________________________________________ >>> dba-SQLServer mailing list >>> dba-SQLServer at databaseadvisors.com >>> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >>> http://www.databaseadvisors.com >>> >>> >> _______________________________________________ >> dba-SQLServer mailing list >> dba-SQLServer at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > From jwcolby at colbyconsulting.com Fri Sep 24 07:46:09 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 24 Sep 2010 08:46:09 -0400 Subject: [dba-SQLServer] Not the end all and be all Message-ID: <4C9C9D91.8040801@colbyconsulting.com> I am not a DBA, and I don't play one on TV (or this list). OTOH I *use* SQL Server every single workday. The other day I was doing a task I do all the time, validating addresses for a bunch of lists. The process has several steps some of which are quite time consuming. One step in particular runs UDF to calculate a code, taking as input four fields and saving the code returned in a field in the same table. *SET BASED* (of course). Another process generates an SHA1 hash on three to five fields and store them into three fields in the same table. *SET BASED* (of course). No cursors here. ;) The tables I do this on are anywhere from 3.5 million names up to 65 million names. Everything was fine until I tried to run the process on the biggest table, whereupon it "went to lunch". I started at 5 pm and by midnight when I went to bed it was still chunking away. 8:00 am when I got up to my office - still chunking away. It seems that my poor underpowered server was paging memory. Maybe. Remember I am not a SQL god so I really don't know for certain the "why" it went to lunch. In any event, trying to do this whole thing (the hash field generation in appears) as a single set on 65 million records was just unwieldy. So I broke it into cursors... ;) *JUST KIDDING*... I drive all of my processes from C#. So we went into the "big processes" and broke them down into sets or chunks of records. I am absolutely certain there is a much more efficient way to do it, and if I were an SQL God (or any kind of god) I probably would have picked a different way. However being a poor working schmuck with a job to get done, I used the tools I know. We dynamically added a "processed" field to the table which we then used to store a flag that records had been processed. We used a do while (RecordsAffected > 0) and updated 500K records at pop, updating the "processed" flag, and grabbing the top(500K) where Processed is null each time through the loop. We enclosed in transactions for good measure. Dropped the processed flag field when finished. Voila, each 500K chunk takes about 30 seconds and while it still takes a half hour to process 65 million records, it no longer goes to lunch and never comes back. The process that had not finished in 12 hours the previous day took 30 minutes yesterday. The morals of the story are (in no particular order): 1) I rarely use cursors and wasn't in this instance 2) I was using set based updates and when it tried to scale it went to lunch anyway. 3) I am not a SQL God (or any other kind of god). 4) Breaking the big set based process down into chunks (still set based) avoided the issue 5) Had I asked how to accomplish this "the right way" on *my* sql server list I would likely have received a RTBOL kind of response 6) So I used the tools I know to accomplish this - C# and command objects running dynamic SQL constructed in C# using cmd.ExecuteNonQuery. In the process I discovered that I could use an entire set (a pair in this case) of action queries, enclosed in transactions, in a single SQL statement executed by the C# command object. I tried this in order to wrap the pair of queries in a transaction, and it works just fine as long as the SQL is syntactically correct. Unfortunately the RecsAffected is a sum of the records affected of the individual action queries, and to be honest I don't know what will happen if the first action query fails in terms of error information returned back into my command object. I assume that it will be the error from the query that failed. Anyway, mission accomplished. -- John W. Colby www.ColbyConsulting.com From garykjos at gmail.com Fri Sep 24 09:20:30 2010 From: garykjos at gmail.com (Gary Kjos) Date: Fri, 24 Sep 2010 09:20:30 -0500 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: <4C9C830B.9070601@colbyconsulting.com> References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <4C9BE088.9040805@colbyconsulting.com> <4C9C830B.9070601@colbyconsulting.com> Message-ID: Play nice all you kids. ;-) We're all on the same team here. GK -- Gary Kjos garykjos at gmail.com From jwcolby at colbyconsulting.com Fri Sep 24 09:34:22 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 24 Sep 2010 10:34:22 -0400 Subject: [dba-SQLServer] SQL Server 2005 - Cursors Or Not Cursors In-Reply-To: References: <2C102B923D2645A6ADC59AB978ACEF96@abpc> <4C9BA244.9090904@colbyconsulting.com> <4C9BE088.9040805@colbyconsulting.com> <4C9C830B.9070601@colbyconsulting.com> Message-ID: <4C9CB6EE.7010903@colbyconsulting.com> Ya think? ;) John W. Colby www.ColbyConsulting.com On 9/24/2010 10:20 AM, Gary Kjos wrote: > Play nice all you kids. ;-) > > We're all on the same team here. > > > GK > > From jwcolby at colbyconsulting.com Fri Sep 24 11:01:08 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Fri, 24 Sep 2010 12:01:08 -0400 Subject: [dba-SQLServer] Clustered Index first or last In-Reply-To: <4C9BA35D.8060504@colbyconsulting.com> References: <4C9BA35D.8060504@colbyconsulting.com> Message-ID: <4C9CCB44.60702@colbyconsulting.com> For anybody interested, building the clustered index before appending records (instead of building the index after appending records) into the table decreased the time to append by roughly 30%. This is in the case where I have a PK (autoincrement) as the key for the index and am inserting records in order sorted by the PK. John W. Colby www.ColbyConsulting.com On 9/23/2010 2:58 PM, jwcolby wrote: > When I build my 65 million record table, I almost always create a clustered index, with the key > being the PK which is an autonumber integer. > > In the past I always inserted the records, then built the index. However if the records are > inserted in sorted order on the PKID, which is in ascending order, is there any reason not to create > the clustered index and then insert the records. Since the key is ascending each new record should > go at the end, no shuffling around to put things in the middle. > > It seems I would actually *save* time doing it that way. > > Yes, or no? From jwcolby at colbyconsulting.com Tue Sep 28 09:19:09 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Tue, 28 Sep 2010 10:19:09 -0400 Subject: [dba-SQLServer] New SQL Server install Message-ID: <4CA1F95D.5010104@colbyconsulting.com> I am about to bring up a new server, Windows 2008 and SQL Server 2008. I know from experience that SQL Server likes to put it's system files buried in a path in C:. Is it possible, and if so is it useful to move those files off to another file on my raid controller. As it stands, I have all of my data files on drives physically connected to a raid controller card. I will need to move that entire raid card and all the disks into the new server. I know from experience that I have to jump through some hoops to find and mount all of the databases. So my question really is, if whatever SQL Server stores in the C: path were to be put on another drive (D:) on my raid controller during the next install, the *next time* I have to go through this, can I install SQL Server and tell it "use these system files" - the ones that were in C: but now are on D: (on the raid drive)? Or am I dreaming and just need to disconnect and reconnect on the new system? -- John W. Colby www.ColbyConsulting.com From fhtapia at gmail.com Tue Sep 28 09:55:38 2010 From: fhtapia at gmail.com (Francisco Tapia) Date: Tue, 28 Sep 2010 07:55:38 -0700 Subject: [dba-SQLServer] New SQL Server install In-Reply-To: <4CA1F95D.5010104@colbyconsulting.com> References: <4CA1F95D.5010104@colbyconsulting.com> Message-ID: John, You are still going to have to go through the installation of Sql Server even if you put the *system* files over on another directory. I don't know what you would gain. Typically file management on Sql involves data files and transaction log files. It might be advantageous to put the *system* files on another partition that has more space in case you have a log file (sql service log or sql agent log) that won't roll over and continues to grow, but you ought to be able to catch that by regular maintenance of your system. More than likely what you may wish to do is to create an "unattended install" script, this way the next time you need to re-install sql server 2008, you can do so with one quick command and have everything else work *auto magically* for a quick guide on how to create your *own* unattended install script, check out this post, it's fairly step by step and easy to follow, however if you should have any trouble just post back and I'll see where I can help. http://www.codeproject.com/Articles/113480/SQL-Server-2008-R2-Unattended-Silent-Install.aspx -Francisco http://bit.ly/sqlthis | Tsql and More... On Tue, Sep 28, 2010 at 7:19 AM, jwcolby wrote: > I am about to bring up a new server, Windows 2008 and SQL Server 2008. I > know from experience that > SQL Server likes to put it's system files buried in a path in C:. Is it > possible, and if so is it > useful to move those files off to another file on my raid controller. > > As it stands, I have all of my data files on drives physically connected to > a raid controller card. > I will need to move that entire raid card and all the disks into the new > server. I know from > experience that I have to jump through some hoops to find and mount all of > the databases. > > So my question really is, if whatever SQL Server stores in the C: path were > to be put on another > drive (D:) on my raid controller during the next install, the *next time* I > have to go through this, > can I install SQL Server and tell it "use these system files" - the ones > that were in C: but now are > on D: (on the raid drive)? > > Or am I dreaming and just need to disconnect and reconnect on the new > system? > > -- > John W. Colby > www.ColbyConsulting.com > From jwcolby at colbyconsulting.com Tue Sep 28 16:12:52 2010 From: jwcolby at colbyconsulting.com (jwcolby) Date: Tue, 28 Sep 2010 17:12:52 -0400 Subject: [dba-SQLServer] New SQL Server install In-Reply-To: References: <4CA1F95D.5010104@colbyconsulting.com> Message-ID: <4CA25A54.5020400@colbyconsulting.com> Thanks Francisco, I will take a look at that unattended install thing. I already put the log files out on a separate raid volume (raid 0) just to get the speed up. John W. Colby www.ColbyConsulting.com On 9/28/2010 10:55 AM, Francisco Tapia wrote: > John, > You are still going to have to go through the installation of Sql Server > even if you put the *system* files over on another directory. I don't know > what you would gain. Typically file management on Sql involves data files > and transaction log files. It might be advantageous to put the *system* > files on another partition that has more space in case you have a log file > (sql service log or sql agent log) that won't roll over and continues to > grow, but you ought to be able to catch that by regular maintenance of your > system. > > More than likely what you may wish to do is to create an "unattended > install" script, this way the next time you need to re-install sql server > 2008, you can do so with one quick command and have everything else work > *auto magically* > > > for a quick guide on how to create your *own* unattended install script, > check out this post, it's fairly step by step and easy to follow, however if > you should have any trouble just post back and I'll see where I can help. > http://www.codeproject.com/Articles/113480/SQL-Server-2008-R2-Unattended-Silent-Install.aspx > > > > -Francisco > http://bit.ly/sqlthis | Tsql and More... > > > > > On Tue, Sep 28, 2010 at 7:19 AM, jwcolbywrote: > >> I am about to bring up a new server, Windows 2008 and SQL Server 2008. I >> know from experience that >> SQL Server likes to put it's system files buried in a path in C:. Is it >> possible, and if so is it >> useful to move those files off to another file on my raid controller. >> >> As it stands, I have all of my data files on drives physically connected to >> a raid controller card. >> I will need to move that entire raid card and all the disks into the new >> server. I know from >> experience that I have to jump through some hoops to find and mount all of >> the databases. >> >> So my question really is, if whatever SQL Server stores in the C: path were >> to be put on another >> drive (D:) on my raid controller during the next install, the *next time* I >> have to go through this, >> can I install SQL Server and tell it "use these system files" - the ones >> that were in C: but now are >> on D: (on the raid drive)? >> >> Or am I dreaming and just need to disconnect and reconnect on the new >> system? >> >> -- >> John W. Colby >> www.ColbyConsulting.com >> > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > >