1

This query is working in mysql but is not working in microsoft sql server management studio 2008, can someone help me out?

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM cms_question_report QR, 
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH 
WHERE C.id=QR.cid 
AND QR.rhid=RH.id 
AND QR.catid='3' 
AND QR.catid=QC.id

I am getting the error: Invalid object name cms_question_report

3
  • Add the name of the schema (likely dbo) and make sure your editor is connecting to the database that has these tables and not the master database Commented May 27, 2015 at 3:52
  • Check selected database is correct or not..!!or write Use Database before this query Commented May 27, 2015 at 3:52
  • also, please use ANSI style joins.... Commented May 27, 2015 at 3:58

2 Answers 2

1
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid 
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id

I think this should do

Sign up to request clarification or add additional context in comments.

4 Comments

This will only join on catid=3. In their example, they filter on catid=3. That's a significant difference.
he/she wanted only catid=3
But this will return all catids and only incur the join on catid = 3
Anyways, I might have a misunderstanding, they accepted your answer.
1

specify Normally it happens when you have specific schema and you don't specify it for example:

Replace dbo. with your schema and/or type your database name

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid 
FROM databasename.dbo.cms_question_report QR, 
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH 
WHERE C.id=QR.cid 
AND QR.rhid=RH.id 
AND QR.catid='3' 
AND QR.catid=QC.id

2 Comments

Thanks but I'm using other queries without including the dbo, but anyway I tried your query and I got the error: Incorrect syntax near '*'.
Try again "replacing databasename with the name of your database" and remove the '*'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.