![](/img/trans.png)
[英]SQL Search Query ,how to assign weight to each input parameters and how to order the results by weight of input parameters,
[英]How to assign Column Results to Parameters of a Query
我有一个表“ Values”,数据看起来像这样:
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''value'? 另外,由于我在查询中使用的数据类型不同,因此与值表中存储的数据类型不同(值在值表中为“ nvarchar”),我是否会遇到潜在的问题?
您可以尝试执行以下操作:
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.