Solved

Inner Join for Insert and Update query in Store Proc

Posted on 2011-03-22
11
416 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
[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
  • 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 143

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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
 
LVL 143

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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