–get first day of last month:
,-(DAY(DATEADD(mm,1,@now))-1),
–get the last day of last month
,-(DAY(@now)),@now),101)
,-(DAY(@now)-1),@now),101)
–get the last day of current month
,-(DAY(DATEADD(mm,1,@now))),DA
–get first day of last month:
,-(DAY(DATEADD(mm,1,@now))-1),
–get the last day of last month
,-(DAY(@now)),@now),101)
,-(DAY(@now)-1),@now),101)
–get the last day of current month
,-(DAY(DATEADD(mm,1,@now))),DA
BEGIN TRY
— do stuff
END TRY
BEGIN CATCH
DECLARE @PROC AS VARCHAR(100)
DECLARE @SERVER AS VARCHAR(200)
DECLARE @ERROR AS VARCHAR(4000)
SELECT @PROC = ERROR_PROCEDURE(),
@SERVER = @@SERVERNAME,
@ERROR = ERROR_MESSAGE();
END CATCH;
Scenario: Need to write a query based on two tables. The tables have a one-to-many relationship. The “bug_comments” table can have many records for one table in the “bug” table. However, I do not want all of the records in the “bug_comments” table, I only want the most recent record. Also, I don’t want to write a subquery or use a cursor.
WITH cte (bc_bug, bc_id)
AS
(SELECT bc_bug, MAX(bc_id)
FROM bug_comments
WHERE LOWER(bc_type) = ‘comment’
GROUP BY bc_bug )
SELECT DISTINCT
b.bg_id AS [ID],
b.bg_short_desc AS [DESCR],
bc.bc_comment AS [COMMENT]
FROM bugs AS b LEFT OUTER JOIN
bug_comments AS bc ON b.bg_id = bc.bc_bug LEFT OUTER JOIN
cte ON cte.bc_bug = b.bg_id AND cte.bc_id = bc.bc_id
WHERE b.bg_status = 1 and
((cte.bc_bug = bugs.bg_id and cte.bc_id = bc.bc_id)
ORDER BY b.bg_id DESC
This error can occur when the error log file does not match the database file or when the .mdb file is detected as being corrupt.
There are multiple ways to fix the problem and the solution depends on whether or not your database has data that is critical for you to keep. For me, I only get this error on my local box where I’m conducting allot of experiments and tests so that there is no need for me to worry about critical data. This makes the restoration task much more simple.
First, I try the following canned stored procedure : sp_resetstatus. If this fails then the following always works: