Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
cbjroms
Starting Member
13 Posts |
Posted - 2008-07-24 : 10:25:10
|
| I have created a stored procedure as follows:CREATE PROCEDURE copy_build_options_from_to_product_id @from_product_id char(30), @to_product_id char(30)AS BEGIN -- stage one - update the main cm nodes table UPDATE CM_products SET product_build_options_hdg1 = (SELECT TOP 1 p.product_build_options_hdg1 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg2 = (SELECT TOP 1 p.product_build_options_hdg2 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg3 = (SELECT TOP 1 p.product_build_options_hdg3 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg4 = (SELECT TOP 1 p.product_build_options_hdg4 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg5 = (SELECT TOP 1 p.product_build_options_hdg5 FROM CM_products p WHERE p.product_id = @from_product_id) WHERE product_id = @to_product_id ; -- stage two - delete any old build options which were already there for the target DELETE FROM CM_product_build_options WHERE product_id = @to_product_id ; -- stage three - copy them in from the source record INSERT INTO CM_product_build_options SELECT NEWID() AS 'product_build_option_id', pbo.product_build_option_item_hdg AS 'product_build_option_hdg', pbo.product_build_option_desc AS 'product_build_option_desc', @to_product_id AS 'product_id' FROM CM_product_build_options AS pbo WHERE pbo.product_id = @from_product_id ; END RETURNWhen I run the procedure using:EXEC copy_build_options_from_to_product_id ‘110125’,’110110’I get an error The query could not run because: Incorrect syntax near "'Can anyone out there see where I have got the syntax wrong?Thanks in anticipation.Chris |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-24 : 10:30:00
|
| i'd say it's probably the single quotes around your identifiers in 'stage 3'Em |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-24 : 10:39:15
|
quote: Originally posted by cbjroms I have created a stored procedure as follows:CREATE PROCEDURE copy_build_options_from_to_product_id @from_product_id char(30), @to_product_id char(30)AS BEGIN -- stage one - update the main cm nodes table UPDATE CM_products SET product_build_options_hdg1 = (SELECT TOP 1 p.product_build_options_hdg1 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg2 = (SELECT TOP 1 p.product_build_options_hdg2 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg3 = (SELECT TOP 1 p.product_build_options_hdg3 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg4 = (SELECT TOP 1 p.product_build_options_hdg4 FROM CM_products p WHERE p.product_id = @from_product_id), product_build_options_hdg5 = (SELECT TOP 1 p.product_build_options_hdg5 FROM CM_products p WHERE p.product_id = @from_product_id) WHERE product_id = @to_product_id ; -- stage two - delete any old build options which were already there for the target DELETE FROM CM_product_build_options WHERE product_id = @to_product_id ; -- stage three - copy them in from the source record INSERT INTO CM_product_build_options SELECT NEWID() AS 'product_build_option_id', pbo.product_build_option_item_hdg AS 'product_build_option_hdg', pbo.product_build_option_desc AS 'product_build_option_desc', @to_product_id AS 'product_id' FROM CM_product_build_options AS pbo WHERE pbo.product_id = @from_product_id ; END RETURNWhen I run the procedure using:EXEC copy_build_options_from_to_product_id ‘110125’,’110110’I get an error The query could not run because: Incorrect syntax near "'Can anyone out there see where I have got the syntax wrong?Thanks in anticipation.Chris
why are you having the RETURN in the end? |
 |
|
|
pootle_flump
1064 Posts |
Posted - 2008-07-24 : 11:04:21
|
| Syntax error will be picked up on creating the sproc. The apostrophes you have used in the procedure call are not valid... |
 |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2008-07-24 : 11:08:26
|
| are you sure? I created it just fine without mods?Are your good enough? Skillprover.com |
 |
|
|
cbjroms
Starting Member
13 Posts |
Posted - 2008-07-24 : 11:14:41
|
| Thank you for the replies.Yes, it was the apostrophes in the EXEC that were causing the problem.Can anyone show me how to chage the stored procedure so that I can copy from one product_id to a range of other product-ids (instead of from one to one)?Thanks in anticipation.Chris |
 |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2008-07-24 : 11:18:05
|
| Look up CASCADING UPDATES...Are you good enough? Skillprover.com |
 |
|
|
|
|
|
|
|