| Author |
Topic |
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 11:51:45
|
| Hi allI am new to SQL and need some help with a stored procedure. What I need is a stored procedure that does a select on a table where a date = @date, however, if the select does not find anything I need to then do a create using the @date to create a new record. I am just not sure how to do the if select returns nothing then do a create. I hope this made sense.Thank you for any help |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-18 : 11:56:22
|
use logic likeCREATE PROC yourprocname@date dateASIF NOT EXISTS(SELECT 1 FROM table WHERE date=@date) INSERT INTO table.... SELECT * FROM table WHERE date=@dateGO [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-18 : 11:56:53
|
| This will create the row if it is not there then return the row - either existing or newly created.create proc s_MyProc@date datetimeasset nocount onif not exists (select * from tbl where dte = @datebegininsert tbl(dte, ...) select @date, ....endselect * frrom tbl where dte = @date==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 12:00:20
|
| Thank you for such a quick reply, I will try out what you suggested. I really am grateful for your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-18 : 12:08:33
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 14:48:54
|
quote: Originally posted by visakh16 welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I have another question, if I may. What I am trying to do in the insert is: using the @date, I need to get what day of the week it is, then use the day of the week to select from another table called AppointmentTimes. This table has a startTime and an endTime for each day of the week. What I am hoping to inserts is something like this.Lets say the date is 07/18/2012 so the day of the week is Wednesday, the start time is 10:00:00 and the end time is 17:00:00. I need to create a new row starting at 10:00:00 and for every hour until 17:00:00. So in the end I would end up with 7 new rows kind of like this: date = 07/18/2012, time = 10:00:00 date = 07/18/2012, time = 11:00:00 date = 07/18/2012, time = 12:00:00 date = 07/18/2012, time = 13:00:00 date = 07/18/2012, time = 14:00:00 date = 07/18/2012, time = 15:00:00 ect....I hope this made sense |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-18 : 15:00:11
|
| Something like;with cte as(select starttime, endtimefrom AppointmentTimes awhere DayOfWeek = datename(dw,@date)) ;cte2 as(select t = starttime from cteunion allselect dateadd(hh,1,t) from cte1 where t < (select endtime from cte))select @date + tfrom cte2==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 15:18:27
|
| Thank you for your reply, I am sorry to say I don't really follow what you are saying there. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-18 : 16:38:13
|
quote: Originally posted by awalker
quote: Originally posted by visakh16 welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
I have another question, if I may. What I am trying to do in the insert is: using the @date, I need to get what day of the week it is, then use the day of the week to select from another table called AppointmentTimes. This table has a startTime and an endTime for each day of the week. What I am hoping to inserts is something like this.Lets say the date is 07/18/2012 so the day of the week is Wednesday, the start time is 10:00:00 and the end time is 17:00:00. I need to create a new row starting at 10:00:00 and for every hour until 17:00:00. So in the end I would end up with 7 new rows kind of like this: date = 07/18/2012, time = 10:00:00 date = 07/18/2012, time = 11:00:00 date = 07/18/2012, time = 12:00:00 date = 07/18/2012, time = 13:00:00 date = 07/18/2012, time = 14:00:00 date = 07/18/2012, time = 15:00:00 ect....I hope this made sense
something like;With TimeSlots (TimeSlot,EndTime)AS(SELECT StartTime,EndTimeFROM AppointmentTimes aWHERE dayofweekfield= DATENAME(dw,@date)UNION ALLSELECT DATEADD(hh,1,TimeSlot),EndTimeFROM TimeSlotsWHERE DATEADD(hh,1,TimeSlot)<= EndTime)SELECT @date,TimeSlotFROM TimeSlots ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 17:16:57
|
| That seems to work, I got back a list of the different times for that day. Now if I wanted to insert that list into my Appointments table how would I do my insert?Thank you for your help, this all seems so strange to me. |
 |
|
|
awalker
Starting Member
12 Posts |
Posted - 2012-07-18 : 20:25:16
|
| Actually I figured it out. thank you for your help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-19 : 00:02:40
|
| welcomemake sure you learn about common table expressionshttp://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|