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 with in query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kirank
Yak Posting Veteran

USA
58 Posts

Posted - 02/05/2013 :  13:30:06  Show Profile  Visit kirank's Homepage  Reply with Quote
hi,

i know this will be a simple question to you but not able to find proper example.

i want to write a query some thing like this

select *, name=(select name from xyvtable where name =12)
from abctable

here in name=(select name from xyvtable where name =12) i will pass the parameter and base on that name value get populated along with all the records from the abctable and name from xyvtable

Thank You.


---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com

James K
Flowing Fount of Yak Knowledge

3653 Posts

Posted - 02/05/2013 :  14:16:30  Show Profile  Reply with Quote
You can try this code:
SELECT
	a.*,
	b.name 
FROM
	abctable a
	LEFT JOIN xyvtable b ON 
		a.name = b.name AND b.name = '12'
The behavior will change depending on whether the relations ship is one to many or many to one. Also, you can change the LEFT JOIN to INNER JOIN and take a look to see if that is what you want.
Go to Top of Page

kirank
Yak Posting Veteran

USA
58 Posts

Posted - 02/07/2013 :  11:52:55  Show Profile  Visit kirank's Homepage  Reply with Quote
Hi,
thanx for the response.actually i dont want to use any joins simple query

SELECT name,age=(select age from abc where name='xyz') FROM xyz

like this.is that possible.




---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 02/07/2013 :  11:58:32  Show Profile  Reply with Quote
Why would you not want to do any joins? The point of a query is to return data. Since the data resides in 2 different tables, the only way to return that data is with a join.








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

kirank
Yak Posting Veteran

USA
58 Posts

Posted - 02/07/2013 :  13:00:35  Show Profile  Visit kirank's Homepage  Reply with Quote
hi, its resolved chk this


select age ,isnull((select name from xyc where abc=32),'') as name
from nnn

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3653 Posts

Posted - 02/07/2013 :  16:13:32  Show Profile  Reply with Quote
quote:
Originally posted by kirank

hi, its resolved chk this


select age ,isnull((select name from xyc where abc=32),'') as name
from nnn

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com

Just couple of caveats about this:

1. If there is more than one row for abc=32, the query will fail (with a message about subquery returning more than one row).

2. If nnn has more than one row, it is going to return the same value for the name column.
Go to Top of Page

kirank
Yak Posting Veteran

USA
58 Posts

Posted - 02/08/2013 :  14:00:36  Show Profile  Visit kirank's Homepage  Reply with Quote
Hi, thanx for your comments.

i have verify that single records are there in case of (select name from xyvtable where name =12 ) so it will not get break as u suggest.



2. If nnn has more than one row, it is going to return the same value for the name column.
i am not able to get this senario and you give me some example.

---------------------------

http://codingstuffsbykiran.blogspot.com | http://webdevlopementhelp.blogspot.com
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3653 Posts

Posted - 02/08/2013 :  14:33:17  Show Profile  Reply with Quote
Regarding #2, if you copy and paste the code below to a query window and run it, you will see what I meant. There are 3 rows in #nnn, with different values for age, and they all return Smith as the name.
CREATE TABLE #nnn(age INT);
CREATE TABLE #xyc(name VARCHAR(64), abc INT);

INSERT INTO #nnn VALUES (10),(20),(30);
INSERT INTO #xyc VALUES ('Smith',32),('Jones',30);

select age ,isnull((select name from #xyc where abc=32),'') as name
from #nnn

DROP TABLE #xyc,#nnn;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/08/2013 :  23:28:53  Show Profile  Reply with Quote
The scenario James suggested will happen so long as you dont have a relation betwwen two tables nnn and xyc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000