Link to home
Start Free TrialLog in
Avatar of Sha1395
Sha1395

asked on

Sql Join Insert Query

Hi,

I have two table Phone and ADUser.

ADUser have all the data,am trying to write store procedure to retireve data from ADUser Table (only phone number) and insert in to Phone table based on employee number.

Am not sure exactly how write write store proc for that.Any help would be great and thanks in Advance

Avatar of pdd1lan
pdd1lan

Avatar of Sha1395

ASKER

thanks for your comment

i already wrote a store procedure for Insert and update but i don't know how to use "Join" in Insert and update.
Avatar of Sara bhai
On which condition basis you need to enter phone data into Phone table.
Or it mean you need all users phone number into Phone table.
ASKER CERTIFIED SOLUTION
Avatar of pdd1lan
pdd1lan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


insert into phone (employeenumber, phonenumber)
select employeenmber, phonenumber
from ADUser
Avatar of Sha1395

ASKER

Sorry for the late reply.

here is my inline command query for insert

 cmd.CommandText = " INSERT INTO Phone (employeeno, PhoneNumber,CreatedBy,UpdatedBy)Select " + "employeeno" + "," + "phoneNo" + "," + "'" + "dbo" + "'" + "," + "'" + "dbo" + "'" + "from ADUser where employeeno=" + item.EmployeeNumber;
        
                cmd.ExecuteNonQuery();

Open in new window

Avatar of Sha1395

ASKER

Am trying to write a store procedure

already i have a store proc for insert an update but i want change my query based on this below

[
" INSERT INTO Phone (employeeno, PhoneNumber,CreatedBy,UpdatedBy)Select " + "employeeno" + "," + "phoneNo" + "," + "'" + "dbo" + "'" + "," + "'" + "dbo" + "'" + "from ADUser where employeeno=" + item.EmployeeNumber;
        

/code]


store proc
[code


					IF EXISTS(SELECT 'True' FROM DEV.[dbo].[Phone] WHERE EmployeeNo = @EmployeeNo)
BEGIN 
					DECLARE  @CONTEXT_INFO varchar(100)
					select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
					update DEV.[dbo].[Phone] set EmployeeNo = @EmployeeNo,
						    PhoneNumber=@PhoneNumber,
							UpdatedBy=@CONTEXT_INFO,
							DateUpdated=getdate()
							where EmployeeNo = @EmployeeNo

END
ELSE
BEGIN
					select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
                    INSERT INTO DEV.[dbo].[Phone]
                               ([EmployeeNo]
							   ,[CreatedBy]
							   ,[UpdatedBy]
                               ,[PhoneNumber])
                         VALUES
                               (@EmployeeNo,
								@CONTEXT_INFO,
								@CONTEXT_INFO,
			                    @PhoneNumber)
END



]

Open in new window