Solved

Inner Join for Insert and Update query in Store Proc

Posted on 2011-03-22
11
414 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

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!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

726 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