As I stated in Part 1 there are optional parameters I want to fire and they have to have a default Value in the SharePoint BDC.
As I know Most of the SharePoint People don't like Sql I made this post so it is easier to make your own Stored Procedure
With optional parameters
In my stored procedure I use a lot of like functions as I will show later on I made a default in my stored procedure that is '%1%'
First of al I start with colling my database And set some other things that are quied obvious.
USE [MyData]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
After that I start my procedure with the parameters that have the exact same names as in de Bdc File
CREATE PROCEDURE [dbo].[Viewpaul]
@ArtsCode varchar(50),
@ArstCollegaCode VARCHAR(50),
@AchternaamPatient VARCHAR(100),
@MeisjesnaamPatient VARCHAR(100),
@GeboorteDatum varchar(100)
Then I declare some more properties that I need to use further in the Procedure
AS
DECLARE @Geboorte datetime
As @Geboorte is a datetime field and I only put in strings(@GeboorteDatum) to make it easier I have to convert it to a datetime
if @GeboorteDatum='%1%'
begin
set @Geboorte=CONVERT(DATETIME, '2/4/1900 12:00:00 AM', 102)
end
else
begin
set @geboortedatum=Left(@GeboorteDatum,len(@GeboorteDatum)-1)
set @geboortedatum=Right(@GeboorteDatum,len(@GeboorteDatum)-1)
set @Geboorte=CONVERT(DATETIME, @geboortedatum, 102)
end
as you can see in above piece I check if it has the default value of '%1%' if it has that value I create a default value for @Geboorte
If not I just convert the date varchar to a real date time value
Then I make the SELECT
SELECT
dbo.KoppelCollega.Active,
dbo.KoppelCollega.ValidTill,
dbo.KoppelCollega.CollegaAgbCode,
dbo.KoppelCollega.HuisartsAgbCode,
dbo.MedRad.ArtsCode,
dbo.MedRad.BerichtDatum,
dbo.MedRad.AchternaamPatient,
dbo.MedRad.MeisjesnaamPatient,
dbo.MedRad.Geboortedatum
FROM
dbo.KoppelCollega RIGHT OUTER JOIN
dbo.MedRad ON dbo.KoppelCollega.HuisartsAgbCode = dbo.MedRad.ArtsCode
After the select I will make the WHERE Clause
WHERE
(dbo.MedRad.ArtsCode LIKE @ArtsCode)
and (dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient or @AchternaamPatient = '%1%')
and (dbo.MedRad.MeisjesnaamPatient LIKE @MeisjesnaamPatient or @MeisjesnaamPatient = '%1%')
and (dbo.MedRad.Geboortedatum = @Geboorte or @Geboorte= '2/4/1900 12:00:00 AM')
or (dbo.KoppelCollega.CollegaAgbCode LIKE @ArstCollegaCode)
and (dbo.KoppelCollega.Active= 1 )
and (dbo.KoppelCollega.ValidTill >= getdate() )
and (dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient or @AchternaamPatient = '%1%')
and (dbo.MedRad.MeisjesnaamPatient LIKE @MeisjesnaamPatient or @MeisjesnaamPatient = '%1%')
and (dbo.MedRad.Geboortedatum = @Geboorte or @Geboorte= '2/4/1900 12:00:00 AM')
Now here is the Trick as you can see I do a LIKE everywhere But the fields that are not optional do not have a or in it
(dbo.MedRad.ArtsCode LIKE @ArtsCode)
(dbo.KoppelCollega.CollegaAgbCode LIKE @ArstCollegaCode)
and (dbo.KoppelCollega.Active= 1 )
and (dbo.KoppelCollega.ValidTill >= getdate() )
the other fields all have a or in it.
So what happens here
(dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient or @AchternaamPatient = '%1%')
Actualy it is a kind of IF Statement
If you look to it it says
dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient should be true
OR
@AchternaamPatient = '%1%'
So if there is a value of '%1%'It also returns true
I hope you can understand this query basicly if one of the statements returns true it can query the where clause.
Under here you will find a exact copy off the whole procedure.
USE [MyData]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Viewpaul]
@ArtsCode varchar(50),
@ArstCollegaCode VARCHAR(50),
@AchternaamPatient VARCHAR(100),
@MeisjesnaamPatient VARCHAR(100),
@GeboorteDatum varchar(100)
AS
DECLARE @Geboorte datetime
if @GeboorteDatum='%1%'
begin
set @Geboorte=CONVERT(DATETIME, '2/4/1900 12:00:00 AM', 102)
end
else
begin
set @geboortedatum=Left(@GeboorteDatum,len(@GeboorteDatum)-1)
set @geboortedatum=Right(@GeboorteDatum,len(@GeboorteDatum)-1)
set @Geboorte=CONVERT(DATETIME, @geboortedatum, 102)
end
SELECT
dbo.KoppelCollega.Active,
dbo.KoppelCollega.ValidTill,
dbo.KoppelCollega.CollegaAgbCode,
dbo.KoppelCollega.HuisartsAgbCode,
dbo.MedRad.AchternaamPatient,
dbo.MedRad.MeisjesnaamPatient,
dbo.MedRad.Geboortedatum
FROM
dbo.KoppelCollega RIGHT OUTER JOIN
dbo.MedRad ON dbo.KoppelCollega.HuisartsAgbCode = dbo.MedRad.ArtsCode
where
(dbo.MedRad.ArtsCode LIKE @ArtsCode)
and (dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient or @AchternaamPatient = '%1%')
and (dbo.MedRad.MeisjesnaamPatient LIKE @MeisjesnaamPatient or @MeisjesnaamPatient = '%1%')
and (dbo.MedRad.Geboortedatum = @Geboorte or @Geboorte= '2/4/1900 12:00:00 AM')
or (dbo.KoppelCollega.CollegaAgbCode LIKE @ArstCollegaCode)
and (dbo.KoppelCollega.Active= 1 )
and (dbo.KoppelCollega.ValidTill >= getdate() )
and (dbo.MedRad.AchternaamPatient LIKE @AchternaamPatient or @AchternaamPatient = '%1%')
and (dbo.MedRad.MeisjesnaamPatient LIKE @MeisjesnaamPatient or @MeisjesnaamPatient = '%1%')
and (dbo.MedRad.Geboortedatum = @Geboorte or @Geboorte= '2/4/1900 12:00:00 AM')
ORDER BY dbo.MedRad.BerichtDatum DESC