Click here to Skip to main content
15,905,587 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
SQL
I created one stored procedure

CREATE PROCEDURE DynamicWhereClause
(
   @input varchar(100)
)

AS

BEGIN

    DECLARE @sqlCommand varchar(1000)
    SET @sqlCommand = 'SELECT * FROM Employee ' + @input
    EXEC (@sqlCommand)

	
END

EX 1):
SQL
EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky' AND LastName = 'palepu' AND Country = 'India' " 

EX 2):
SQL
EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky'  " 

I am passing where condition to the stored procedure as a parameter
This is executing fine.

Could you please suggest me, How to create "DynamicWhereClause" stored procedure without EXEC in stored procedure? I need to Execute ("DynamicWhereClause" stored procedure without EXEC ) using above two examples.


Please guild me.
Thanks in advance.
Posted
Updated 30-Mar-11 21:07pm
v3
Comments
Member 9770473 23-Aug-13 9:26am    
if column have integer datatype then your procedure not work

@AccountNumber As Varchar(50),
 @FirstName As Varchar(50),
 @LastName As Varchar(50),
@ActiveClinicID As Varchar(50),
 @ActivePhysicianID As Varchar(50),
 @ActivePON As Varchar(50),
 @TaxNumber As Varchar(50),
 @DateOfBirth As Datetime
 
AS  
BEGIN  
  Declare @Where AS Varchar(2000)
  declare @v_Query as varchar(3000)  
   Set @Where='Where 1=1'
	IF @AccountNumber <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.AccountNumber = '''+@AccountNumber+''''
	END
	IF @FirstName <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.FirstName LIKE ''%'+ @FirstName+'%'''
	END
	IF @LastName <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.LastName LIKE ''%'+ @LastName+'%'''
	END
	IF @ActiveClinicID <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.ActiveClinicID ='''+ @ActiveClinicID+''''
	END
	IF @ActivePhysicianID <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.ActivePhysicianID ='''+ @ActivePhysicianID+''''
	END
	IF @ActivePON <> ''
	BEGIN
	SET @Where =@Where + ' AND PatientMaster.ActivePON ='''+ @ActivePON+''''
	END
	IF @DateOfBirth <> ''
	BEGIN
	--SET @Where =@Where + ' AND  PatientMaster.DateOfBirth='''+ CONVERt(CHAR(10),@DateOfBirth,101)+''''
	SET @Where =@Where + ' AND  CAST (CONVERt(CHAR(50),PatientMaster.DateOfBirth,101)AS DATETIME)='''+ CONVERt(CHAR(10),@DateOfBirth,101)+''''
	END
	
    
        set @v_Query='SELECT  PatientMaster.id, PatientMaster.Fullname, PatientMaster.DateOfBirth, PatientMaster.ActivePON,(staffMaster.FirstName +''  ''+staffMaster.LastName) As Physician, clinicMaster.Abbreviation as Clinic
					  FROM    PatientMaster INNER JOIN
                      clinicMaster ON PatientMaster.ActiveClinicID = clinicMaster.clinicid INNER JOIN
                      staffMaster ON PatientMaster.ActivePhysicianID = staffMaster.staffid  ' + @Where
        exec(@v_Query)
 
Share this answer
 
v2
Comments
venkatrao palepu 31-Mar-11 4:49am    
Hi Anil,

whatever your posted, that is correct..

But i am not excepting this one,

is there any solution without "exec(@v_Query)" in stored procedure.....?

Thanks
venkat
SQL
SELECT  PatientMaster.id, PatientMaster.Fullname, PatientMaster.DateOfBirth, PatientMaster.ActivePON,(staffMaster.FirstName +''  ''+staffMaster.LastName) As Physician, clinicMaster.Abbreviation as Clinic
                      FROM    PatientMaster INNER JOIN
                      clinicMaster ON PatientMaster.ActiveClinicID = clinicMaster.clinicid INNER JOIN
                      staffMaster ON PatientMaster.ActivePhysicianID = staffMaster.staffid
+ @Where
 
Share this answer
 
Comments
walterhevedeich 5-Apr-11 2:35am    
Can you prove that this works?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900