我正在使用
declare @starttime datetime = '2015-10-28 10:00',
@endtime datetime = '2015-10-28 12:00',
@interval int = 30
我想从表中检索这些值,而不是将它们声明为静态值。我有一个单独的表,其中包含以下3个值(Startdate
,Enddate
,interval
)。
我尝试创建一个临时表,然后在其中插入值,但如果我想在我的进一步查询中使用vales,则命名约定无效。我的整个查询就是这个,我希望从数据库中提取startdate
,enddate
,interval
而不是静态。
create table #booking (start datetime, [end] datetime)
insert into #booking values
('2015-10-28 08:00','2015-10-28 08:30'),
('2015-10-28 10:00','2015-10-28 10:30'),
('2015-10-28 10:30','2015-10-28 11:00')
declare @starttime datetime = '2015-10-28 08:00',
@endtime datetime = '2015-10-28 12:00',
@interval int = 30,
@slots int
select @slots = datediff(minute, @starttime, @endtime)/@interval
SELECT TOP (@slots) N=IDENTITY(INT, 1, 1)
INTO #Numbers
FROM master.dbo.syscolumns a CROSS JOIN master.dbo.syscolumns b;
select
dateadd(minute,((n-1)*@interval),@starttime) as start,
dateadd(minute,(n*@interval),@starttime) as [end]
into
#slots
from
#numbers
select s.*, b.* from #slots s
left join #booking b
on s.start = b.start and s.[end] = b.[end]
where b.start is null
drop table #numbers, #booking, #slots
答案 0 :(得分:1)
我不太确定我理解这个问题,但假设你可以从该表中获取一行,你可以这样做:
declare @starttime datetime
,@endtime datetime
,@interval int
select @starttime = starttime
,@endtime = endtime
,@interval = interval
from yourTable
where <condition>
答案 1 :(得分:0)
您可以执行以下操作:
SELECT
@starttime = Startdate
, @endtime = Enddate
, @interval = interval
FROM YourTableHere