SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 insert qry with a value from another tbl
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

svibuk
Yak Posting Veteran

62 Posts

Posted - 10/11/2012 :  05:46:52  Show Profile  Reply with Quote
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 55
how 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

Saudi Arabia
257 Posts

Posted - 10/11/2012 :  06:10:04  Show Profile  Reply with Quote
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. Eliot

Muhammad Al Pasha
Go to Top of Page

svibuk
Yak Posting Veteran

62 Posts

Posted - 10/11/2012 :  08:34:08  Show Profile  Reply with Quote
thankls
tht works but there is slight prblm in this which i had not noticed

1) 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 months
it shld be updated frm current month only

my trans tbl has fields
tid cid pid qty rate month Year AMOUNT Billno frmdt todt

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/11/2012 :  15:53:33  Show Profile  Reply with Quote
quote:
Originally posted by svibuk

thankls
tht works but there is slight prblm in this which i had not noticed

1) 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 months
it shld be updated frm current month only

my 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 below
UPDATE t
set t.pid='37' ,t.rate = m.prate
FROM trans t
INNER JOIN M_PRDT m
ON m.PID = t.pid
WHERE t.cid =36 and t.pid=37

2. for this do you've a date field in yout M-PRDT table to indicate range for which a rate is valid?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000