Solved

Inner Join for Insert and Update query in Store Proc

Posted on 2011-03-22
11
409 Views
Last Modified: 2012-05-11
i written in line query in my code but i was trying to convert in to store proc i just stuck some where,please advise me to correct the errors.Thanks in Advance

Even am planning to use IF exist then use update rather than insert

Here is my in line code query and it works good but am trying to write sp

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

Open in new window


ALTER PROC [dbo].[InsertPhone] 
                    @EmployeeNo int,
                    @PhoneNumber char(24)



DECLARE  @CONTEXT_INFO varchar(100)
					select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
 INSERT INTO [Phone]
							(employeeno
							,PhoneNumber
							,CreatedBy
							,UpdatedBy)
select  Ad.employeeno,Ad.PhoneNo,@CONTEXT_INFO,@CONTEXT_INFO from ADUser AD where Ad.employeeno=@EmployeeNo

Open in new window

0
Comment
Question by:Sha1395
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 50 total points
ID: 35187574
when you create a stored proc you must use the AS clause
CREATE PROC [dbo].[InsertPhone] 
                    @EmployeeNo int,
                    @PhoneNumber char(24)

AS

DECLARE  @CONTEXT_INFO varchar(100)
select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
IF NOT EXISTS (select employeeno from Phone where employeeno = @EmployeeNo and PhoneNumber = @PhoneNumber)
BEGIN
INSERT INTO [Phone]
							(employeeno
							,PhoneNumber
							,CreatedBy
							,UpdatedBy)
select  Ad.employeeno,Ad.PhoneNo,@CONTEXT_INFO,@CONTEXT_INFO from ADUser AD where Ad.employeeno=@EmployeeNo
END
--here you can put in an else clause to update the record if required.

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35187576
hi,

  please clarify if you are working with MySQL or MS SQL Server (and which version)
0
 
LVL 4

Assisted Solution

by:qasim_md
qasim_md earned 100 total points
ID: 35187615
try this... let me know if it helped...

ALTER PROC [dbo].[InsertPhone]
                    @EmployeeNo int,
                    @PhoneNumber char(24)
as
begin
declare @CONTEXT_INFO varchar(100)
                              select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
 INSERT INTO [Phone]
                                          (employeeno
                                          ,PhoneNumber
                                          ,CreatedBy
                                          ,UpdatedBy)
select  Ad.employeeno,Ad.PhoneNo,@CONTEXT_INFO,@CONTEXT_INFO from ADUser AD where Ad.employeeno=@EmployeeNo
end
0
 

Author Comment

by:Sha1395
ID: 35187768
Thanks for all the comments.

Am working on MS Sql server 2005
0
 

Author Comment

by:Sha1395
ID: 35187814
so the problem for my code is missed "AS" Clause right ?

Here is my whole sp inclueded Update condition,please correct me if am wrong

ALTER PROC [dbo].[InsertPhone]
                    @EmployeeNo int,
                    @PhoneNumber char(24)
as
begin
declare @CONTEXT_INFO varchar(100)
                              select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
 INSERT INTO [Phone]
                                          (employeeno
                                          ,PhoneNumber
                                          ,CreatedBy
                                          ,UpdatedBy)
select  Ad.employeeno,Ad.PhoneNo,@CONTEXT_INFO,@CONTEXT_INFO from ADUser AD where Ad.employeeno=@EmployeeNo
end
Else
Begin
select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
UPDATE o SET o.employeeno = c. employeeno ,o.PhoneNumber =c.PhoneNumber,o.CreatedBy=@CONTEXT_INFO,O.UpdatedBy=@CONTEXT_INFOT FROM Phone o INNER JOIN ADUser c ON  c.EmployeeNo = o.EmployeeNo WHERE c.EmployeeNo = " + item.EmployeeNumber;
END

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 350 total points
ID: 35187885
I understand you want  to update "if exists", otherwise insert
ALTER PROC [dbo].[InsertPhone]
( @EmployeeNo int
, @PhoneNumber char(24)
)
as
begin
declare @CONTEXT_INFO varchar(100)
select @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)

UPDATE o 
  SET o.employeeno = c.employeeno 
    , o.PhoneNumber =c.PhoneNumber
    , o.CreatedBy=@CONTEXT_INFO
    , O.UpdatedBy=@CONTEXT_INFOT 
 FROM Phone o 
 JOIN ADUser c 
   ON c.EmployeeNo = o.EmployeeNo 
WHERE c.EmployeeNo =  @EmployeeNo

IF @@ROWCOUNT = 0
BEGIN
  INSERT INTO [Phone]
    ( employeeno
    , PhoneNumber
    , CreatedBy
    , UpdatedBy
    )
   SELECT Ad.employeeno
        , Ad.PhoneNo
        , @CONTEXT_INFO
        , @CONTEXT_INFO 
     FROM ADUser AD 
    WHERE Ad.employeeno=@EmployeeNo

END

Open in new window

0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35187913
I think this is what you are looking for:

I have made @CONTEXT_INFO  bigger because you have used 128 characters in your convert in the select statement
0
 

Author Comment

by:Sha1395
ID: 35187955
Thanks for all your help,i think Angel III gave code (awesome).
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35187957
Note that if the SP exists then you can alter it. If it does not exists you have to create it.

ALTER PROC [dbo].[InsertPhone] 
                    @EmployeeNo int,
                    @PhoneNumber char(24)

AS

DECLARE  @CONTEXT_INFO varchar(100)
select   @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
IF NOT EXISTS (select employeeno from Phone where employeeno = @EmployeeNo and PhoneNumber = @PhoneNumber)
BEGIN
INSERT INTO [Phone]
							(employeeno
							,PhoneNumber
							,CreatedBy
							,UpdatedBy)
select  Ad.employeeno,Ad.PhoneNo,@CONTEXT_INFO,@CONTEXT_INFO from ADUser AD where Ad.employeeno=@EmployeeNo
END
ELSE
BEGIN
update [Phone] set PhoneNumber= @PhoneNumber, UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo 
END

Open in new window

0
 

Author Comment

by:Sha1395
ID: 35188065
thanks mkobrin,already i wrote SP for simple insert and update now i will atlter
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

919 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

12 Experts available now in Live!

Get 1:1 Help Now