I am getting data from a table and I am writing off to the report through a stored procedure. In the reporting part it has to show the results by users defined date.
E.g:
Today - should show today's results only
Yesterday - should show Yesterday's results only
How should I declare the values in a stored procedure and how can I implement in ssrs reporting part?
My CreateDate column keeps changing has the table (FailedJobsInfo) gets updated.
Here is my stored procedure:
ALTER PROCEDURE [dbo].[FailedJobsInfo]
#StartDate DATETIME
AS
SELECT
[SQLServer]
,[JobName]
,[StepName]
,[FailureMessage]
,[RunDateTime]
,[RunDuration]
,[CreateDate]
FROM
[dbo].[FailedJobsInfo]
WHERE
CONVERT(varchar(20), CreateDate, 101) = CONVERT(varchar(20), GETDATE(), 101)
Starting from your SSRS Report.
1) Create a Parameter with Labels and Values as below
Label Value
Today 0
Yesterday 1
2) Make your Procedure to accept an INT Parameter, If user selects Today it will pass zero 0 to stored procedure, if user selects Yesterday it will pass procedure 1 to procedure and this Integer value will be subtracted from your getdate() value avoiding time part. you will need to do as follow :
ALTER PROCEDURE [dbo].[FailedJobsInfo]
#StartPeriod INT
AS
BEGIN
SET NOCOUNT ON;
SELECT
[SQLServer]
,[JobName]
,[StepName]
,[FailureMessage]
,[RunDateTime]
,[RunDuration]
,[CreateDate]
FROM
[dbo].[FailedJobsInfo]
WHERE
CAST(CreateDate AS DATE) = CAST(GETDATE() AS DATE) - #StartPeriod --<--
END
Related
Table structure:
ID int
status varchar (10)
DATE_CREATED datetime
DATE_CLOSED datetime
Stored procedure for filling DATE_CREATED:
CREATE PROCEDURE [dbo].[...]
#ID INT,
#STATUS VARCHAR(10) = 'Open',
#DATE_CREATED DATETIME = NULL
AS
SET NOCOUNT ON
UPDATE table
SET STATUS = #STATUS,
DATE_CREATED = COALESCE(#DATE_CREATED, GETDATE())
FROM table
From that point the column DATE_CLOSED is NULL. I wanted to automatically fill the column with the date of DATE_CREATED column but with the time of 10pm, and it should be filled by 10pm automatically and also the status filled to 'closed'.
You can calculate 10 p.m. on the current date using:
dateadd(hour, 22, cast(cast(getdate() as date) as datetime))
This is easy to add into the update. I'm not 100% sure, though, that this is all you are asking for.
If you are looking to schedule a query or stored procedure to run at a set time of the day, then the normal approach is to use SQL Server Agent which has fairly elaborate job scheduling functionality.
Look at this question for an example of how to run a daily job.
how to schedule a job for sql query to run daily?
And here's Microsoft's documentation: https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job
I pass two date range to my procedure then I select last date of each and every month as below
create procedure stcheck
#From_Date date,
#To_Date date
as
begin
declare #tmpTable table (dates date)
declare #startDate date = #From_Date
declare #endDate date = #To_Date
while #startDate <= #endDate
begin
insert into #tmpTable (dates) values (#startDate)
set #startDate = DATEADD(DAY, 1, #startDate)
end
select max(dates) as [Last day] from #tmpTable as o
group by datepart(YEAR, dates), datepart(MONTH, dates)
If am getting result as follows
Last day
2017-01-31
2017-02-28
2017-03-31
2017-04-30
I need to pass some data to temporary table by using these date output
Ex :-
Insert #xyz
Select * from abcd where postdate <=’2017-01-31’
And my requirement is that these all dates should be automatically used and relevant data should pass to #table.
Hi expert . thank for your early reply.
In brief my requirement is as follows. Please help because I am new with sql.
Think,I have a table as ‘stock’ with in and out stock transaction and stock
report can be taken as follows to any as at date
SELECT item ,SUM(InQty-OutQty) FROM stock
WHERE TransDate <= #AsatDate
GROUP BY item
Having SUM(InQty-OutQty) > 0
If I need to get the each and every month end stock when I am passing the date range
from #From date= ‘2017/05/01’ to #AsatDate=‘2017/09/30’ .
The stock report result should be appear something like this.
STOCK REPORT EXAMPLE
Please help what can be done?
NB:- it should be able to pass any date range when it is required.
Your question is a little unclear, but I'm assuming you want to run the stored procedure and insert data into the temp table #xyz (assumes temp table #xyz already exists):
insert into #xyz
exec stcheck #From_Date, #To_Date
Now off on a little tangent:
I'd also look at table-valued functions if you need parameterized, deterministic tabular output like this. I agree with #John that you should avoid looping if possible. One possible way to achieve this is instead of iterating day by day you use a pre-populated calendar table (stick say 50-100 years worth of future calendar dates in a table and use this in calculations - there are tons of examples of how to do this online). Another method is to use a recursive CTE or a numbers table, both outlined in answers here.
Once your calendar dates are easily queryable, you can stick all this in a view or inline table-valued function and it will be super fast and much clearer. If you regularly use "last day of month" as a filter criteria, you could even pre-calculate these as a column in your calendar table. Here's what it could end up like:
insert into #xyz
select dbo.DatesBetween(#From_Date, #To_Date)
You could even use dbo.DatesBetween(#From_Date, #To_Date) directly in your query in place of the temp table, if it's only used once.
I am using SQL Server 2014 with a table named dbo.ClientRecords. In this table I have a DateOfBirth field set as DATETIME and an Age field set as Nvarchar(40) at the moment. I know that this needs to be changed to int and I will do this.
I have the following query that essentially does a calculation based on the DateOfBirth field and the current date to work out the Age of the person. This works as I require it to.
SELECT id, DateOfBirth,
GETDATE() As [Today],
DATEDIFF (YY,DateOfBirth,GETDATE()) -
CASE
WHEN DATEADD(YY,DATEDIFF(YY,DateOfBirth,GETDATE()),DateOfBirth)
> GETDATE() THEN 1
ELSE 0
END AS [Age]
FROM dbo.ClientRecords
I am following the below website to create an update trigger as I need this update the age field once the insert statement for the web form has been submitted.
Tutorial about Triggers
This is my trigger that im trying to create, but I just cant seem to get this working.
CREATE TRIGGER [dbo].[Age_INSERT]
ON [dbo].[ClientRecords]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #id DATETIME
DECLARE #Age Varchar(40)
SELECT id,DateOfBirth,
GETDATE() As [Today],
DATEDIFF (YY,DateOfBirth,GETDATE()) -
CASE
WHEN DATEADD(YY,DATEDIFF(YY,DateOfBirth,GETDATE()),DateOfBirth)
> GETDATE() THEN 1
ELSE 0
END AS [Age]
FROM dbo.ClientRecords
INSERT INTO ClientRecords
VALUES(#id, #Age)
END
The error message that I get is the following.
Msg 213, Level 16, State 1, Procedure Age_INSERT, Line 21
Column name or number of supplied values does not match table definition.
I'm failing as im not to sure what I need to set as a variable. I thought that I would need the id from the ClientRecords table and the Age field.
I have a table with a date field, and I want to execute a certain query when that date reaches the current date. Is there any way to do that on Sql Server?
One way or say good way is making a Sql - job and second is trigger. But trigger is not suitable and also not advisable to use.
Sqljob is automated process, just give date-time with day and it will executed automatically. In that you call the sp, where first check the date and then process as you want.
First create a job, please check the link
http://www.databasedesign-resource.com/sql-server-jobs.html
http://forums.asp.net/p/1364020/2826971.aspx
Now create a sp which executed by job like (This is sample example)..
create procedure spname()
as
begin
declare #currentdate datetime = getdate()
declare #tempdateToCheck datetime = (select datecolumn from tablename where ....give condition if any )
if(#tempdateToCheck >= #currentdate)
begin
--execute statement you want like
insert tablename .... --insert statement
update tablename.... --update statement
declare #tempvariable1 int --anydatatype you define and any no. of variable, you want.
select #tempvariable1 = columnname from tablename
update tablename1 set columname = #tempvariable1 where condition
end
end
You can create a sql server agent job in order to check the date column everyday and if reached then execute query.
First, here is the code for sp_GetWorkQByUserName:
ALTER PROCEDURE [dbo].[sp_GetWorkQByUserName]
( #UserName varchar(50),
#StartDate datetime,
#EndDate datetime )
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT SpotId FROM tblSpotCount WHERE StoreNum = EXECUTE sp_GetUserLocationCodes(#UserName)
ORDER BY SpotDt ASC
END
I know my SELECT DISTINCT statement is wrong, but I wrote it like that to help show what I'm trying to do. I want to run this stored procedure based on the results from the sp_GetUserLocationCodes with a parameter of #UserName.
From what I can tell, my problem lies in how I'm calling sp_GetUserLocationCodes.
Question: how can I run a SELECT DISTINCT query on tblSpotCount.SpotId based on the results from the sp_GetUserLocationCodes stored procedure?
You cannot use a stored procedure directly in a query. You can, however, insert the results of a stored procedure into a temporary table and use that in your query:
CREATE TABLE #storeLocations
(
-- appropriate column names and data types go here
)
INSERT INTO #storeLocations (put column list here)
EXECUTE sp_GetUserLocationCodes(#UserName)
SELECT DISTINCT SpotId
FROM tblSpotCount
WHERE EXISTS (SELECT 1
FROM #storeLocations
WHERE #storeLocations.StoreNum = tblSpotCount.StoreNum)
ORDER BY SpotDt ASC
DROP TABLE #storeLocations