Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL SERVER 2008.

PLEASE ASSIST TO COMPLETE THE SELECT STATEMENT BELOW WITH @MWHERE




IF @CDCOMPT <>'ALL'
BEGIN
SET @MWHERE=@MWHERE+'AND A.COMP_TYPE=@CDCOMPT'
END
ELSE
IF @CDCOMPT ='ALL'
BEGIN
SET @MWHERE=@MWHERE
END
-----------------------------------
------COMPLAINT_TYPE---------------
-----------------------------------

SELECT * FROM RESPONSE A
LEFT JOIN BRANCH C
ON A.BRN_NAME = C.BRN_NAME
LEFT JOIN PAYMAST B
ON A.LNAME+''+A.FNAME+''+A.EMPNO= B.LNAME+''+B.FNAME+''+B.EMPNO
&@MWHERE

What I have tried:

These are codes which I need help to correct
Posted
Updated 18-Oct-17 6:49am
v2
Comments
RedDk 18-Oct-17 14:16pm    
I see here, and in other spots, difficulties with TSQL concepts that working through "Book on Line" samples will quickly render less difficult. Along the lines of F1 help for individual keywords, is the idea that picking through the statements starting something like 'SELECT' will direct you to something that works.

Speaking of works, I also suggest installing AdventureWorks database which is a cornucopia of good programming tactics and nuances which no hardcopy on earth attainable through Amazon or Ebay is going to overrule.

In conclusion:

Install the BOL
Install the Aw database

1 solution

1) DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

2) Start by trying to find out what the problem is: remember that we have no idea what the SELECT statement is supposed to do, or any access to your DB to find out what it might be doing.
But there are some oddities there:
SQL
SET @MWHERE=@MWHERE
Does nothing.
So this code is completely redundant:
SQL
IF @CDCOMPT ='ALL' 
BEGIN 
SET @MWHERE=@MWHERE 
END

And this code could probably use a space:
SQL
SET @MWHERE=@MWHERE+'AND A.COMP_TYPE=@CDCOMPT'
Try:
SQL
SET @MWHERE=@MWHERE+' AND A.COMP_TYPE=@CDCOMPT'
And "&" is an SQL Bitwise operator and will give you an error when applied to strings:
SQL
ON A.LNAME+''+A.FNAME+''+A.EMPNO= B.LNAME+''+B.FNAME+''+B.EMPNO
&@MWHERE
Probably you are trying to build a command, which implies you need "+" instead, but then you would have to build the whole command into a string and use EXEC to run it.
 
Share this answer
 
Comments
Richard Deeming 18-Oct-17 14:56pm    
Preferably sp_executesql[^], with a properly parameterized query. :)

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