Solved

Copying Records Using Stored Procedure

Posted on 2006-06-11
3
1,248 Views
Last Modified: 2012-06-27
Hi - I want to copy one or more existing records from a certain table, using those records to create new records in the same table, but change one of the field values in each record as it is created.

Here's the scenario - I have a products table and a features table.  Each product record has a number of feature records associated with it.  I want to search the features table for a group of records matching one ProductID, copy them, insert them as new records in the features table, and append the ProductID field to reflect a different product ID.

I can copy records OK with this:

CREATE PROCEDURE spCopyFeatures
     @ProductID Integer
AS
BEGIN
     INSERT INTO dbo.tblFeatures (MfgID, SrsID, ProID, rlFeaCatSeq, rlFeaSeq, nvcFeaName, ntFeaDesc)
     SELECT MfgID, SrsID, ProID, rlFeaCatSeq, rlFeaSeq, nvcFeaName, ntFeaDesc FROM dbo.tblFeatures WHERE ProductID = @ProdID
END

I'm new to SQL and stored procedures beyond the easy stuff.  How can I simultaneously edit the "ProID" field with a product ID I supply (different from the product ID used above to search the features table)?

Thanks,

Rod
0
Comment
Question by:okanagan
3 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 500 total points
ID: 16882990
change your select to
   SELECT MfgID, SrsID, ProID +  @ProductID, rlFeaCatSeq, rlFeaSeq, nvcFeaName, ntFeaDesc FROM dbo.tblFeatures WHERE ProductID = @ProdID
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16882995
okanagan,
> How can I simultaneously edit the "ProID" field with a product ID I supply

You need to pass the new productId's as argument
0
 

Author Comment

by:okanagan
ID: 16883063
Thanks sajuks - right on the money.

Rod
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Server Management Studio, Save query 2 18
Sql Stored Procedure 65 26
Querying data from 3 SQL tables 2 31
Create snapshot on MSSQL 2012 3 17
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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