Søgestreng i storedprocedure fungerer ikke
Hej,Jeg forsøger at lave en søge funktion, som jeg ikke helt kan få til at lykkes. Håber en eller anden herinde, kan hjælpe mig.....
Først min stored procedure:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[GetCompanySearch]
@SearchString AS VARCHAR(200) = null,
@companyCategory AS VARCHAR(200) = null,
@companyType AS VARCHAR(200) = null,
@companyCountry AS VARCHAR(200) = null,
@companyFullName AS VARCHAR(200) = null
AS
BEGIN
SELECT tCompany.fCompanyPId, tCompany.fCompanyAccount, tCompany.fCompanyAlias, tCompany.fCompanyName,
tCompany.fCompanyAddress1, tCompany.fCompanyfAddress2, tCompany.fZipCodeFId, tCompany.fCompanyState,
tCompany.fCompanyPhone, tCompany.fCompanyFax, tCompany.fCompanyEmail, tCompany.fCompanyHomepage,
tCompany.fDimDetailCustomF1Id, tCompany.fCompanyNote, tCompany.fDimDetailCustomF2Id, tCompany.fCompanyCvr,
tCompany.fCompanyPnr, tCompany.fDimDetailCustomF3Id, tCompany.fCompanyOrder, tDimZipCodes.fZipCodeNo,
tDimZipCodes.fZipCodeCity, tDimDetail.fDimDetailText, tDimDetail_1.fDimDetailText AS CompanyCategory,
tCompany.fDimDetailCustomF4Id, tOurContact.fOurContactFirstName + N' ' + tOurContact.fOurContactLastName AS ContactFullName,
tDimDetail_2.fDimDetailText AS CompanyCountry
FROM tOurContact RIGHT OUTER JOIN
tCompany LEFT OUTER JOIN
tDimDetail AS tDimDetail_2 RIGHT OUTER JOIN
tDimZipCodes ON tDimDetail_2.fDimDetailPId = tDimZipCodes.fDimDetailFId ON
tCompany.fZipCodeFId = tDimZipCodes.fZipCodePId LEFT OUTER JOIN
tDimDetail AS tDimDetail_1 ON tCompany.fDimDetailCustomF2Id = tDimDetail_1.fDimDetailPId ON
tOurContact.fOurContactPId = tCompany.fDimDetailCustomF4Id LEFT OUTER JOIN
tDimDetail ON tCompany.fDimDetailCustomF1Id = tDimDetail.fDimDetailPId
WHERE
(
((CAST(fCompanyPId AS VARCHAR(200))) LIKE COALESCE('%' + @SearchString + '%', (CAST(tCompany.fCompanyPId AS VARCHAR(200))))) OR
(fCompanyAlias LIKE COALESCE('%' + @SearchString + '%', tCompany.fCompanyAlias)) OR
(fCompanyName LIKE COALESCE('%' + @SearchString + '%', tCompany.fCompanyName)) OR
(fCompanyAddress1 LIKE COALESCE('%' + @SearchString + '%', tCompany.fCompanyAddress1)) OR
(fCompanyfAddress2 LIKE COALESCE('%' + @SearchString + '%', tCompany.fCompanyfAddress2)) OR
(fZipCodeNo LIKE COALESCE('%' + @SearchString + '%', tDimZipCodes.fZipCodeNo)) OR
(fZipCodeCity LIKE COALESCE('%' + @SearchString + '%', tDimZipCodes.fZipCodeCity)) OR
(fCompanyPhone LIKE COALESCE('%' + @SearchString + '%', tCompany.fCompanyPhone))
)
AND
(tDimDetail_1.fDimDetailText = COALESCE(@companyCategory, tDimDetail_1.fDimDetailText))
END
Company tabellen indeholder 9 rækker. Nu vil jeg frygtelig gerne at hvis indholdet af @SearchString og @companyCategory er tomme, skal den hente alle 9 rækker fra Company tabellen..... MEN det gør den ikke. Den henter kun de 8, hvor der er skrevet noget i tDimDetail_1.fDimDetailText.
Er det i min FROM eller WHERE sætning, der er et problem - og hvor går det galt henne?
mvh
simsen :-)