Solved

Sql Join Insert Query

Posted on 2011-03-20
7
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 8

Accepted Solution

by:
pdd1lan earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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