Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL copy data in weekdays


TSQL copy data in weekdays

Author
Message
zhtway
zhtway
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 75
Hi,
I am very new and never write a TSQL code before. I started learning from yesterday.
I have those tables.
Holiday
- HolidayDate
All the Holiday dates in a year will be populated.
Routes
- RouteID(Identity), StaffID, SatDayDelivery(bit)
Pickup
- PickUpID(Identity), RouteID, StaffID, PickupDate


What I want to do is ---
1) Copy all records from Routes to Pickup in every weekdays.
2) And copy the (SatDayDelivery = True) records on Friday or the day before Holiday.

After learning I know how to copy the data but I don't know how to do complex things. Still learning..

Any help will be greatly appreciated.

rgds,
zhtway
Nakul Vachhrajani
Nakul Vachhrajani
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1824 Visits: 2127
Hello!

Can you post the CREATE TABLE scripts and some sample data generation scripts as well?

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
zhtway
zhtway
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 75
Hi,

I have just tested with SQL Agent.
I don't know how to retrieve date from Holiday table.

use testDB
Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()) from Routes
Nakul Vachhrajani
Nakul Vachhrajani
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1824 Visits: 2127
Hello!

Shouldn't your "SatDayDelivery" BIT be in the Pickup table and not in the Routes table?

The way I read it, the Routes table associates a staff member with a particular route. It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.

Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Nakul Vachhrajani
Nakul Vachhrajani
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1824 Visits: 2127
See if the following does the trick. I have used TABLE variables instead of actual tables just for the POC.


/*
Major Assumption Here:

The "SatDayDelivery" BIT should be in the Pickup table and not in the Routes table.

The way I read it, the Routes table associates a staff member with a particular route.
It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.
*/
-- Simulate the tables
DECLARE @Holiday TABLE (HolidayDate DATE)
DECLARE @Routes TABLE (RouteID INT,
StaffID INT)
DECLARE @Pickup TABLE (PickUpID INT IDENTITY (1,1),
RouteID INT,
StaffID INT,
PickupDate DATE,
SatDayDelivery BIT)
-- Holds Today's date
DECLARE @TodaysDate DATE

--Generate Test data
INSERT INTO @Holiday VALUES ('08/21/2010'), ('08/22/2010'), ('08/24/2010')
INSERT INTO @Routes VALUES (1, 1), (1, 2), (1, 3), (2, 4), (2, 5)

----Test Point# 1
--SELECT * FROM @Holiday
--SELECT * FROM @Routes

SET @TodaysDate = CONVERT(DATE, GETDATE(), 101)

--Test Point# 2 (Comment the line above and uncomment the one below to test)
--SET @TodaysDate = '08/23/2010'

INSERT INTO @Pickup (RouteID, StaffID, PickupDate, SatDayDelivery)
SELECT r.RouteID, r.StaffID, @TodaysDate AS PickupDate, CASE WHEN EXISTS (SELECT * FROM @Holiday WHERE HolidayDate = @TodaysDate) THEN 1
ELSE 0 END AS SatDayDelivery
FROM @Routes r

--Test Point# 3
SELECT * FROM @Pickup



Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Nakul Vachhrajani
Nakul Vachhrajani
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1824 Visits: 2127
The SQL2005 version of the script is:


/*
Major Assumption Here:

The "SatDayDelivery" BIT should be in the Pickup table and not in the Routes table.

The way I read it, the Routes table associates a staff member with a particular route.
It is the Pickup table that has the pickup related information, and therefore the "SatDayDelivery" should be in Pickup, not in Routes.
*/
-- Simulate the tables
DECLARE @Holiday TABLE (HolidayDate DATETIME)
DECLARE @Routes TABLE (RouteID INT,
StaffID INT)
DECLARE @Pickup TABLE (PickUpID INT IDENTITY (1,1),
RouteID INT,
StaffID INT,
PickupDate DATETIME,
SatDayDelivery BIT)
-- Holds Today's date
DECLARE @TodaysDate DATETIME

--Generate Test data
INSERT INTO @Holiday VALUES ('08/21/2010')
INSERT INTO @Holiday VALUES ('08/22/2010')
INSERT INTO @Holiday VALUES ('08/24/2010')
INSERT INTO @Routes VALUES (1, 1)
INSERT INTO @Routes VALUES (1, 2)
INSERT INTO @Routes VALUES (1, 3)
INSERT INTO @Routes VALUES (2, 4)
INSERT INTO @Routes VALUES (2, 5)

----Test Point# 1
--SELECT * FROM @Holiday
--SELECT * FROM @Routes

SET @TodaysDate = CONVERT(DATETIME, CONVERT(NCHAR,GETDATE(), 112))

--Test Point# 2 (Comment the line above and uncomment the one below to test)
--SET @TodaysDate = '08/23/2010'

INSERT INTO @Pickup (RouteID, StaffID, PickupDate, SatDayDelivery)
SELECT r.RouteID, r.StaffID, @TodaysDate AS PickupDate, CASE WHEN EXISTS (SELECT * FROM @Holiday WHERE HolidayDate = @TodaysDate) THEN 1
ELSE 0 END AS SatDayDelivery
FROM @Routes r

--Test Point# 3
SELECT * FROM @Pickup



Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
zhtway
zhtway
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 75
Hi Nakul Vachhrajani,

Thanks for your replies.

Let me explain to you what I want to do.

Routes is the Master table for all routes.
Pickup is the table for a day where all the staffs have to pickup for their routes.
And All the data in the Pickup will be copied to PickupHistory when all the packages are delivered (may be in the same day or the next days).
Saturday Delivery has to be prepared on Friday. If the Holiday is Friday, Saturday Delivery will has to prepared on Thursday.
As holiday, records will not be copied to Pickup.

That TSQL will be in the SQL Agent Job and will schedule for weekdays.
Basically my logic is like that..

if today <> holiday then
copy all the records from routes to pickup
if today = friday or today= holiday-1 then
copy all the SatDelivery records to pickup
endif
endif

Actually, I can write code in VB.net and run as job schedule or windows service, yet I am not expert.
But I know there is something I can do with TSQL and I want to learn TSQL a bit.

Thanks
zhtway
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45098 Visits: 39912
zawhtway (8/21/2010)
Hi,
I am very new and never write a TSQL code before. I started learning from yesterday.
I have those tables.
Holiday
- HolidayDate
All the Holiday dates in a year will be populated.
Routes
- RouteID(Identity), StaffID, SatDayDelivery(bit)
Pickup
- PickUpID(Identity), RouteID, StaffID, PickupDate


What I want to do is ---
1) Copy all records from Routes to Pickup in every weekdays.
2) And copy the (SatDayDelivery = True) records on Friday or the day before Holiday.

After learning I know how to copy the data but I don't know how to do complex things. Still learning..

Any help will be greatly appreciated.

rgds,
zhtway


My recommendation is to learn the basics before trying to do such a thing. You can do that at sites like w3Schools.com .

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
zhtway
zhtway
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 75
Hi,
Here is the code I have done and tested and seems working.
Correct me if something is wrong. I'll have some more to do


use MobilePOD

Declare @chkHoliday int
Declare @chkTomorrow int
declare @DayName varchar(20)
Declare @DayNo int

SELECT @chkHoliday = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE(),101)
SELECT @chkTomorrow = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE()+1,101)

set @DayNo=(DATEPART(dw, getdate()) + @@DATEFIRST) % 7

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate())
from Routes
where @chkHoliday = 0

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+1)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @DayNo = 6

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+2)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @chkTomorrow = 1 and @DayNo = 5

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45098 Visits: 39912
zawhtway (8/21/2010)
Hi,
Here is the code I have done and tested and seems working.
Correct me if something is wrong. I'll have some more to do


use MobilePOD

Declare @chkHoliday int
Declare @chkTomorrow int
declare @DayName varchar(20)
Declare @DayNo int

SELECT @chkHoliday = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE(),101)
SELECT @chkTomorrow = count(*) from holidays where CONVERT(CHAR(10),freedate,101) = CONVERT(CHAR(10),GETDATE()+1,101)

set @DayNo=(DATEPART(dw, getdate()) + @@DATEFIRST) % 7

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate())
from Routes
where @chkHoliday = 0

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+1)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @DayNo = 6

Insert into Pickup (StaffID, RouteID, PickupDate)
select StaffID, RouteID, (getdate()+2)
from Routes
where @chkHoliday = 0 and Routes.SatDayDelivery = 1 and @chkTomorrow = 1 and @DayNo = 5


Ask yourself if you'll have the correct number of entries if the second INSERT kicks in according to the criteria... if you think so, then look at the first INSERT and see why you'll end up with double the number of rows you expect.

Also, did you actually want times on the PickupDate your inserting into the PickupTable with?

Be advised that @@DateFirst is fickle if someone changes it. @@DateFirst should probably NOT be used here.

Last but not least, the SELECTs you used on the Holiday table make it impossible for an index to be used correctly in at least two different ways. We really need for you to generate the CREATE TABLE statements for the Holidays and Routes tables along with their indexes. Start by right clicking on each table and look for "Script Table as" and follow your nose. If it doesn't script the indexes at the same time, then do the same for the indexes.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search