473,987 Members | 39,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populate table with stored proc

I am looking to populate a Schedule table with information from two
other tables. I am able to populate it row by row, but I have created
tables that should provide all necessary information for me to be
able
to automatically populate a "generic" schedule for a few weeks or
more
at a time.

The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
shift_position
A DaysOff table contains:
(pk) emp_id, dayoff_1, dayoff_2 <-- the days off are entered in day
of
week (1-7) form
A CalendarDays table contains:
(pk) date, calendar_dow <-- dow contains the day of week number (as
above) for each day until 2010.
My main question is how to put all of this information together and
have SQL populate the rows with data based on days off, for a few
weeks in advance. Any
suggestions?

Apr 24 '07 #1
4 6580
Nate (na**********@w estecnow.com) writes:
I am looking to populate a Schedule table with information from two
other tables. I am able to populate it row by row, but I have created
tables that should provide all necessary information for me to be able
to automatically populate a "generic" schedule for a few weeks or more
at a time.

The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
shift_position
A DaysOff table contains:
(pk) emp_id, dayoff_1, dayoff_2 <-- the days off are entered in day of
week (1-7) form
A CalendarDays table contains:
(pk) date, calendar_dow <-- dow contains the day of week number (as
above) for each day until 2010.
My main question is how to put all of this information together and have
SQL populate the rows with data based on days off, for a few weeks in
advance. Any suggestions?
The problem looks kind of interesting, but alas the hour is late here,
so I'm not able to compose a solution right now. But I would like some
clarifications:

1) Which version of SQL Server are you using?
2) Do I understand the DaysOff table correctly that this table details
two days in the week a certain employer never works, for instance
one bloke is always free on Tuesdays and Fridays?
3) What is supposed to go into shift_employee and shift_position?

It would be great if you could post:
1) CREATE TABLE statements for your tables.
2) INSERT statements with sample data (at least for the first two tables).
3) The desired result given the sample.

This makes it easy to test a solution. It also helps to clarify your
problem.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 24 '07 #2
It is a little bit unclear based on your tables to figure out how to
transform the days off to the schedule (what are those shifts, start/end
datetime, etc.). But here is a way to pull the schedule data together based
on your calendar table and the table with days off.

The first step is to normalize the DaysOff table. You can either redesign
the table and have it with only emp_id and dayoff columns and PK (emp_id,
dayoff), or if redesign is not possible then use a view, like this:

CREATE VIEW EmployeeDaysOff
(emp_id, dayoff)
AS
SELECT emp_id,
dayoff_1
FROM DaysOff
UNION ALL
SELECT emp_id,
dayoff_2
FROM DaysOff;

Then pulling the schedule based on your calendar table and this view becomes
a simple query:

DECLARE @start DATETIME
DECLARE @end DATETIME

SET @start = '20070429'
SET @end = '20070512'

SELECT E.emp_id,
C.date
FROM DaysOff AS E, CalendarDays AS C
WHERE C.date BETWEEN @start and @end
AND NOT EXISTS
(SELECT *
FROM EmployeeDaysOff AS O
WHERE O.emp_id = E.emp_id
AND O.dayoff = C.calendar_dow) ;

Notes:
- You can pass those parameters (@start and @end) to your stored procedure,
that will be the date range to open schedule for
- In the query I used DaysOff to get all employees. But you probably have a
table with employees and should replace it with that. The query above will
not produce the correct results if you have employees that do not have days
off.

If you are on SQL Server 2005 you can use the EXCEPT, like this:

SELECT E.emp_id,
C.date
FROM DaysOff AS E, CalendarDays AS C
WHERE C.date BETWEEN @start and @end
EXCEPT
SELECT O.emp_id,
C.date
FROM EmployeeDaysOff AS O, CalendarDays AS C
WHERE C.date BETWEEN @start and @end
AND O.dayoff = C.calendar_dow;

The same note to replacing DaysOff with the table with employees apply for
the here (for the first query before EXCEPT).

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Apr 24 '07 #3
Just to clarify, you can still get the same results without normalizing the
DaysOff table... :)

SELECT E.emp_id,
C.date
FROM DaysOff AS E, CalendarDays AS C
WHERE C.date BETWEEN @start and @end
AND NOT EXISTS
(SELECT *
FROM DaysOff AS O
WHERE O.emp_id = E.emp_id
AND C.calendar_dow IN (O.dayoff_1, O.dayoff_2));

Plamen Ratchev
http://www.SQLStudio.com
Apr 24 '07 #4
In addition, if you meant to have the data into the schedule table by
listing the ranges of dates between the days off for each employee, you can
get the data like this (ROW_NUMBER assumes SQL Server 2005 is used):

SELECT emp_id,
MIN(date) AS StartDate,
MAX(date) AS EndDate
FROM (
SELECT E.emp_id,
C.date,
C.date - ROW_NUMBER() OVER(PARTITION BY E.emp_id
ORDER BY C.date) AS RangeGroup
FROM DaysOff AS E, CalendarDays AS C
WHERE C.date BETWEEN @start and @end
AND NOT EXISTS
(SELECT *
FROM EmployeeDaysOff AS O
WHERE O.emp_id = E.emp_id
AND O.dayoff = C.calendar_dow) ) AS S
GROUP BY emp_id, RangeGroup;

Plamen Ratchev
http://www.SQLStudio.com
Apr 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3268
by: Alex | last post by:
I have a "source" table that is being populated by a DTS bulk import of a text file. I need to scrub the source table after the import step by running appropriate stored proc(s) to copy the source data to 2 normalized tables. The problem is that table "Companies" needs to be populated first in order to generate the Identity ID and then use that as the foreign key in the other table. Here is the DDL: CREATE TABLE . (
35
3418
by: .:mmac:. | last post by:
I have a bunch of files (Playlist files for media player) and I am trying to create an automatically generated web page that includes the last 20 or 30 of these files. The files are created every week and are named XX-XX-XX.ASX where the X's represent the date i.e. 05-22-05.asx The files are a specific format and will always contain tags like the following: <TITLE>My media file title</TITLE> <AUTHOR>Media file author</AUTHOR> <Ref href =...
4
4974
by: Robin Tucker | last post by:
Hi, I'm trying to determine with my program whether or not a given database supports a given feature set. To do this I'm querying for certain stored procedures in the sysobjects table and if they are present, making the assumption the database will support the given feature. The problem is I can't find a certain stored procedure in the sysobjects table, even though I know it exists and can see other similar procedures using: select...
2
10531
by: hubert.trzewik | last post by:
Hello, Is it possible to EXEC stored procedure from a query? I want to execute stored procedure for every line of SELECT result table. I guess it's possible with cursors, but maybe it's possible to make it easier. Give an example, please.
3
2711
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to populate multiple webpages containing customer information. There isn't a one-to-one correlation between the stored procedure and a webpage. In other words, a webpage may have to refer to 1 or more DataTables to populate itself. Therefore, a...
5
2347
by: Uwe C. Schroeder | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, maybe my mind is stuck, but here's something strange. This is the classic "counter" thing, where you can't / won't use sequences. Basically I need to assemble an identifier like
5
2120
by: John | last post by:
Hi all, I'm sorry I'm reposting this but the original was urgent and I do need closure on this. I'm calling a stored proc which does 4 "selects" and then I populate a dataset looping through the dr using a dr.NextResult() but the stored proc may not return any results for one or more Select stetments. When this happens, a table is not returned so the dataset.Fill() does not populate conatin 4 tables which then each bind to a grid -...
2
2049
by: Nate | last post by:
I am looking to populate a Schedule table with information from two other tables. I am able to populate it row by row, but I have created tables that should provide all necessary information for me to be able to automatically populate a "generic" schedule for a few weeks or more at a time. The schedule table contains: (pk) schedule_id, start_datetime, end_datetime, shift_employee,
2
9351
by: cookie0311 | last post by:
Hi I am trying to place three DropDownLists inside a Repeater, each DLL is dependent on it’s previous DDL’s selection. I’m developing in asp.net and VB. An Initial DDL (inside the repeater) needs to be populated under Page_load () through a stored proc, the selection from this needs to fire another stored proc and populate it’s child DDL. My main concern at this moment is how to populate the first DDL under page_load. I am able to do this...
0
10395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11477
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10975
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6475
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5227
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.