Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Comparing the results of 2 Queries in a Sprocedure

Author  Topic 

tranceboy_johny
Starting Member

4 Posts

Posted - 2012-02-29 : 16:26:24
Hi all,

Besides the fact that this is my first post on this forum, I'm also brand new to SQL stored procedure.
I'll try to explain the scenario and if someone could post their advice below, it will be appreciated

I need to create a stored procedure that does the following:

match results of 2 queries against each other, for example, match each row returned by Query 1 with each row returned by Query 2 based on certain criteria(example, first name, last name or age). if a match is found, {do something}

i will provide more information if needed

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-29 : 16:36:30
looks like a simple left join to be between queries

Make a dervied table out of them and use like


SELECT case when q2.[last name] is not null then .... else ... end,..
FROM Query1 q1
LEFT JOIN Query2 q2
ON q2.[first name]=q1.[first name]
AND q2.[last name] = q1.[last name]
AND q2.age = q1.age


make sure you put actual queries here

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

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-29 : 16:38:46
Post the Queries, the DDL of the tables, sample data and expected results

EDIT: Hey my neck of the woods

http://maps.google.com/maps?hl=en&q=Lodi,+NJ&ie=UTF-8&tab=wl

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/

Go to Top of Page

tranceboy_johny
Starting Member

4 Posts

Posted - 2012-02-29 : 16:48:54
X002548

The data retrieved by both queries is result of lots of inner joins/left outer joins

for example in c#, i can use the following to find matches, don't know SQL offers something like this
foreach(Item item in list1)
{
if (list2.Any(x => x.firstName == item.FirstName) == true) do something....
}

P.S.

Haah ! You're from West orange :P
Go to Top of Page

tranceboy_johny
Starting Member

4 Posts

Posted - 2012-02-29 : 16:52:32
quote:
Originally posted by visakh16

looks like a simple left join to be between queries

Make a dervied table out of them and use like


SELECT case when q2.[last name] is not null then .... else ... end,..
FROM Query1 q1
LEFT JOIN Query2 q2
ON q2.[first name]=q1.[first name]
AND q2.[last name] = q1.[last name]
AND q2.age = q1.age


make sure you put actual queries here

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





visakh16 :

Is it possible to store queries in a variable ?
Go to Top of Page

tranceboy_johny
Starting Member

4 Posts

Posted - 2012-02-29 : 18:35:23
quote:
Originally posted by visakh16

looks like a simple left join to be between queries

Make a dervied table out of them and use like


SELECT case when q2.[last name] is not null then .... else ... end,..
FROM Query1 q1
LEFT JOIN Query2 q2
ON q2.[first name]=q1.[first name]
AND q2.[last name] = q1.[last name]
AND q2.age = q1.age


make sure you put actual queries here

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





Thanks visakh16, this helped ;)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-29 : 22:34:43
welcome

b/w you cant store query results in variable as it can hold only single value

You can use table variables or temporary tables for that

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

Go to Top of Page
   

- Advertisement -