我有一张桌子"价值观"数据如下所示:
ID Label Value
1 StartDate 1/1/17
2 EndDate 1/15/17
3 Dept 6
我想要做的是加载"标签"列查询我的查询中的相应参数:
Declare
@startdate Datetime,
@enddate Datetime,
@DepartmentID int
Select *
From Customers
Where created between @startdate and @enddate and @DepartmentID
如何将@Startdate分配给' Startdate' '值'在价值表中?另外,由于我在查询中使用不同的数据类型,而不是存储在Values表中的值(值是' nvarchar'在值表中)我是否会遇到潜在的问题?
答案 0 :(得分:1)
您可以尝试这样做:
SELECT
*
FROM
CUSTOMERS
WHERE
CREATED BETWEEN
(SELECT TOP 1 [Value] FROM Values WHERE Label = 'StartDate') --perform casts here if necessary
AND
(SELECT TOP 1 [Value] FROM Values WHERE Label = 'EndDate') --perform casts here if necessary
答案 1 :(得分:1)
Declare
@startdate Datetime,
@enddate Datetime,
@DepartmentID int
set @startdate = (Select convert(datetime,[Value]) from dbo.Values where Label='StartDate')
set @enddate = (Select convert(datetime,[Value]) from dbo.Values where Label='EndDate')
set @DepartmentID =(Select convert(int,[Value]) from dbo.Values where Label='Dept')
Select *
From Customers
Where created between @startdate and @enddate and DepartmentId = @DepartmentID