Msg 134, Level 15, State 1, Line 21
The variable name '#AsOfDate' has already been declared. Variable names must be unique within a query batch or stored procedure.
I am getting this error when running a set of IF statements. Ideally I would simply need to toggle my bit to 1 for the quarter I'm interested in and then execute my code. The IF statements would set the dates I need.
I have tried this with IF and ELSE IF, but neither seems to make a difference. Any suggestions on how I can do this would be greatly appreciated. Sadly I can't simply have it pick the quarter I'm in (or the one previous) as these reports may be requested at any time.
-------------------------
-- Choose Quarter --
-------------------------
DECLARE #Q1 BIT = 0
DECLARE #Q2 BIT = 0
DECLARE #Q3 BIT = 0
DECLARE #Q4 BIT = 0
DECLARE #Annual BIT = 0
-------------------------
--Q1
IF #Q1 = '1'
BEGIN
DECLARE #AsOfDate datetime = '2016-03-31';
DECLARE #StartDate datetime = '2016-01-01'
END
--Q2
IF #Q2 = '1'
BEGIN
DECLARE #AsOfDate datetime = '2016-06-30';
DECLARE #StartDate datetime = '2016-04-01'
END
--Q3
IF #Q3 = '1'
BEGIN
DECLARE #AsOfDate datetime = '2016-09-30';
DECLARE #StartDate datetime = '2016-07-01'
END
--Q4
IF #Q4 = '1'
BEGIN
DECLARE #AsOfDate datetime = '2016-12-31';
DECLARE #StartDate datetime = '2016-10-01'
END
-- ANNUAL
IF #Annual = '1'
BEGIN
DECLARE #AsOfDate datetime = '2016-12-31';
DECLARE #StartDate datetime = '2016-01-01'
END
-- Check to ensure a time period is set
IF (#Q1 = 0 AND #Q2 = 0 AND #Q3 = 0 AND #Q4 = 0 AND #ANNUAL = 0)
BEGIN
PRINT 'NO TIME SET'
END
-- Run Code with dates
ELSE
BEGIN
EDIT:
Disregard... actually reading my error and code clearly points to my declares being an issue not the setting of the variable... Solution is to declare the variables outside of my IF statements and SET them in the statement.
I was declaring in each IF statement instead of declaring prior to all the IF statements. Below is the updated version of the code.
DECLARE #ASOfDate DATETIME
DECLARE #StartDate DATETIME
-------------------------
-- Choose Quarter --
-------------------------
DECLARE #Q1 BIT = 0
DECLARE #Q2 BIT = 0
DECLARE #Q3 BIT = 0
DECLARE #Q4 BIT = 0
DECLARE #Anual BIT = 0
-------------------------
--Q1
IF #Q1 = '1'
BEGIN
SET #AsOfDate = '2016-03-31' ; SET #StartDate = '2016-01-01'
END
--Q2
IF #Q2 = '1'
BEGIN
SET #AsOfDate = '2016-06-30' ; SET #StartDate = '2016-04-01'
END
--Q3
IF #Q3 = '1'
BEGIN
SET #AsOfDate = '2016-09-30' ; SET #StartDate = '2016-07-01'
END
--Q4
IF #Q4 = '1'
BEGIN
SET #AsOfDate = '2016-12-31' ; SET #StartDate = '2016-10-01'
END
--ANUAL
IF #Anual = '1'
BEGIN
SET #AsOfDate = '2016-12-31' ; SET #StartDate = '2016-01-01'
END
-- Check to ensure a time period is set
IF (#Q1 = 0 AND #Q2 = 0 AND #Q3 = 0 AND #Q4 = 0 AND #ANUAL = 0)
BEGIN
PRINT 'NO TIME SET'
END
-- Run Code with dates
ELSE
BEGIN
Start of Actual Program using dates shown
END
Related
I am using below function to calculate the elapsed time between 2 input timestamps. Only time spent during business hours should be calculated. Business hrs are Mon-Sat 8 am to 6 pm.
Function call syntax:
select xxxxx('2018.09.28 19:02:28','2018-09-29 10:40:35') Function is giving output as 98 mins, the correct answer is 160 mins.
Function structure is:
Create FUNCTION xxxxx (#LeadAssignTime DATETIME, #LeadContactTime DATETIME)
RETURNS VARCHAR(9)
AS
BEGIN
DECLARE #Temp BIGINT
SET #Temp=0
DECLARE #LeadAssignDay VARCHAR(9)
SET #LeadAssignDay = CONVERT(VARCHAR(9),#LeadAssignTime, 112)
DECLARE #LeadContactDay VARCHAR(9)
SET #LeadContactDay = CONVERT(VARCHAR(9),#LeadContactTime, 112)
DECLARE #StartTime VARCHAR(9)
SET #StartTime = CONVERT(VARCHAR(9),#LeadAssignTime, 108)
DECLARE #FinishTime VARCHAR(9)
SET #FinishTime = CONVERT(VARCHAR(9),#LeadContactTime, 108)
DECLARE #WorkStart VARCHAR(9)
SET #WorkStart = '08:00:00'
DECLARE #WorkFinish VARCHAR(9)
SET #WorkFinish = '18:00:00'
IF (#StartTime<#WorkStart)
BEGIN
SET #StartTime = #WorkStart
END
IF (#FinishTime>#WorkFinish)
BEGIN
SET #FinishTime=#WorkFinish
END
DECLARE #CurrentDate VARCHAR(9)
SET #CurrentDate = CONVERT(VARCHAR(9),#LeadAssignTime, 112)
DECLARE #LastDate VARCHAR(9)
SET #LastDate = CONVERT(VARCHAR(9),#LeadContactTime, 112)
WHILE(#CurrentDate<=#LastDate)
BEGIN
IF (DATEPART(dw, #CurrentDate)!=1 )
BEGIN
IF (#CurrentDate!=#LeadAssignDay) AND (#CurrentDate!=#LeadContactDay)
BEGIN
SET #Temp = (#Temp + (8*60))
END
ELSE IF (#CurrentDate=#LeadAssignDay) AND (#CurrentDate!=#LeadContactDay)
BEGIN
SET #Temp = #Temp + DATEDIFF(MINUTE, #StartTime, #WorkFinish)
END
ELSE IF (#CurrentDate!=#LeadAssignDay) AND (#CurrentDate=#LeadContactDay)
BEGIN
SET #Temp = #Temp + DATEDIFF(MINUTE, #WorkStart, #FinishTime)
END
ELSE IF (#CurrentDate=#LeadAssignDay) AND (#CurrentDate=#LeadContactDay)
BEGIN
SET #Temp = DATEDIFF(MINUTE, #StartTime, #FinishTime)
END
END
SET #CurrentDate = CONVERT(VARCHAR(9),DATEADD(day, 1, #CurrentDate),112)
END
Return #TEMP
END
You're going around your loop twice, but in the first iteration, you fall into the second IF block and setting #temp to be -62
In the second iteration, you fall into the third IF block and calculate 160 for the difference between #WorkStart and #FinishTime, but then this is added to the value already in #Temp. 160-62 = 98.
You'll need your second IF block to check if 'start time' is before 'work finish' before executing that logic.
(#CurrentDate=#LeadAssignDay) AND (#CurrentDate!=#LeadContactDay)
needs to become
(#CurrentDate=#LeadAssignDay) AND (#CurrentDate!=#LeadContactDay) AND ( #StartTime < #WorkFinish)
I haven't done any vetting beyond the one use case. Make sure to do some thorough testing.
Good morning
I have an interesting situation:
1 June 2018 = 43252 as Integer
30 May 2018 = 43250 as Integer
I try to convert a date to the first of the respective month (reporting month) with the following code:
DECLARE #InputDate DateTime, #Result Float, #Date DateTime
Set #InputDate = '2018/06/18 23:13:05'
If isDate(#InputDate) = 0
BEGIN
Set #Result = 0
END
ELSE
BEGIN
Set #Date = DATEFROMPARTS(Year(#InputDate), Month(#InputDate), 1)
Set #Result = cast (cast(#Date as datetime) as float)
Select #Date, #Result
END
The result for #Date is '2018-06-01 00:00:00.000' as expected but the result for #Result = 43250 which is 30 May 2018. How does this happen?
My procedure that I am calling works on day by day basis. I need to pass/iterate by one day upto the current date to the procedure for it to perform the action. Can some help with that. Here is the sample that I tried but it fails to work. I am beginner.
USE [aaa]
GO
DECLARE #return_value int
DECLARE #VarDate1 Datetime ='2015-09-30'
DECLARE #VarDate2 Datetime ='2015-09-30'
WHILE (#VarDate2 <= '2015-12-10')
BEGIN
EXEC #return_value = [dbo].[procname]
#tday1 = #VarDate1 ,
#tday2 = #VarDate2
SET #VarDate2 = DATEADD(DAY, 1, #VarDate2)
set #VarDate1 = #VarDate2
SELECT 'Return Value' = #return_value
END
GO
By checking your code, you're probably getting an error while trying to set the value for #VarDate1 and #VarDate2. Try the code below:
DECLARE #return_value int
DECLARE #VarDate1 Datetime = CAST('20150930 00:00:00' AS DATETIME)
DECLARE #VarDate2 Datetime = CAST('20150930 00:00:00' AS DATETIME)
WHILE (#VarDate2 <= '20151210') BEGIN
SET #VarDate2 = DATEADD(DAY, 1, #VarDate2)
SET #VarDate1 = #VarDate2
PRINT #VarDate2 --Change your logic here
END
GO
I am trying to insert the Date and Day Of The Week into a table. The Table should contain 30 days excluding Sunday. I can not get the Insert inside the case statement to work. Any help would be greatly appreciated.
Declare #StartDate as Date = DateAdd(day,-30,GetDate())
Declare #DOW varchar(10)
Declare #adddaycnt int
Declare #RecsWrite int
Set #RecsWrite = 0
Set #adddaycnt = 0
While #RecsWrite <= 30
Begin
Select #DOW = DateName(dw,DateAdd(dw,#adddaycnt,#startDate))
Select Case When #DOW <> 'Sunday' then
Insert Into temp_ProdSaleInv (ProdDate,DOW)
Values(
Convert(varchar(10),DateAdd(day, #adddaycnt,#startDate),101),
DateName(dw,DateAdd(dw, #adddaycnt,#startDate)
)
)
End
Select Case When #DOW <> 'Sunday' then
Set #RecsWrite = #RecsWrite + 1
Set #adddaycnt = #adddaycnt + 1
Else
Set #adddaycnt = #adddaycnt + 1
End
The case statements won't work like you coded. Try this with IF instead.
Declare #StartDate as Date = DateAdd(day,-30,GetDate())
Declare #DOW varchar(10)
Declare #adddaycnt int
Declare #RecsWrite int
Set #RecsWrite = 0
Set #adddaycnt = 0
While #RecsWrite <= 30
Begin
Select #DOW = DateName(dw,DateAdd(dw,#adddaycnt,#startDate))
IF #DOW <> 'Sunday'
BEGIN
Insert Into temp_ProdSaleInv (ProdDate,DOW)
Values(
Convert(varchar(10),DateAdd(day, #adddaycnt,#startDate),101),
DateName(dw,DateAdd(dw, #adddaycnt,#startDate)
)
)
END
IF #DOW <> 'Sunday'
BEGIN
Set #RecsWrite = #RecsWrite + 1
Set #adddaycnt = #adddaycnt + 1
END
Else
BEGIN
Set #adddaycnt = #adddaycnt + 1
END
End
The CASE statement evaluates a list of conditions and returns one of multiple possible result expressions.
Instead, you are trying to actually run CRUD statements in the CASE statements.
Therefore, use something like IF conditions instead.
I've had a look around the forum and I can't find anything that fits my question exactly. This query is supposed to add recurring holidays for employees. If I exclude the PL_ID variable and just have a table with no primary key, the code adds in the correct days holiday defined in the case statement, this is on a test table but needs to be added to a table with over 25,000 entries.
The table includes PL_ID, Log_Date, Out_Time, In_Time
I currently have this code:
DECLARE #StartDate DateTime
DECLARE #EndDate DateTime
DECLARE #DateVar DateTime
DECLARE #PL_ID int
SET #StartDate = '1/10/2012'
SET #EndDate = '1/11/2012'
SET #DateVar = #StartDate
SET #PL_ID = 0
WHILE #DateVar <= #EndDate
BEGIN
INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
SELECT #PL_ID,#DateVar,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN '9'
WHEN DATENAME(dw,#DateVar) = 'Monday' THEN '12'
END AS In_Time,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN 'After 5'
WHEN DATENAME(dw,#DateVar) = 'Monday' THEN 'After 5'
END AS Out_Time
WHERE DATENAME(dw,#DateVar) IN ('Thursday','Monday')
SET #DateVar = DATEADD(d,1,#DateVar)
UPDATE Logger_test2 set #PL_ID = PL_ID = #PL_ID +1
END
With this code, it only adds one row into the table with the greatest PL_ID but I need it to create a new ID for each entry, sorry it's a bit vauge. Would really appreciate your help though!
much closer to getting it right now! however if i change the date to the next month following i get duplicate key errors: Msg 2627, Level 14, State 1, Line 15 Violation of PRIMARY KEY constraint 'PK_Logger_test2'. Cannot insert duplicate key in object 'dbo.Logger_test2'. The duplicate key value is (4) - is there another way around this so that it does not try to insert duplicate keys?
SOLVED:
here is the completed code :), worked a charm and made my life alot easier! inputted everyones pre-planned holiday for the next year just by just by changing the userID:
DECLARE #StartDate DateTime
DECLARE #EndDate DateTime
DECLARE #DateVar DateTime
DECLARE #PL_ID int
DECLARE #User_ID int
DECLARE #Reason nvarchar(50)
DECLARE #Details nvarchar (500)
SET #StartDate = '30/09/2012'
SET #EndDate = '24/12/2012'
SET #DateVar = #StartDate
SET #User_ID = 55
SET #Reason = 'Day off'
SET #Details = 'Day off'
--SET #PL_ID = 0
SELECT #PL_ID = max(PL_ID) from People_Logger
WHILE #DateVar <= #EndDate
BEGIN
select #PL_ID = #PL_ID +1
SET IDENTITY_INSERT dbo.People_logger ON;
INSERT INTO People_Logger(PL_ID,LOG_Date,User_ID,Reason,Details,In_Time,Out_Time)
SELECT #PL_ID,#DateVar,#User_ID,#Reason,#Details,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN 'After 5'
WHEN DATENAME(dw,#DateVar) = 'Friday' THEN 'After 5'
END AS In_Time,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN '9'
WHEN DATENAME(dw,#DateVar) = 'Friday' THEN '9'
END AS Out_Time
WHERE DATENAME(dw,#DateVar) IN ('Thursday','Friday')
SET IDENTITY_INSERT dbo.People_Logger OFF;
SET #DateVar = DATEADD(d,1,#DateVar)
END
OK I think you just need to increment the value of your variable before you insert. Like this
WHILE #DateVar <= #EndDate
BEGIN
select #PL_ID = #PL_ID + 1
INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
SELECT #PL_ID,#DateVar,
etc etc
Set #DateVar= DATEADD(d,1,#DateVar)
-- NO Update statement
END
DECLARE #StartDate DateTime
DECLARE #EndDate DateTime
DECLARE #DateVar DateTime
DECLARE #PL_ID int
SET #StartDate = '1/10/2012'
SET #EndDate = '1/11/2012'
SET #DateVar = #StartDate
SET #PL_ID = 0
WHILE #DateVar <= #EndDate
BEGIN
INSERT INTO Logger_test2(PL_ID,Log_Date,In_Time,Out_Time)
SELECT #PL_ID,#DateVar,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN '9'
WHEN DATENAME(dw,#DateVar) = 'Monday' THEN '12'
END AS In_Time,
CASE WHEN DATENAME(dw,#DateVar) = 'Thursday' THEN 'After 5'
WHEN DATENAME(dw,#DateVar) = 'Monday' THEN 'After 5'
END AS Out_Time
WHERE DATENAME(dw,#DateVar) IN ('Thursday','Monday')
SET #DateVar = DATEADD(d,1,#DateVar)
SET #PL_ID = #PL_ID +1
END