merging 2 or more recordsets in sql.

We can merge 2 or more recordsets in sql. Also we can determine in what order the records should appear.. The rough syntax for merging 3 query resultsets is as follows…

(
SELECT
1
, [Colnames]
FROM
a
WHERE
a.col= @somecolval1
)

UNION ALL

(
SELECT
2
, [Colnames]
FROM
b

WHERE
b.col = @somecolval2

)

UNION ALL

(
SELECT
3
, [Colnames]
FROM
c
WHERE
c.col=@somecolval3
)

ORDER BY 1,2,3

One thing to note here is that the number of col names returned should be same for each single query…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: