Solved

Pass where clause to stored procedure

Posted on 2013-10-23
4
362 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now