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 2005 Forums
 Transact-SQL (2005)
 use result of first request for a second request

Author  Topic 

nico38
Starting Member

6 Posts

Posted - 2012-04-26 : 06:14:39
Hi,

I would like to use a result of a first request in a second request.
actually I use a php code for do this :
exemple :

_My first request
_loop who take result of first request and insert into many var
_second request who take var in clause where for found if result must request in second request.

Can I take that's all in sql server

if yes, do you have an exemple of request or tuto that do that ?

Thanks in advance and excuse me for my very bad maitrese of English language.

Nico38

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-04-26 : 06:34:06
Read abour CTE and Sub Queries in Sql server.

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-26 : 12:38:15
i feel like its a matter of simple join
But again without having a bit more information on problem I cant suggest much.
Probably with a data sample we should get more clarity
Can you post some DML to depict the scenario?

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

Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-04-27 : 06:12:17
I think you are looking for a Subquery like this:


--Create First Table

Create Table Test
(Id int Identity(1,1),
Status varchar(20) )


--Create Second Table

Create Table Test1
(Id int Identity(1,1),
Name varchar(20) )


--Insert Data in First Table

Declare @temp int = 1
While(@temp <= 10)
Begin
Insert Into Test Values('Checked')
Set @temp = @temp + 1
End


--Insert Data in Second Table

Insert Into Test1
Select 'Vinu'
Union
Select 'Visakh'
Union
Select 'Jack'
Union
Select 'Jim'

--Query

Select Id, Status From Test Where Id IN (Select Id From Test1 Where Name LIKE 'Vi%')


Hope this is what you are looking for.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

nico38
Starting Member

6 Posts

Posted - 2012-05-02 : 02:30:29
Hi,

I am so sorry for my late reply I'am in holydays (first since 11 months !)

This is an exemple that's I would like :

I have 2 table :
Personns => with 2 columns (ID_personns;activite_old)
Personns_Activites => with 2 columns (ID_personns;activite)

And I would like for every personns, sql servers return Personns.ID_personns that's Date is same for Personns.activite_old and Personns_Activites.activite



Thanks you very much.

Nico38.
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-02 : 13:26:40
ave 2 table :
Personns => with 2 columns (ID_personns;activite_old)
Personns_Activites => with 2 columns (ID_personns;activite)


Select P1.ID_personns,P1.activite_old,P2.activite
from Personns P1 inner join Personns_Activites P2
on P1.ID_personns =P2.D_personns

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-02 : 19:50:55
quote:
Originally posted by nico38

Hi,

I am so sorry for my late reply I'am in holydays (first since 11 months !)

This is an exemple that's I would like :

I have 2 table :
Personns => with 2 columns (ID_personns;activite_old)
Personns_Activites => with 2 columns (ID_personns;activite)

And I would like for every personns, sql servers return Personns.ID_personns that's Date is same for Personns.activite_old and Personns_Activites.activite



Thanks you very much.

Nico38.



is that a typo? i cant see date anywhere in ddl

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

Go to Top of Page

nico38
Starting Member

6 Posts

Posted - 2012-05-03 : 02:14:45
Hi,

Fields Personns.activite_old and Personns_Activites.activite are in date format.

Thanks,

Nico38
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-03 : 07:40:58
quote:
Originally posted by nico38

Hi,

I am so sorry for my late reply I'am in holydays (first since 11 months !)

This is an exemple that's I would like :

I have 2 table :
Personns => with 2 columns (ID_personns;activite_old)
Personns_Activites => with 2 columns (ID_personns;activite)

And I would like for every personns, sql servers return Personns.ID_personns that's Date is same for Personns.activite_old and Personns_Activites.activite



Thanks you very much.

Nico38.









Select P1.ID_personns,CASE WHEN P1.activite_old = P2.activite
THEN 'SameDate else 'NotSameDate' end As DateStatus,
P1.activite_old ,
P2.activite
from Personns P1 inner join Personns_Activites P2
on P1.ID_personns =P2.D_personns
Go to Top of Page

nico38
Starting Member

6 Posts

Posted - 2012-05-03 : 08:53:58
Many thanks vijays3,

This is whate I want but I would like that sql server return only row that DateStatus like 'SameDate'

Then I modifiy your request :

Select P1.ID_personns,CASE WHEN P1.activite_old = P2.activite
THEN 'SameDate' else 'NotSameDate' end As DateStatus,
P1.activite_old ,
P2.activite
from Personns P1 inner join Personns_Activites P2
on P1.ID_personns =P2.D_personns
where DateStatus like 'SameDate'

But sql return an error that columns DateStatus does not exist

Do you have a solution

Many thanks.

Nico38.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 15:35:32
then shouldnt it be simply this?

Select P1.ID_personns,
P1.activite_old,
P2.activite
from Personns P1
inner join Personns_Activites P2
on P1.ID_personns =P2.D_personns
and P1.activite_old = P2.activite


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

Go to Top of Page

nico38
Starting Member

6 Posts

Posted - 2012-05-04 : 02:15:18
Thanks visakh16,

It works great
But I would like to use this simply exemple for having an simple exemple of CTE.

It is possible to have the same result with CTE ?

Thank you.

Nico38
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-05 : 01:29:20
quote:
Originally posted by nico38

Thanks visakh16,

It works great
But I would like to use this simply exemple for having an simple exemple of CTE.

It is possible to have the same result with CTE ?

Thank you.

Nico38


why do you need cte for this?
its a simple scenario that does require any intermediate processing and doesnt call for a cte

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-05 : 01:30:26
if you really need to use it (without any need) here is it


;With CTE
AS
(
Select P1.ID_personns,
P1.activite_old,
P2.activite
from Personns P1
inner join Personns_Activites P2
on P1.ID_personns =P2.D_personns
and P1.activite_old = P2.activite
)
select *
from CTE


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

Go to Top of Page

nico38
Starting Member

6 Posts

Posted - 2012-05-05 : 04:15:18
Many thanks, this a good exemple who help me to understand cte.

Nico38.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-05 : 10:40:57
welcome

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

Go to Top of Page
   

- Advertisement -