Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
i have a below qry IF EXISTS (SELECT * FROM trans WHERE cid =36 and pid=37) UPDATE trans set pid='37' WHERE cid =36 and pid=37 Else insert into trans (tid,cid,pid,qty,rate,frmdt,todt) values (207,36, 37,0,55 ,'1/1/1900','1/1/1900') here currently when inserting i need to get the rate value from another table select prate from M_PRDT where PID=37 this rate value needs to be put in place of 55how can it be done in a single stmt without having to use another qry & execute it and get the records
malpashaa
Constraint Violating Yak Guru
264 Posts
Posted - 2012-10-11 : 06:10:04
Try this:
...insert into trans(tid, cid, pid, qty, rate, frmdt, todt) select 207, 36, 37, 0, prate, '1/1/1900', '1/1/1900' from M_PRDT where PID=37
For us, there is only the trying. The rest is not our business. ~T.S. EliotMuhammad Al Pasha
svibuk
Yak Posting Veteran
62 Posts
Posted - 2012-10-11 : 08:34:08
thankls tht works but there is slight prblm in this which i had not noticed1) how do i update the rate in update stmt given above rate= ??2)suppose the rsate of a product X till last month was 100 but the rate was updated in the master product table from current month so when above stmt is executed fr current month then the rate of product X shld not be update fr any previous monthsit shld be updated frm current month onlymy trans tbl has fields tid cid pid qty rate month Year AMOUNT Billno frmdt todt
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-10-11 : 15:53:33
quote:Originally posted by svibuk thankls tht works but there is slight prblm in this which i had not noticed1) how do i update the rate in update stmt given above rate= ??2)suppose the rsate of a product X till last month was 100 but the rate was updated in the master product table from current month so when above stmt is executed fr current month then the rate of product X shld not be update fr any previous monthsit shld be updated frm current month onlymy trans tbl has fields tid cid pid qty rate month Year AMOUNT Billno frmdt todt
1. it would require a join to rate table as belowUPDATE tset t.pid='37' ,t.rate = m.prateFROM trans tINNER JOIN M_PRDT mON m.PID = t.pidWHERE t.cid =36 and t.pid=372. for this do you've a date field in yout M-PRDT table to indicate range for which a rate is valid?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/