Solved

Pass where clause to stored procedure

Posted on 2013-10-23
4
392 Views
Last Modified: 2013-10-24
I'm trying to creat a stored procedure to wich I ca pass a long string that is a WHERE clause, e.g., "dbo.tbl_PS_Scorecard.PricingReceiptDate>='01/01/2013' And dbo.tbl_PS_Scorecard.PricingReceiptDate<='10/13/2013'" which would the value of the variable @WHERE_Date@.

When I run CREATE PROCEDURE I get an error:
An expression of non-boolean type specified in a context where a condition is expected

How do I pass that lon string to the query code?

Here's the code:

CREATE PROCEDURE [dbo].[sp_PS_Load_EvaluationPerformanceReportTmptest]
      -- Add the parameters for the stored procedure here
      @WHERE_Date@ AS nvarchar(200)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      --SET NOCOUNT ON;
DELETE dbo.tbl_PS_EvaluationPerformanceReportTmp
FROM dbo.tbl_PS_EvaluationPerformanceReportTmp
;      
WITH ReportData (Associate, NumberOfEvaluations, Score)
AS

(SELECT [Last] + ', ' + [First] AS Associate,

ISNull((SELECT Count(AssociateID)
FROM dbo.tbl_PS_Scorecard
GROUP BY dbo.tbl_PS_Scorecard.AssociateID
HAVING dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID),0) AS NumbrOfEvaluations,

ISNull((SELECT
 Sum(dbo.tbl_PS_TransactionLevelComboRowSources.Points)
FROM dbo.tbl_PS_Associates
LEFT JOIN ((dbo.tbl_PS_TransactionLevel
LEFT JOIN dbo.tbl_PS_TransactionLevelComboRowSources
ON dbo.tbl_PS_TransactionLevel.TransactionLevelAnswerID
= dbo.tbl_PS_TransactionLevelComboRowSources.TransactionLevelAnswerID)
LEFT JOIN dbo.tbl_PS_Scorecard
ON dbo.tbl_PS_TransactionLevel.ScoreCardID = dbo.tbl_PS_Scorecard.ScorecardID)
ON dbo.tbl_PS_Associates.AssociateID = dbo.tbl_PS_Scorecard.AssociateID
WHERE dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID AND @WHERE_Date@
GROUP BY dbo.tbl_PS_Associates.AssociateID),0) AS Score

FROM dbo.tbl_PS_Associates a)

INSERT INTO dbo.tbl_PS_EvaluationPerformanceReportTmp
( Associate, NumberOfEvaluations)

SELECT Associate, NumberOfEvaluations FROM ReportData

END



GO
0
Comment
Question by:paulmcneil
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39594341
You would need to use dynamic SQL or change your approach entirely.

DECLARE @SQL NVARCHAR(4000);

SET @SQL = 'SELECT [Last] + '', '' + [First] AS Associate,

ISNull((SELECT Count(AssociateID)
FROM dbo.tbl_PS_Scorecard
GROUP BY dbo.tbl_PS_Scorecard.AssociateID
HAVING dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID),0) AS NumbrOfEvaluations,

ISNull((SELECT
 Sum(dbo.tbl_PS_TransactionLevelComboRowSources.Points)
FROM dbo.tbl_PS_Associates
LEFT JOIN ((dbo.tbl_PS_TransactionLevel
LEFT JOIN dbo.tbl_PS_TransactionLevelComboRowSources
ON dbo.tbl_PS_TransactionLevel.TransactionLevelAnswerID
= dbo.tbl_PS_TransactionLevelComboRowSources.TransactionLevelAnswerID)
LEFT JOIN dbo.tbl_PS_Scorecard
ON dbo.tbl_PS_TransactionLevel.ScoreCardID = dbo.tbl_PS_Scorecard.ScorecardID)
ON dbo.tbl_PS_Associates.AssociateID = dbo.tbl_PS_Scorecard.AssociateID
WHERE dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID AND ' + @WHERE_Date@ +  ' GROUP BY dbo.tbl_PS_Associates.AssociateID),0) AS Score
FROM dbo.tbl_PS_Associates a'

EXECUTE sp_executesql @SQL
0
 

Author Comment

by:paulmcneil
ID: 39594413
Thanks BriCrowe. My stored procedure needs to do 2 things:

1. DELETE all the records from  dbo.tbl_PS_EvaluationPerformanceReportTmp
2. INSERT INTO dbo.tbl_PS_EvaluationPerformanceReportTmp by selecting from the CTE

WITH ReportData (Associate, NumberOfEvaluations, Score)
AS

(SELECT [Last] + ', ' + [First] AS Associate,

ISNull((SELECT Count(AssociateID)
FROM dbo.tbl_PS_Scorecard
GROUP BY dbo.tbl_PS_Scorecard.AssociateID
HAVING dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID),0) AS NumbrOfEvaluations,

ISNull((SELECT
 Sum(dbo.tbl_PS_TransactionLevelComboRowSources.Points)
FROM dbo.tbl_PS_Associates
LEFT JOIN ((dbo.tbl_PS_TransactionLevel
LEFT JOIN dbo.tbl_PS_TransactionLevelComboRowSources
ON dbo.tbl_PS_TransactionLevel.TransactionLevelAnswerID
= dbo.tbl_PS_TransactionLevelComboRowSources.TransactionLevelAnswerID)
LEFT JOIN dbo.tbl_PS_Scorecard
ON dbo.tbl_PS_TransactionLevel.ScoreCardID = dbo.tbl_PS_Scorecard.ScorecardID)
ON dbo.tbl_PS_Associates.AssociateID = dbo.tbl_PS_Scorecard.AssociateID
WHERE dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID AND @WHERE_Date@
GROUP BY dbo.tbl_PS_Associates.AssociateID),0) AS Score

FROM dbo.tbl_PS_Associates a)

Your code does not include the CTE which I use to do the INSERT with the final lines:

INSERT INTO dbo.tbl_PS_EvaluationPerformanceReportTmp
( Associate, NumberOfEvaluations)

SELECT Associate, NumberOfEvaluations FROM ReportData
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39594467
The code was so strangely formatted that I completely missed the cte.  The concept still holds though...

DELETE dbo.tbl_PS_EvaluationPerformanceReportTmp 
FROM dbo.tbl_PS_EvaluationPerformanceReportTmp;

DECLARE @SQL NVARCHAR(4000);

SET @SQL = '
WITH ReportData (Associate, NumberOfEvaluations, Score)
AS
(
	SELECT [Last] + '', '' + [First] AS Associate,
		ISNull((
			SELECT Count(AssociateID)
			FROM dbo.tbl_PS_Scorecard
			GROUP BY dbo.tbl_PS_Scorecard.AssociateID
			HAVING dbo.tbl_PS_Scorecard.AssociateID = a.AssociateID), 0) AS NumbrOfEvaluations,
		ISNull((
			SELECT Sum(dbo.tbl_PS_TransactionLevelComboRowSources.Points)
			FROM dbo.tbl_PS_Associates 
			LEFT JOIN ((dbo.tbl_PS_TransactionLevel 
				LEFT JOIN dbo.tbl_PS_TransactionLevelComboRowSources 
					ON dbo.tbl_PS_TransactionLevel.TransactionLevelAnswerID = dbo.tbl_PS_TransactionLevelComboRowSources.TransactionLevelAnswerID) 
				LEFT JOIN dbo.tbl_PS_Scorecard
					ON dbo.tbl_PS_TransactionLevel.ScoreCardID = dbo.tbl_PS_Scorecard.ScorecardID) 
				ON dbo.tbl_PS_Associates.AssociateID = dbo.tbl_PS_Scorecard.AssociateID
			WHERE dbo.tbl_PS_Scorecard.AssociateID=a.AssociateID AND ' + @WHERE_Date@ +
			'GROUP BY dbo.tbl_PS_Associates.AssociateID), 0) AS Score
	FROM dbo.tbl_PS_Associates AS a
)
INSERT INTO dbo.tbl_PS_EvaluationPerformanceReportTmp
(
	Associate,
	NumberOfEvaluations
)
SELECT Associate,
	NumberOfEvaluations
FROM ReportData'

EXECUTE sp_executeSQL @SQL

Open in new window

0
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 39594600
Why don't you just use 2 date parameters:

CREATE PROCEDURE [dbo].[Sp_ps_load_evaluationperformancereporttmptest] 
  -- Add the parameters for the stored procedure here 
 @startdate varchar(25)=null,
 @enddate varchar(25)=null
AS 
  BEGIN 
      -- SET NOCOUNT ON added to prevent extra result sets from 
      -- interfering with SELECT statements. 
      --SET NOCOUNT ON; 
      DELETE dbo.tbl_ps_evaluationperformancereporttmp 
      FROM   dbo.tbl_ps_evaluationperformancereporttmp; 

      WITH reportdata (associate, numberofevaluations, score) 
           AS (SELECT [last] + ', ' + [first] 
                      AS 
                         Associate, 
                      Isnull((SELECT Count(associateid) 
                              FROM   dbo.tbl_ps_scorecard 
                              GROUP  BY dbo.tbl_ps_scorecard.associateid 
                              HAVING 
                      dbo.tbl_ps_scorecard.associateid = a.associateid), 0) 
                      AS 
                      NumbrOfEvaluations, 
                      Isnull((SELECT Sum( 
                             dbo.tbl_ps_transactionlevelcomborowsources.points) 
                              FROM   dbo.tbl_ps_associates 
                                     LEFT JOIN ((dbo.tbl_ps_transactionlevel 
                      LEFT JOIN dbo.tbl_ps_transactionlevelcomborowsources 
                             ON 
                      dbo.tbl_ps_transactionlevel.transactionlevelanswerid 
                      = 
           dbo.tbl_ps_transactionlevelcomborowsources.transactionlevelanswerid) 
                      LEFT JOIN dbo.tbl_ps_scorecard 
                             ON dbo.tbl_ps_transactionlevel.scorecardid = 
                                dbo.tbl_ps_scorecard.scorecardid) 
                  ON dbo.tbl_ps_associates.associateid = 
                     dbo.tbl_ps_scorecard.associateid 
                   WHERE  dbo.tbl_ps_scorecard.associateid = a.associateid 
                          AND
						  (dbo.tbl_PS_Scorecard.PricingReceiptDate>=@startdate
							and dbo.tbl_PS_Scorecard.PricingReceiptDate<=@enddate
							OR 
							@startdate is null and @enddate is null)

                   GROUP  BY dbo.tbl_ps_associates.associateid), 0) 
           AS 
              Score 
               FROM   dbo.tbl_ps_associates a) 
      INSERT INTO dbo.tbl_ps_evaluationperformancereporttmp 
                  (associate, 
                   numberofevaluations) 
      SELECT associate, 
             numberofevaluations 
      FROM   reportdata 
  END 

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question