nicemanish
asked on
stored procedure with dynamic
HI All,
could you please help me to create the dynamic stored procedure i already written it but seems like need to improve,plz see the screen in attachment.
I have to create dynamic stored procedure with some criteria
Validation
1.if none of the fields provided it will not search anything
2.(First Name should not be the only search criteriaits) it will not search if only provided firstname,
if user is providing first name then he should also provide the last name.then it will search
3.last name should be at least 3 character,other wise raise issue to user
4.its should be search with other parameter like medicare id ,medicaid id and date of birth
5.here also some issue with date which i used like ,i know its wrong.
Please help me to make a perfect stored procedure
-------------------------- ---------- ---------- ---------- ---------- ---------- ------
CREATE PROCEDURE SGetPatientInfo
@in_vcLastName varchar(30),
@in_vcFirstName varchar(17),
@in_dtDOB DateTime,
@in_nMedicaidID numeric,
@in_nMPID numeric,
@in_nMedicare numeric
AS
BEGIN
DECLARE @sSqlStart Varchar(1000),
@sSqlEnd Varchar(100)
SET NOCOUNT ON
Set @sSqlStart=''
Set @sSqlEnd=''
IF @in_vcLastName = ''
Begin
Set @in_vcLastName = '%'
End
IF in_vcFirstName = ''
Begin
Set in_vcFirstName = '%'
End
IF @in_dtDOB = ''
Begin
Set @in_dtDOB = '%'
End
IF @in_nMedicaidID = ''
Begin
Set @in_nMedicaidID = '%'
End
IF @in_nMPID = ''
Begin
Set @in_nMPID = '%'
End
IF @in_nMedicare = ''
Begin
Set @in_nMedicare = '%'
End
Else
Begin
Set in_vcFirstName = in_vcFirstName + '%'
End
Set @sSqlStart= 'SELECT
Last_Name AS LastName,
First_Name AS FirstName,
Patient_ID AS PatientID,
Birth_Date AS BirthDate,
SSN AS SSN,
Gender AS Gender,
Age AS Age,
FROM
Patient PT
INNER JOIN PatientSecIds PS
ON
PT.Patient_Key = PS.Patient_Key
WHERE
PT.Last_Name like ' + '''' + @in_vcLastName + '''' + '
AND PT.First_Name like ' + '''' + @in_vcFirstName + '''' + '
AND PT.Birth_Date like ' + '''' + @in_dtDOB + '''' + '
AND PS.Medicadeid like ' + '''' + @in_nMedicaidID + '''' + '
AND PS.MPID like ' + '''' + @in_nMPID + '''' + '
AND PS.MediCare like ' + '''' + @in_nMedicare + '''' + '
Begin
Set @sSqlEnd = 'Order By First_Name Asc'
End
EXEC (@sSqlStart + @sSqlEnd)
SET NOCOUNT OFF
END
Doc1.docx
could you please help me to create the dynamic stored procedure i already written it but seems like need to improve,plz see the screen in attachment.
I have to create dynamic stored procedure with some criteria
Validation
1.if none of the fields provided it will not search anything
2.(First Name should not be the only search criteriaits) it will not search if only provided firstname,
if user is providing first name then he should also provide the last name.then it will search
3.last name should be at least 3 character,other wise raise issue to user
4.its should be search with other parameter like medicare id ,medicaid id and date of birth
5.here also some issue with date which i used like ,i know its wrong.
Please help me to make a perfect stored procedure
--------------------------
CREATE PROCEDURE SGetPatientInfo
@in_vcLastName varchar(30),
@in_vcFirstName varchar(17),
@in_dtDOB DateTime,
@in_nMedicaidID numeric,
@in_nMPID numeric,
@in_nMedicare numeric
AS
BEGIN
DECLARE @sSqlStart Varchar(1000),
@sSqlEnd Varchar(100)
SET NOCOUNT ON
Set @sSqlStart=''
Set @sSqlEnd=''
IF @in_vcLastName = ''
Begin
Set @in_vcLastName = '%'
End
IF in_vcFirstName = ''
Begin
Set in_vcFirstName = '%'
End
IF @in_dtDOB = ''
Begin
Set @in_dtDOB = '%'
End
IF @in_nMedicaidID = ''
Begin
Set @in_nMedicaidID = '%'
End
IF @in_nMPID = ''
Begin
Set @in_nMPID = '%'
End
IF @in_nMedicare = ''
Begin
Set @in_nMedicare = '%'
End
Else
Begin
Set in_vcFirstName = in_vcFirstName + '%'
End
Set @sSqlStart= 'SELECT
Last_Name AS LastName,
First_Name AS FirstName,
Patient_ID AS PatientID,
Birth_Date AS BirthDate,
SSN AS SSN,
Gender AS Gender,
Age AS Age,
FROM
Patient PT
INNER JOIN PatientSecIds PS
ON
PT.Patient_Key = PS.Patient_Key
WHERE
PT.Last_Name like ' + '''' + @in_vcLastName + '''' + '
AND PT.First_Name like ' + '''' + @in_vcFirstName + '''' + '
AND PT.Birth_Date like ' + '''' + @in_dtDOB + '''' + '
AND PS.Medicadeid like ' + '''' + @in_nMedicaidID + '''' + '
AND PS.MPID like ' + '''' + @in_nMPID + '''' + '
AND PS.MediCare like ' + '''' + @in_nMedicare + '''' + '
Begin
Set @sSqlEnd = 'Order By First_Name Asc'
End
EXEC (@sSqlStart + @sSqlEnd)
SET NOCOUNT OFF
END
Doc1.docx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You Really help me ... i have done my proc with some other changes.
you are Excellent
you are Excellent
ASKER
could anyone reply?