SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query to get data based on two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

archana23
Yak Posting Veteran

77 Posts

Posted - 04/24/2013 :  15:12:17  Show Profile  Reply with Quote
Hello,

I am new to writing queries in Sql, I have two tables like GetReports(ReportName, Url,ReportId,DeptId as columns) and UserDepts(UserName,DeptId as columns) tables.

My requiement is i need to get the data from GetReports table if DeptId is exist in UserDepts for particular UserName

so for that i tried with below query but getting error saying that "Incorrect syntax near keyword Where"

select * from GetReports where ReportID = 5 and DeptId in (select DeptId from UserDepts where UserName ='abc')

Can you please help me on this?

Archana

MuMu88
Aged Yak Warrior

547 Posts

Posted - 04/24/2013 :  15:31:38  Show Profile  Reply with Quote
You may want to try something like this:


SELECT ReportName, Url, ReportId, DeptId from GetReports T1 inner join UserDepts T2 on T1.DeptId = T2.DeptId and T2.UserName = 'abc';

Go to Top of Page

archana23
Yak Posting Veteran

77 Posts

Posted - 04/24/2013 :  16:11:33  Show Profile  Reply with Quote
Thank you.. it worked..

Archana
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 04/24/2013 :  21:21:18  Show Profile  Reply with Quote
You are welcome.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/25/2013 :  00:48:18  Show Profile  Reply with Quote
your original doesnt seem to have any errors. Are you sure this was the full query attempted?



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000