Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I written in line query in my code but I was trying to convert in to store procedure I just stuck some where,please advise me to correct the errors.Thanks in Advance

Even I am planning to use IF exist then use update rather than select

Here is my in line code query


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




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 User AD where Ad.employeeno=@EmployeeNo
Posted
Updated 22-Mar-11 0:18am
v3
Comments
m@dhu 22-Mar-11 4:32am    
Use pre tags for the code for better readability.

1 solution

Try something like:
SQL
CREATE PROC [dbo].[InsertPhone]
      @EmployeeNo int
AS
BEGIN
      DECLARE @CONTEXT_INFO varchar(100)
      DECLARE @PhoneNo varchar(15)

      SELECT @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
      
      SELECT
             @PhoneNo = Ad.PhoneNo
      FROM
             User Ad
      WHERE Ad.Employeeno= @EmployeeNo


      INSERT INTO [Phone]
             (employeeno, PhoneNumber, CreatedBy, UpdatedBy)
      VALUES
             (@EmployeeNo, @PhoneNo, @CONTEXT_INFO, @CONTEXT_INFO) 
END


Now, if you need some modifications on exists or not (that would need a if and then UPDATE/INSERT staments), then make the changes accordingly.

Try!
 
Share this answer
 
v3
Comments
shan1395 22-Mar-11 5:52am    
hi Sandeep

Thanks for your comment do you seen my query,am trying to pull values from another table (ADUser) and insert in to Phone table.Your store proc to insert for getting values inline and insert.
Sandeep Mewara 22-Mar-11 6:49am    
I did saw that. But your try showed Phoneno as a parameter too that made it look like not needed.

I have modified the SP and it was simple enough for you too to modify it. You should have tried instead of asking me if I read your question. I did said 'try something like...'
shan1395 22-Mar-11 7:00am    
Awesome thank you so much Sandeep
Sandeep Mewara 29-Mar-11 13:37pm    
Glad it worked. Your welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900