Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Join Insert Query

Posted on 2011-03-20
7
Medium Priority
?
174 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

618 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