Solved

Inner Join for Insert and Update query in Store Proc

Posted on 2011-03-22
11
406 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

747 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

13 Experts available now in Live!

Get 1:1 Help Now