Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
DeniseGoodheart
Starting Member
12 Posts |
Posted - 2007-04-05 : 13:10:16
|
Good Day:I'm working a reservation Web application and I need to insert missing dates based on calculations using the DATEADD function using SQL Server 2000. I get the following errors:Server: Msg 156, Level 15, State 1, Line 15Incorrect syntax near the keyword 'Select'.Server: Msg 156, Level 15, State 1, Line 16Incorrect syntax near the keyword 'Select'.My entire script is as follows:DECLARE @MonthNumber varchar(2)DECLARE @YearNumber char(4)DECLARE @CkEndDate DATETIME---------Gets new StatusStartDate to insert into Status tableDECLARE @NewStartDateAvail DATETIME ---------Gets new StatusEndDate to insert into Status tableDECLARE @NewEndDateAvail DATETIME ---------Used to determine whether a StatusStartDate is missing DECLARE @StatusStartDate DATETIME---------Used to determine whether a StatusEndDate is missing DECLARE @StatusEndDate DATETIME ----For testing only--This will eventually be a parameter passed by ASP.NETSET @MonthNumber='6' ----For testing only--This will eventuall be a parameter passed by ASP.NET SET @YearNumber='2007' ----Make Sure the first day of the month is in the Status table SET @CkEndDate=@MonthNumber + '/1/' + @YearNumber WHILE @@ROWCOUNT<>0BEGINSET @StatusStartDate=Select StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumberSET @StatusEndDate=Select StatusEndDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumberIF @StatusStartDate<>@CkEndDate SET @NewEndDateAvail=StatusStartDateSET @NewStartDateAvail=@CkEndDateSET @NewEndDateAvail=NewEndDateAvail.AddDays(-1)SET @CkEndDAte=@StatusEndDateSET @CkEndDate=DATEADD(d,1,@CkEndDate)-1Select * from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumberENDAdditional information:My qryStatusMissingDates Sql Syntax is as follows:SELECT TOP 100 PERCENT DATEPART([month], StatusStartDate) AS MonthNumber, DATEPART([year], StatusEndDate) AS YearNumber, StatusStartDate, StatusEndDate, StatusType, PropertyMgtIDFROM dbo.StatusWHERE (DATEPART([month], StatusStartDate) = 6) AND (DATEPART([year], StatusEndDate) = 2007)ORDER BY StatusStartDateThank You,Denise |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-05 : 13:12:38
|
[code]SET @StatusStartDate=(Select StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)ORSelect @StatusStartDate = StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber[/code] KH |
 |
|
DeniseGoodheart
Starting Member
12 Posts |
Posted - 2007-04-05 : 13:55:01
|
Hello,Thank you for your suggestion. Now it gives me the following error message:Invalid column name 'StatusStartDate'I am confused because I have a column called StatusStartDay within my view called qryStatusMissingDates as follows:SELECT TOP 100 PERCENT StatusStartDate, StatusEndDate, DATEPART([month], StatusStartDate) AS MonthNumber, DATEPART([year], StatusEndDate)AS YearNumber, StatusType, PropertyMgtIDFROM dbo.StatusORDER BY StatusStartDateThanks,Densie |
 |
|
X002548
Not Just a Number
15586 Posts |
|
DeniseGoodheart
Starting Member
12 Posts |
Posted - 2007-04-05 : 16:16:00
|
Hello,Thank you for the helpful links.Here is some additional information and I hope it helps as follows:Objective:Insert missing date ranges to support an ASP.NET reservation Web application. The table that gets the inserted dates is as follows:CREATE TABLE [dbo].[Status] ( [StatusID] [int] IDENTITY (1, 1) NOT NULL , [StatusStartDate] [smalldatetime] NOT NULL , [StatusEndDate] [smalldatetime] NOT NULL , [StatusType] [nvarchar] (15) NOT NULL , [PropertyMgtID] [smallint] NULL ) ON [PRIMARY]Test Data: 2421 6/1/2007 6/2/2007 Pending 79 2423 6/3/2007 6/7/2007 Available 79 2417 6/8/2007 6/14/2007 Unavailable 79 2422 6/22/2007 6/28/2007 Pending 79 2424 6/29/2007 6/30/2007 Available 79Notice how there is a date range missing between StatusID 2417 and StatusID 2422. The missing date range is StatusStartDate=6/15/2007 and StatusEndDate=6/21/2007. And these are the two date range values that need to be inserted into the Status table. The literal values that need to be inserted into the Status table are StatusType=’Available’ and PropertyMgtID=’79’’I am trying to write a script that inserts missing date ranges by month. The script should read each record from the following view called qryStatusMissingDates based on two parameters as follows:DECLARE @MonthNumber varchar(2)DECLARE @YearNumber char(4)----For testing only--This will eventually be a parameter passed by ASP.NET using a stored procedure.SET @MonthNumber='6' SET @YearNumber='2007' The qryStatusMissingDates view SQL Syntax is as follows:SELECT TOP 100 PERCENT StatusStartDate, StatusEndDate, DATEPART([month], StatusStartDate) AS MonthNumber, DATEPART([year], StatusEndDate) AS YearNumber, StatusType, PropertyMgtIDFROM dbo.StatusORDER BY StatusStartDateThe Sql Script should read the first record and compare the dates with the following SQL syntax:DECLARE @StatusStartDate DATETIMEDECLARE @CkEndDate DATETIMESET @CkEndDate=@MonthNumber + '/1/' + @YearNumber SET @StatusStartDate=(Select StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)IF @StatusStartDate<>@CkEndDate ---then insert the missing valuesSET @NewStartDateAvail=@CkEndDateSET @NewEndDateAvail=@StatusStartDateSET @NewEndDateAvail= DATEADD(d,1@ NewEndDateAvail)-1SET @CkEndDate=@StatusEndDate SET @CkEndDate=DATEADD(d,1,@CkEndDate)-1INSERT INTO STATUS (StatusStartDate, StatusEndDate,StatusType,PropertyMgtID)VALUES (@NewStartDateAvail, @NewEndDateAvail, 'Available','79')---Now the script should get the @StatusEndDate value to create the date ranges as follows:SET @StatusEndDate=(Select StatusEndDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)@CkEndDate=@StatusStartDate@CkEndDate=DateAdd(d, 1, dCkEndDate)END===============================================================New Error:Now I am getting a new error as follows: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The syntax that has the error is as follows:SET @StatusStartDate=(Select StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)My entire SQL Script is as follows:DECLARE @MonthNumber varchar(2)DECLARE @YearNumber char(4)DECLARE @CkEndDate DATETIMEDECLARE @NewStartDateAvail DATETIME DECLARE @NewEndDateAvail DATETIME DECLARE @StatusStartDate DATETIMEDECLARE @StatusEndDate DATETIME ----For testing only--This will eventually be a parameter passed by ASP.NETSET @MonthNumber='6' ----For testing only--This will eventuall be a parameter passed by ASP.NET SET @YearNumber='2007' ----Make Sure the first day of the month is in the Status table as 6/1/2007 (Test Data)SET @CkEndDate=@MonthNumber + '/1/' + @YearNumber WHILE @@ROWCOUNT<>0BEGINSET @StatusStartDate=(Select StatusStartDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)SET @StatusEndDate=(Select StatusEndDate from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumber)IF @StatusStartDate<>@CkEndDate SET @NewStartDateAvail=@CkEndDateSET @NewEndDateAvail=@StatusStartDateSET @NewEndDateAvail=DATEADD(d,1,@NewEndDateAvail)-1SET @CkEndDate=@StatusEndDateSET @CkEndDate=DATEADD(d,1,@CkEndDate)-1INSERT INTO STATUS (StatusStartDate, StatusEndDate,StatusType,PropertyMgtID)VALUES (@NewStartDateAvail, @NewEndDateAvail, 'Available','79')Select * from qryStatusMissingDates where MonthNumber=@MonthNumber and YearNumber=@YearNumberENDThanks,Denise |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
DeniseGoodheart
Starting Member
12 Posts |
Posted - 2007-04-05 : 18:21:05
|
Yes, it was the simplified version and then I realized I needed to post more information. |
 |
|
DeniseGoodheart
Starting Member
12 Posts |
Posted - 2007-04-07 : 15:11:35
|
Hi All,This question has been resolved, but I do not know what is the procedure here for closing a resolved questioned. My syntax that works is as follows:select date_start_missing, min(date_end_missing) as date_end_missingfrom (select d_begin.StatusEndDate + 1 as date_start_missing, d_end.StatusStartDate as date_end_missingfrom dbo.status d_beginjoin (select min(StatusStartDate)-1 as StatusStartDate, StatusEndDate from dbo.status group by StatusEndDate) d_end on (d_begin.StatusEndDate + 1 < d_end.StatusStartDate)where d_begin.StatusEndDate + 1 not in (select StatusStartDate from dbo.status)) agroup by date_start_missing |
 |
|
|
|
|
|
|