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 4, 2012 1:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 6:35 AM
Points: 227, Visits: 706
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 4, 2012 2:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 6:21 AM
Points: 2,837, Visits: 3,954
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----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1392293
Posted Tuesday, December 4, 2012 2:29 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 6:35 AM
Points: 227, Visits: 706
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 4, 2012 8:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:51 PM
Points: 13,126, Visits: 11,963
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
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1392509
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse