我有以下SP:連接具有XML PATH
CREATE PROCEDURE AdvancedSearch
@MechanicID nvarchar(50),
@done bit,
@sDate datetime,
@eDate datetime
AS
SELECT VehicleData.*, Vehicle.*, C5.dbo.debkart.navn,
C5.dbo.DEBKART.ADRESSE1, C5.dbo.debkart.adresse2,
Mechanic.MechanicName
FROM (((Vehicle INNER JOIN VehicleData ON Vehicle.ID = VehicleData.IDRegistrationNumber)
INNER JOIN C5.dbo.debkart ON Vehicle.KeyC5 = c5.dbo.debkart.LXbenummer
INNER JOIN Mechanic_VehicleData ON VehicleData.ID = Mechanic_VehicleData.PK_VehicleData))
INNER JOIN Mechanic ON Mechanic_VehicleData.PK_Mechanic = Mechanic.ID
WHERE (@MechanicID IS NULL OR Mechanic.ID = @MechanicID) AND
(@done IS NULL OR VehicleData.Done = @done) AND
((@sDate IS NULL OR @eDate IS NULL) OR VehicleData.DATE BETWEEN @sDate AND @eDate)
GO
我想串連跨越與同一VehicleData.ID所有行MechanicName柱所以想確切地做,因爲這文章在這裏:Concatenate with XML Path 但只有一個是機械師姓名。
我的問題是,我無法弄清楚如何從我的SP中的帖子插入sql代碼。
有人可以幫助我嗎?
更新:我得到的東西的工作,我做了這一點,它好像又回到了我的需要,但有一個問題,我描述了進一步下跌:
SELECT VehicleData.*, Vehicle.*, C5.dbo.debkart.navn, C5.dbo.DEBKART.ADRESSE1, C5.dbo.debkart.adresse2,
STUFF(
(SELECT
',' + MechanicName
FROM Mechanic m
INNER JOIN Mechanic_VehicleData vm ON m.ID = vm.PK_Mechanic
WHERE vm.PK_VehicleData = VehicleData.ID
FOR XML PATH('')), 1, 1, ''
) AS 'Mechanics'
FROM (((Vehicle INNER JOIN VehicleData ON Vehicle.ID = VehicleData.IDRegistrationNumber)
INNER JOIN C5.dbo.debkart ON Vehicle.KeyC5 = c5.dbo.debkart.LXbenummer))
ORDER BY VehicleData.ID
,所以我就到我需要的部分添加WHERE子句,現在我不知道放置在哪裏。我有一種搜索特定的MechanicID,完成標誌和一些日期的方法。我應該在哪裏放這些?
更新2
@thanks馬克幫了我,我仍然有一個 「小」 問題,雖然:)
下面是最終SP:
CREATE PROCEDURE AdvancedSearch
@MechanicID nvarchar(50),
@done bit,
@sDate datetime,
@eDate datetime
AS
SELECT VehicleData.*, Vehicle.*, C5.dbo.debkart.navn, C5.dbo.DEBKART.ADRESSE1, C5.dbo.debkart.adresse2,
STUFF(
(SELECT
',' + MechanicName
FROM Mechanic m
INNER JOIN Mechanic_VehicleData vm ON m.ID = vm.PK_Mechanic
WHERE vm.PK_VehicleData = VehicleData.ID
FOR XML PATH('')), 1, 1, ''
) AS 'Mechanics'
FROM (Vehicle
INNER JOIN VehicleData ON Vehicle.ID = VehicleData.IDRegistrationNumber
INNER JOIN C5.dbo.debkart ON Vehicle.KeyC5 = c5.dbo.debkart.LXbenummer), Mechanic, Mechanic_VehicleData
WHERE VehicleData.ID = Mechanic_VehicleData.PK_VehicleData
AND Mechanic.ID = Mechanic_VehicleData.PK_Mechanic
AND (@MechanicID IS NULL OR Mechanic.ID = @MechanicID)
AND (@done IS NULL OR VehicleData.Done = @done)
AND ((@sDate IS NULL OR @eDate IS NULL) OR VehicleData.Date BETWEEN @sDate AND @eDate)
GO
如上所述,它工作正常。我遇到的問題是,如果我運行EXECUTE AdvancedSearch null, null, null, null
查詢需要4秒鐘才能完成。如果我從SP運行查詢,則不需要任何時間。我覺得很奇怪。爲什麼SP需要更長的時間?
謝謝,我試過,但我仍然無法讓它工作。當我需要將它集成到我的最終SP中時,我的問題就出現了。我甚至得到了工作的例子,但只要我去集成我失敗:( – dbrasco
我更新了上面的帖子,我很感激,如果你可以幫助 – dbrasco
我試過你的建議,如果我添加WHERE Mechanic_VehicleData.ID = 1或Mechanic.ID = 1我剛剛得到'多部分標識符'Mechanic_VehicleData。ID「無法綁定」,我相信這是因爲它沒有包含在select中,但我怎麼能添加它,因爲eache Vehicle的ID會是多個,所以WHERE必須在名稱連接之前完成。有道理我想說什麼,如果是的話,我該如何解決它? – dbrasco