![](/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.