• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Stored Procedure to move records

I have a stored procedure that just moves contacts from one table to another. It looks like this.

CREATE PROCEDURE sp_MoveContacts
(
      @ID      int,
                @companyID int
)
AS

INSERT Contacts ( FirstName,LastName, ContactTypeID, companyID )
SELECT FirstName, LastName, ContactTypeID ,companyID
FROM
      v_CartContactInfo
WHERE
      ID=@ID
GO

Instead of it copying the ComanyID from one table to another. I would like to put the companyID that I pass to the stored procedure
Instead ove
0
jui2ce
Asked:
jui2ce
  • 2
1 Solution
 
mokuleCommented:
BEGIN TRAN

INSERT Contacts ( FirstName,LastName, ContactTypeID, companyID )
SELECT FirstName, LastName, ContactTypeID ,companyID
FROM
     v_CartContactInfo
WHERE
     ID=@ID

DELETE
FROM
     v_CartContactInfo
WHERE
     ID=@ID

COMMIT TRAN
0
 
jui2ceAuthor Commented:
I don't see where this would insert into the new Table the CompanyId I pas instead of just coping the one in the current table.
0
 
rafranciscoCommented:
Try this one:

CREATE PROCEDURE sp_MoveContacts
(
     @ID     int,
                @companyID int
)
AS

INSERT Contacts ( FirstName,LastName, ContactTypeID, companyID )
SELECT FirstName, LastName, ContactTypeID , @companyID
FROM
     v_CartContactInfo
WHERE
     ID=@ID
GO

Simply replace the companyID with @companyID and this should work.  If you have a foreign key to your company table, make sure that the @companyID exists in the company table.

Hope this answers your question.
0
 
jui2ceAuthor Commented:
Ah sooo simple should have tried it. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now