Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
create table #tmpTable
(
ProductName varchar(200),
Price float
)


insert into #tmpTable values('Cake',10),('Chocolate',20),('bun',30),('bisckit',40)

select * from #tmpTable
drop table #tmpTable



how to increase value of cake bun and biscuit by 10 and choclate by 5 without using cursor
Posted
Updated 23-Jan-12 19:45pm
v2
Comments
MingyiWang 24-Jan-12 2:06am    
What's your meaning?
What's cursor?

There is a one way that u can use Update statement after insert statement in your stored procedure.
 
Share this answer
 
v2
Comments
elinashrestha 24-Jan-12 2:13am    
It should be in loop increment cake bun and biscuit by 10 and choclate by 5 as we use cursor it is done bze cursor value return one value bt cursor shouldnt be use .so in loop i wanna increment
uisng update only u can increase .how to increase using cursor?
 
Share this answer
 
Comments
elinashrestha 24-Jan-12 2:22am    
plz can u send me code how to do that??
declare cursor ,open cursor after fetch put that query insert then close it will incremented one by one ..bt plz send me code how to do
Do you want to loop through the table and where ever you find choclate/bun/cake by 10 and where you find biscuit..increase the price by 5?

You can do it in 2ways.

1.Write update statements which will update all the corresponding rows.

update #tempTable set price =price+ 10 where productname in ('Choclate','Bun','Cake');
update #tempTable set price =price+ 5 where productname='Biscuit' 



2. Loop through the table using a cursor and update the table accoridingly row by row.
See the below example

SQL
DECLARE vendor_cursor CURSOR FOR
SELECT BusinessEntityID, Name
FROM PurchasingVendor;

OPEN vendor_cursor;

FETCH NEXT FROM vendor_cursor 
INTO @vendor_id, @vendor_name;

WHILE @@FETCH_STATUS = 0
BEGIN
..... Do your updates here


Hope this helps.
 
Share this answer
 
v3
Comments
elinashrestha 24-Jan-12 3:58am    
cursor shouldnt be declare. i want it without using cursor function
elinashrestha 24-Jan-12 4:15am    
result should be bun = 20,cake= 40 and biscuit= 60 and choclate should be 25
The Doer 24-Feb-12 7:30am    
how your result would be bum=20 when its previously 30 and you want to increase it by 10????
and cake=40 when its previously 10??
and bisckit=60 when its previously 40??
If u wanna increment thost by 10 and increase Chocolate by 5 then your result should be--
ProductName Price
Cake 20
Chocolate 30
bun 40
bisckit 45

use the query i have suggested..
update #tmpTable set price =price+ 10 where productname in ('Chocolate','Bun','Cake');
update #tmpTable set price =price+ 5 where productname='Bisckit'


the result will be--
ProductName Price
Cake 20
Chocolate 30
bun 40
bisckit 45
 
Share this answer
 
v2

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