Solved

Sql Join Insert Query

Posted on 2011-03-20
7
157 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
Comment Utility
0
 

Author Comment

by:Sha1395
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Accepted Solution

by:
pdd1lan earned 500 total points
Comment Utility
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:ewangoya
Comment Utility


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

Author Comment

by:Sha1395
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now