Try this procedure instead:
CREATE PROCEDURE dbo.spGetAdminTotalYTD_AB -- always use schema prefix!
@Begin_Date DATETIME,
@End_Date DATETIME,
@program_id INT = NULL -- use a name consistent with the column
AS
BEGIN
SET NOCOUNT ON; -- always use SET NOCOUNT ON and semi-colons
DECLARE @year INT = 2014; -- why should this be hard-coded?
-- So you can refactor the code in January?
-- And again the following January?
-- let's get the beginning of this year instead:
DECLARE @thisyear DATETIME = DATEADD(DAY,
1-DATEPART(DAYOFYEAR, GETDATE()), DATEDIFF(DAY,0,GETDATE()));
-- if @Begin_Date pre-dates that, make it Jan 1:
DECLARE @startrange DATETIME = CASE
WHEN @Begin_Date < @thisyear THEN @thisyear ELSE @Begin_Date END;
-- make sure the end of the range is actually at the
-- end of the month *before* the end date they actually
-- specify. Assumes they're basing this on GETDATE().
-- If they pass the last day of the month, you may need
-- to add logic to make that a valid selection.
DECLARE @endrange DATETIME = DATEADD
(
DAY,
1-DATEPART(DAY, DATEADD(DAY, DATEDIFF(DAY,0,@End_Date),0)),
DATEADD(DAY, DATEDIFF(DAY,0,@End_Date), 0)
);
;WITH x AS
(
SELECT company_id, dealer_id,
[Jan],[Feb],[Mar],[Apr],[May],[Jun],
[Jul],[Aug],[Sep],[Oct],[Nov],[Dec],
-- to group YTD by company_id + dealer_id:
YTD = [Jan]+[Feb]+[Mar]+[Apr]+[May]+[Jun]
+[Jul]+[Aug]+[Sep]+[Oct]+[Nov]+[Dec]
-- to group YTD by only company_id:
YTD2 = SUM([Jan]+[Feb]+[Mar]+[Apr]+[May]+[Jun]
+[Jul]+[Aug]+[Sep]+[Oct]+[Nov]+[Dec])
OVER (PARTITION BY Company_id)
FROM
(
SELECT company_id, dealer_id,
[Jan] = COALESCE([Jan],0), [Feb] = COALESCE([Feb],0), [Mar] = COALESCE([Mar],0),
[Apr] = COALESCE([Apr],0), [May] = COALESCE([May],0), [Jun] = COALESCE([Jun],0),
[Jul] = COALESCE([Jul],0), [Aug] = COALESCE([Aug],0), [Sep] = COALESCE([Sep],0),
[Oct] = COALESCE([Oct],0), [Nov] = COALESCE([Nov],0), [Dec] = COALESCE([Dec],0)
FROM
(
SELECT m = LEFT(DATENAME(MONTH,DATEADD(MONTH,
DATEDIFF(MONTH,0,Funded_date), 0)),3),
company_id,
dealer_id,
mc = COUNT(*)
FROM dbo.tContract
WHERE program_id = @program_id
AND Funded_date >= @startrange
AND Funded_date < @endrange -- don't use BETWEEN for range queries
GROUP BY LEFT(DATENAME(MONTH,DATEADD(MONTH,
DATEDIFF(MONTH,0,Funded_date), 0)),3),
company_id,
dealer_id
)
AS x
PIVOT
(
MAX(mc) FOR m IN
(
[Jan],[Feb],[Mar],[Apr],[May],[Jun],
[Jul],[Aug],[Sep],[Oct],[Nov],[Dec]
)
) AS p
) AS y
)
SELECT
comp.name, deal.dealer_code, -- don't use meaningless aliases like a,b,c,d
cont.last_name, cont.city, cont.[state], cont.phone,
x.[Jan],x.[Feb],x.[Mar],x.[Apr],x.[May],x.[Jun],
x.[Jul],x.[Aug],x.[Sep],x.[Oct],x.[Nov],x.[Dec],
x.YTD, x.YTD2
FROM x
INNER JOIN dbo.tDealer AS deal
ON x.dealer_id = deal.dealer_id
INNER JOIN dbo.tCompany AS comp
ON x.company_id = comp.company_id
INNER JOIN dbo.tContact AS cont
ON deal.contact_id = cont.contact_id;
END
GO
Many of the comments have a lot of background, and it's not just me ranting about how you should write code like I do:
I could not put this as comment so here it is for you. It is not compete answer to your question because your question can either be correctly answered by person working in Microsoft or Paul Randal(I guess). All I can say for all paractical purposes every information is logged.
You can read the contents of log file using undocumented command
select * from fn_dblog(Null,Null)
If you run it you can see lot of information related to database, pages,extents,locks ect. But it would be difficult for you to extract information from it as it requires a level of expertise to decipher the output.
If you read Books Online document SQL Server Transaction Log architecture and Management it says
Many types of operations are recorded in the transaction log. These
operations include:
•The start and end of each transaction.
•Every data modification (insert, update, or delete). This includes
changes by system stored procedures or data definition language (DDL)
statements to any table, including system tables.
•Every extent and page allocation or deallocation.
•Creating or dropping a table or index.
Log records for data modifications record either the logical operation performed or they record the before and after images of the modified data. The before image is a copy of the data before the operation is performed; the after image is a copy of the data after the operation has been performed.
AFAIK there is no information about query which was fired but the changes which query made is written in transaction log. Changes made to page, extents , locks that were taken, resources that were locked.
Best Answer
You will want to use the
sys.dm_exec_query_stats
dmv. If you simply want to see the last statement that was executed, this should get you started (note: you need to execute this in the context of your database e.g. by using theUSE <db_name>
command):