Cannot delete rows from a temporal history table

If you make the DELETE dynamic, your stored procedure will successfully ALTER the table, DELETE the records in question, and then ALTER it back. CREATE PROCEDURE [dbo].[OrderHistoryDelete] (@Id UNIQUEIDENTIFIER) AS BEGIN DECLARE @sql VARCHAR(MAX) BEGIN TRANSACTION ALTER TABLE [dbo].[Order] SET ( SYSTEM_VERSIONING = OFF ) SET @sql=”DELETE FROM [dbo].[OrderHistory] WITH (TABLOCKX) WHERE [Id] = “” … Read more

Previous Monday & previous Sunday’s date based on today’s date

Easy: –start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0) –end of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6) EDIT: The below will handle the Sunday date issue. DECLARE @input varchar(10) –SET @input=”9/9/2012″ — simulates a Sunday SET @input = GETDATE() –start of last week SELECT DATEADD(wk, DATEDIFF(wk, 6, CASE DATEPART(dw,@input) WHEN 1 … Read more

How to group by a Calculated Field

Sure, just add the same calculation to the GROUP BY clause: select dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))), sum(mwspp.QtyRequired) from manufacturingweekshortagepartpurchasing mwspp where mwspp.buildScheduleSimID = 10109 and mwspp.partID = 8366 group by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))) order by dateadd(day, -7, Convert(DateTime, mwspp.DateDue) + (7 – datepart(weekday, mwspp.DateDue))) … Read more