Jump to content

SQL 'SET' problems


celfyn

Recommended Posts

Please check my code below. Sorry, but the table and column names are in welsh.CREATE PROCEDURE dbo.usp_CyrsiauAdnewyddu @rhif_cwr intDECLARE @dyddiadAdnewyddu datetimeASSELECT ID_Staff, Rhif_Cyflog, Enw_Cyntaf, Enw_Canol, Cyfenw, b.rhif_cwrs, b.rhif_digwyddiad, c.dyddiad_cychwyn, c.dyddiad_gorffen, dyddiad_adnewydduFROM staff aINNER JOIN tbl_staffCyrsiau b on a.id_staff = b.rhif_staffINNER JOIN tbl_DigwyddiadauCwrs c on b.rhif_cwrs = c.rhif_cwrsWHERE c.rhif_cwrs = @rhif_cwrs--SET @dyddiadAdnewyddu = c.dyddiad_gorffen + dateadd(month, c.dyddiad_adnewyddu, getdate())--AND @dyddiadAdnewyddu BETWEEN getdate() AND dateadd(month, 6, getdate())Is there any way of me being able to set @dyddiadAdnewyddu with the values from table tbl_DigwyddiadCwrs as I am trying to above? I cannot do it at the beginning as the tables haven't been selected etc.What I am trying to do is set the variable @dyddiadAdnewyddu with the date value of whatever is in c.dyddiad_gorffen + the int value in c.dyddiad_adnewyddu - this represents the months.Thanks

Link to comment
Share on other sites

I got it to do what I want it by adapting the code, and not using a variable to hold the value. Here's the code.SELECT ID_Staff, Rhif_Cyflog, Enw_Cyntaf, Enw_Canol, Cyfenw, b.rhif_cwrs, b.rhif_digwyddiad, c.dyddiad_cychwyn, c.dyddiad_gorffen, dyddiad_adnewydduFROM staff aINNER JOIN tbl_staffCyrsiau b on a.id_staff = b.rhif_staffINNER JOIN tbl_DigwyddiadauCwrs c on b.rhif_cwrs = c.rhif_cwrsWHERE c.rhif_cwrs = 98and dateadd(month, c.dyddiad_adnewyddu, c.dyddiad_gorffen) between getdate() and dateadd(month, 6, getdate())Thanks

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...