Groups | Blog | Home
all groups > sql server new users > june 2005 >

sql server new users : Insert into


Tango
6/27/2005 11:51:01 PM
Hi
can i please have some assistance around performing a sql query that inserts
data based on a date range.

for example my initial query is
INSERT INTO tbl_master
([date], unavailableemployeeid, reasonid,
unavailablehours)
VALUES (, 1234, 12, 7)

but i want to insert a record for say 01/06/05 - 30/06/05.

Id also like to be able to not add a record for sundays.

Thankyou in advance

Jens Süßmeyer
6/28/2005 10:37:31 AM
That doens´t work. you have to do that on a Loop or use an calendar table to
query against.

DECLARE @i int
SET @i = 0

WHILE @I<10
BEGIN
Select DATEADD(DD,@i,GETDATE())
SET @I = @I+1
--OR Place yourINSERT Script in here
END


HTH, Jens Suessmeyer.


"Tango" <Tango@discussions.microsoft.com> schrieb im Newsbeitrag
news:63670890-8A50-4C48-B631-2F8D2D7E5F1A@microsoft.com...
[quoted text, click to view]

Hugo Kornelis
6/30/2005 11:38:28 AM
[quoted text, click to view]

Hi Todd,

A calendar table is an invaluable tool for tasks like this. Check out
http://www.aspfaq.com/show.asp?id=2519 for lots of information,
including the code to make one and some examples of how to use it. Note
that you only need to create a calendar table once, but you can use it
as often as you want!

For the task at hand, use

INSERT INTO tbl_master
([date], unavailableemployeeid, reasonid, unavailablehours)
SELECT dt, 1234, 12, 7
FROM dbo.Calendar
WHERE dt >= '20050601' -- Note the standard date format yyyymmdd
AND dt < '20050701' -- Note how I test for end of date range
AND dayname <> 'Sunday'

Best, Hugo
--

AddThis Social Bookmark Button