2017-05-16 114 views
-1

我想要做的是從SQLSystem中提取以下信息。 我被困在如何將參數放入我的查詢中。SQL SERVER PARAMETER問題

有沒有人制定過如何做到這一點?

參數[Date From] DateTime,[Date To] DateTime,[Site Group] Text;

SELECT Contacts.Name, Points.M1_Code1, DataProfile.Date, DataProfile.[00:30], DataProfile.[01:00], DataProfile.[01:30], DataProfile.[02:00], DataProfile.[02:30], DataProfile.[03:00], DataProfile.[03:30], DataProfile.[04:00], DataProfile.[04:30], DataProfile.[05:00], DataProfile.[05:30], DataProfile.[06:00], DataProfile.[06:30], DataProfile.[07:00], DataProfile.[07:30], DataProfile.[08:00], DataProfile.[08:30], DataProfile.[09:00], DataProfile.[09:30], DataProfile.[10:00], DataProfile.[10:30], DataProfile.[11:00], DataProfile.[11:30], DataProfile.[12:00], DataProfile.[12:30], DataProfile.[13:00], DataProfile.[13:30], DataProfile.[14:00], DataProfile.[14:30], DataProfile.[15:00], DataProfile.[15:30], DataProfile.[16:00], DataProfile.[16:30], DataProfile.[17:00], DataProfile.[17:30], DataProfile.[18:00], DataProfile.[18:30], DataProfile.[19:00], DataProfile.[19:30], DataProfile.[20:00], DataProfile.[20:30], DataProfile.[21:00], DataProfile.[21:30], DataProfile.[22:00], DataProfile.[22:30], DataProfile.[23:00], DataProfile.[23:30], DataProfile.[24:00] 
    FROM (Lookup INNER JOIN Groups ON Lookup.Lookup_Id = Groups.Lookup_Id) INNER JOIN (Contacts INNER JOIN (Points INNER JOIN DataProfile ON Points.Id = DataProfile.Point_Id) ON Contacts.Id = Points.Contacts_Id) ON Groups.Link_Id = Contacts.Id 
    WHERE (((Lookup.Lookup_Name)=[Site Group]) AND ((DataProfile.Date) Between [Date From] And [Date To]) AND ((Points.Type)='Electricity') AND ((DataProfile.Type)=0)) 
    ORDER BY Contacts.Name, Points.M1_Code1, DataProfile.Date; 

回答

0

你真的應該看看格式化你的查詢。文本牆是噩夢破譯和維護是可怕的。它還具有由給定可怕連接語法的工具生成的獨特外觀。你也應該停止使用文本數據類型。它已被棄用贊成varchar(max)超過十年。而要使用文本,你必須將它轉換爲varchar(max)在你的where謂詞中。而在你的情況下,我會建議你不需要最大值。當然,你沒有超過2GB字符的SiteGroup值?像25或50這樣的東西可能是矯枉過正的。

這是您的代碼格式化參數。我也會建議使用別名,但這完全是另一個商店。

declare @DateFrom datetime 
    , @DateTo datetime 
    , @SiteGroup varhcar(max) 

SELECT Contacts.Name 
    , Points.M1_Code1 
    , DataProfile.Date 
    , DataProfile.[00:30] 
    , DataProfile.[01:00] 
    , DataProfile.[01:30] 
    , DataProfile.[02:00] 
    , DataProfile.[02:30] 
    , DataProfile.[03:00] 
    , DataProfile.[03:30] 
    , DataProfile.[04:00] 
    , DataProfile.[04:30] 
    , DataProfile.[05:00] 
    , DataProfile.[05:30] 
    , DataProfile.[06:00] 
    , DataProfile.[06:30] 
    , DataProfile.[07:00] 
    , DataProfile.[07:30] 
    , DataProfile.[08:00] 
    , DataProfile.[08:30] 
    , DataProfile.[09:00] 
    , DataProfile.[09:30] 
    , DataProfile.[10:00] 
    , DataProfile.[10:30] 
    , DataProfile.[11:00] 
    , DataProfile.[11:30] 
    , DataProfile.[12:00] 
    , DataProfile.[12:30] 
    , DataProfile.[13:00] 
    , DataProfile.[13:30] 
    , DataProfile.[14:00] 
    , DataProfile.[14:30] 
    , DataProfile.[15:00] 
    , DataProfile.[15:30] 
    , DataProfile.[16:00] 
    , DataProfile.[16:30] 
    , DataProfile.[17:00] 
    , DataProfile.[17:30] 
    , DataProfile.[18:00] 
    , DataProfile.[18:30] 
    , DataProfile.[19:00] 
    , DataProfile.[19:30] 
    , DataProfile.[20:00] 
    , DataProfile.[20:30] 
    , DataProfile.[21:00] 
    , DataProfile.[21:30] 
    , DataProfile.[22:00] 
    , DataProfile.[22:30] 
    , DataProfile.[23:00] 
    , DataProfile.[23:30] 
    , DataProfile.[24:00] 
FROM Lookup 
INNER JOIN Groups ON Lookup.Lookup_Id = Groups.Lookup_Id 
INNER JOIN Contacts ON Groups.Link_Id = Contacts.Id 
INNER JOIN Points ON Contacts.Id = Points.Contacts_Id 
INNER JOIN DataProfile ON Points.Id = DataProfile.Point_Id 
WHERE Lookup.Lookup_Name = @SiteGroup 
    AND DataProfile.Date BETWEEN @DateFrom AND @DateTo 
    AND Points.Type = 'Electricity' 
    AND DataProfile.Type = 0 
ORDER BY Contacts.Name 
    , Points.M1_Code1 
    , DataProfile.Date;