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)
 combine two queries

Author  Topic 

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-23 : 06:57:59
Hi...
I am having these querise
insert into password_management
(User_id,current_password,old_pasword,pwd_change_date)
values
(_User_id,_current_password,_old_pasword,current_date());

insert into password_management(security_q1,answer1)(select Security_Question_ID,Security_Answer from app_user where User_id=_User_id);

While executing these queries 2 rows created ..
like this

user_id...current_password...Old_password...password_created_date...security_q1...answer1
1.......xxxxxxx.....yyyyyy....23/10/2010....null...null
null....null....null.....null...2...fine
My need is these two rows must be combined into single one

1.......xxxxxxx.....yyyyyy....23/10/2010....2...fine

Sachin.Nand

2937 Posts

Posted - 2010-10-23 : 07:54:44
You can use a simple join to on user_id to get the matching rows.

PBUH

Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-23 : 08:00:46
Tnx for reply ..I need some more explanation can you ?? Please...
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-23 : 08:08:39
You wrote 2 different insert statements.So it is going to insert 2 rows.if both the rows you are inserting are related then you canjoin them to create a single select statement which you can insert.
We need some more explaination from you.

PBUH

Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-23 : 08:19:20
Sure..I will give you...
I have two tables user_table and password_table
At the new registration of a user datas will be stored in user_table...If the user changes his password then the password will be updated and the old and new password are stored in password_table ...

At this time of inserting the old and new password in password table security question from the user_table must be copied to password table..
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-23 : 08:58:30
what if the password is changed multiple times?Should it insert a new record in the password table?

PBUH

Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2010-10-23 : 09:04:22
Yes..It is act as a log ..New record must inserted ..
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-23 : 11:12:42
You can write a insert trigger on the users table that will insert the data in the password table if any user password is updated in the users table

PBUH

Go to Top of Page
   

- Advertisement -