?
Solved

Sql Join Insert Query

Posted on 2011-03-20
7
Medium Priority
?
177 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Sha1395
7 Comments
 
LVL 8

Expert Comment

by:pdd1lan
ID: 35175345
0
 

Author Comment

by:Sha1395
ID: 35175354
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.
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 35175368
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
pdd1lan earned 2000 total points
ID: 35175385
I am not sure what do you mean "Join in Insert and update?

my guest you can check in your phone table to see the record exists or not, if exists then do update, if not then do insert.

IF exists (select 1 from phone_table)
    --do update
     UPDATE phone_table set phone_num = @phone_num where employee_num=@employee_num

ELSE
   --do insert
    INSERT phone_table (phone_num)
    values (@phone_num)
    where employee_num=@employee_num
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35175536


insert into phone (employeenumber, phonenumber)
select employeenmber, phonenumber
from ADUser
0
 

Author Comment

by:Sha1395
ID: 35179437
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

0
 

Author Comment

by:Sha1395
ID: 35179454
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

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question