Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Searching in two tables and add final result to a string Expand / Collapse
Author
Message
Posted Tuesday, December 04, 2012 1:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 9:33 AM
Points: 119, Visits: 360
Hi Team,

I got exact results from Team for whatever i've posted,

Very Very thanks.

Here my requirement is :

I have two tables,

table 1 : Test1
ColA | Value
----------------------
A 100
C 300



Table 2 : Test2
ColA | Value
-----------------------
B 200
D 150


if Record "A" is available in table1 then add string to a variable

@string = A : 300

else search in the table 2

Next

search for B, it is not available in table 1, then get from table 2

@ string = A : 300, B 200


am using below query for searching in table 1, how to include table 2 for searching.

SELECT RTRIM(
+ 'ColA:'+ISNULL(ColA,'')+', '
+ ISNULL('ColB:'+CAST(ColB AS VARCHAR(10))+' ','')
+ ISNULL('ColC:'+CAST(ColC AS VARCHAR(10))+' ','')
+ ISNULL('ColD:'+CAST(ColD AS VARCHAR(10))+' ','')
)
FROM

Please help.........
Post #1392274
Posted Tuesday, December 04, 2012 2:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
Minnu (12/4/2012)
am using below query for searching in table 1, how to include table 2 for searching.
you need to make JOIN with second table with first one.


-------Bhuvnesh----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1392293
Posted Tuesday, December 04, 2012 2:29 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 9:33 AM
Points: 119, Visits: 360
Yes, Using joins..

First it need to check in table1, if value found then assign that values to a string,
if not found then only it should check in table2.


table 1 : Test1
ColA | Value
----------------------
A 100


Table 2 : Test2
ColA | Value
-----------------------
B 200

if COLA =A is available in Table1 then
COLA : A Value: 100

else
check in Table2, if available
COLA : A Value: 100


Query Please...
Post #1392298
Posted Tuesday, December 04, 2012 8:00 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:45 PM
Points: 8,567, Visits: 8,218
Please stop cross posting. You don't need to start a new thread for each follow up question.

http://www.sqlservercentral.com/Forums/Topic1392324-391-1.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1392509
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse