# Thursday, May 12, 2011

Common Table Expression to produce a list of weeks in Sql

One of the many nice things about the current iterations of SQL since s05 is the inclusion of Common Table Expressions (CTE). A CTE is a 'derived table', which means that it can replace the temporary tables that we have used on occasion as well as functioning as a temporary View. Because these are restricted to the query you do not have to worry about their existence once the query is gone.

One of my favorite tricks with a CTE is to use it for a list of weeks, which I can then join with tables that have dates to sort them. This allows me to do an outer join and include weeks that have no data in the physical table itself. I wish I could point you to the discussion that stirred this idea, but I cannot find it anymore...

Example:

DECLARE @startDate DateTime = '9/1/10'
DECLARE @endDate DateTime = '8/31/11'
   
DECLARE @startOfWeek DateTime = DATEADD(day, -(Datepart(dw, @startDate) - 1), @startDate)

With Weeks (StartOfWeek, EndOfWeek, weekNumber, yearFor) AS
( select
    @startOfWeek as StartOfWeek,
    DATEADD(DAY, 6, @startOfWeek) as EndOfWeek,
    DATEPART(WEEK, @startOfWeek),
    DATEPART(YEAR, @startOfWeek)
  UNION ALL
  select
    DATEADD(DAY, 7, StartOfWeek),
    DATEADD(DAY, 7, EndOfWeek),
    DATEPART(WEEK, DATEADD(day, 7, StartOfWeek)),
    DATEPART(YEAR, DATEADD(day, 7, StartOfWeek))
  from
    Weeks
  where
    EndOfWeek <= @endDate
),
BillingSummary(weekNumber, yearFor, ClientId, TotalHours) AS
( Select
    DATEPART(WEEK, DateFor) As weekNumber,
    DATEPART(YEAR, DateFor) As YearFor,
    ClientId,
    SUM(HoursBilled) AS TotalHours
  From Billing
  Where DateFor >= @startDate And DateFor <= @endDate
      Group By ClientId, DATEPART(YEAR, DateFor),DATEPART(WEEK, DateFor)
 )
  Select W.StartOfWeek, W.EndOfWeek,
    IsNull(S.ClientId,'') AS ClientId, IsNull(S.TotalHours,0) As TotalHours
   From Weeks W
    Left Outer Join BillingSummary S ON W.yearFor = S.yearFor AND W.weekNumber = S.weekNumber
    Order By W.startOfWeek Desc

Hope this helps...

# Thursday, June 24, 2010

Sql 2008 'truncate_only' is not a recognized BACKUP option.

If you have ever had to truncate a log in Sql 2005, the 'with truncate_only' is fairly familiar. Unfortunately for you, if you have it in your scripts, it has been removed in Sql 2008. 

You can use the Sql Management Studio to shrink the log files, I can post later about how to do this, but for some databases it will appear not to work (even if it throws no error). The log files stay the same size.

What may be the issue is that the Database is using a Full vs a Simple backup recovery method. You can find out more about the difference between the two here -> http://msdn.microsoft.com/en-us/library/ms187048.aspx. What we are concerned about is the note show below:

NoteNote
Under the simple recovery model, the transaction log is automatically truncated to remove any inactive virtual log files. Truncation usually occurs after each checkpoint but can be delayed under some conditions. For more information, see Transaction Log Truncation.

What this means in the short run is that by switching your backup recovery method to Simple, you will instantly be able to shrink your log file.

Ex: Alter Database %your Db Name Here% Set Recovery Simple

I would be remiss if I did not mention this note from the page on the Simple recovery method:

Important noteImportant
The simple recovery model is inappropriate for production systems for which loss of recent changes is unacceptable. In these cases, we recommend that you use the full recovery model. For more information, see Backup Under the Full Recovery Model.

One could always switch it to Simple, truncate, and switch back to Full, but it would probably be better to keep it at Full and establish a more functional means to keep those pesky log files small...

# Friday, March 12, 2010

Sharepoint 2010 Products Configuration Wizard gives you "Unhandled exception" 0x80070005 (E_ACCESSDENIED)

Trying to run the SP 2010 Config Wizard to resolve a Search issue I ran into the problem shown below.



The resolution was that I was logged in with an account that was not used in the SQL database (remember you need the perms)...
# Tuesday, February 09, 2010

Sql 2008 and 'Agent XPs' component is turned off as part of the security configuration of this server when creating a Maintenance Plan

If you try to create a Maintenance Plan on Sql 2008 you may get the following error:
'Agent XPs' component is turned off as part of the security configuration of this server. A system administrator can enable the use of 'Agent XPs' by using sp_configure....

The reason is simple - the Sql Server Agent Service, which handles Maintenance plans among other things, is not started automatically. The service is set to Manual start. The solution is simple - just set it to Automatic (so it starts in case of a reboot) and start it up!

You can, of course, do this through windows services, but you can also do it through the Sql tools - specifically the "Sql Server Configuration Manager".

Step 1 - open up the Sql Server Configuration Manager and click on 'Sql Server Services in the left pane. You will see that the SQL Server Agetn is Stopped and has its 'Start Mode' set to Manual.




Step 2 - double click on the Sql Server Agent in the right tab and its properties will appear. Click on the 'Start' button to begin it running.




Step 3 - now click on the 'Service' tab and change the Start Mode to Automatic. This will ensure that if your server restarts this service will start as well (and hence you will get your Maintenance plans.




Now, give yourself a raise and take the rest of the day off....