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 |
|
simi28
Starting Member
27 Posts |
Posted - 2007-04-03 : 04:20:31
|
| Hi All I have created a stored procedure for transferring data from one table to another tablewith a simple condition.but i am getting error 'Incorrect syntax near the keyword 'select'.may be it is a syntax error but i m not able to resolved it.as i have not worked in stored procedures before.below is my code///alter procedure trans_data3@nop int,@nop1 intasset @nop=select noofparts from test_productsset @nop=select noofparts from test_products1insert test_products1(prod_name,rate,qty)select prod_name,rate,qty from test_products--if (select @nop=noofparts from test_products1) > 0 if @nop > 0 and @nop1 > 0 insert test_products1_parts(noofparts,weight,height) select noofparts,weight,height from test_products_partsgo///in my code I want if column 'noofparts'of table 'test_products'>0then second insert command should execute.Please help. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-03 : 04:27:32
|
[code]alter procedure trans_data3 @nop int, @nop1 intasBEGINset @nop = (select noofparts from test_products)set @nop1 = (select noofparts from test_products1)insert test_products1(prod_name, rate, qty)select prod_name, rate, qty from test_products--if (select @nop=noofparts from test_products1) > 0if @nop > 0 and @nop1 > 0begin insert test_products1_parts(noofparts, weight, height) select noofparts, weight, height from test_products_partsendEND[/code]What are you trying to do for the part in red ? As you have basically assigning one of the records value to the variable. Or do you want number of records in the table ? KH |
 |
|
|
simi28
Starting Member
27 Posts |
Posted - 2007-04-03 : 04:32:47
|
| 'noofparts' is a column in test_products tableI want if the value of noofparts >0 thenthe second insert query should execute. |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-03 : 04:33:35
|
like this?If exists(select * from test_products where noofparts > 0)begin insert test_products1_parts(noofparts,weight,height) select noofparts,weight,height from test_products_partsend Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
simi28
Starting Member
27 Posts |
Posted - 2007-04-03 : 04:47:39
|
| its working now thanks a lot. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-03 : 05:37:41
|
| Also note that this assignment will lead to error if the query retunrs more than one valueset @nop = (select noofparts from test_products)set @nop1 = (select noofparts from test_products1)Safer method isSelect @nop = noofparts from test_productsselect @nop1 = noofparts from test_products1Although it maynot return the value you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|