Author |
Topic |
NewCents
Starting Member
19 Posts |
Posted - 2005-08-04 : 04:15:55
|
casedetail table:casedetail_ID, cases_ID, qty, price, product_code1, 199, 2, 1000, 137012, 199, 2, 1000, 137023, 199, 2, 1000, 13705product_items table:product_items_ID, product_code, products_ID:15, 13701, 316, 13701, 317, 13701, 318, 13702, 519, 13704, 320, 13705, 6I would like to create a temporary table that is the same as the casedetail table but in place of the product_code is the products_ID. So the resulting table would be:1, 199, 2, 1000, 32, 199, 2, 1000, 53, 199, 2, 1000, 6I am very new to SQL, any help appreciated!Thank you |
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-08-04 : 04:20:38
|
just the way you created the permanent table but this time use temp tables or table variablescreate table #temp(fields...)ordeclare @temp table(fields...)--------------------keeping it simple... |
|
|
NewCents
Starting Member
19 Posts |
Posted - 2005-08-04 : 04:27:37
|
Yes, I know how to create temporary tables, I'm just trying to replace the product_code in casedetail to products_ID in product_items. I was hoping someone could post a query.Thank you for any help |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-04 : 04:58:27
|
I think you can create view alsoCreate view myView asSelect cd.casedetail_ID, cd.cases_ID, cd.qty, cd.price, pi.products_ID fromcasedetail cd inner join product_items pi on cd.product_code=pi.product_code or use temp tableCreate table #mytable(your columns)Insert into #mytableSelect cd.casedetail_ID, cd.cases_ID, cd.qty, cd.price, pi.products_ID fromcasedetail cd inner join product_items pi on cd.product_code=pi.product_code MadhivananFailing to plan is Planning to fail |
|
|
NewCents
Starting Member
19 Posts |
Posted - 2005-08-04 : 05:21:15
|
madhivanan,here is the problem I'm having. If I try this:Select cd.casedetail_ID, cd.cases_ID, cd.qty, cd.price, pi.products_ID into #mytablefrom casedetail cd inner join product_items pi on cd.product_code=pi.product_code#mytable has 88 rows of data, even though casedetail only has 3 rows of data. There might be 50 rows in product_items table that have 13701 as a product_code. So when creating this temporary table, all of those rows get thrown in. What I'm looking for is simply to replace the product_code in the case_detail table to the corresponding products_ID in the product_items table. Thank you for any help! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-04 : 05:32:36
|
Try thisSelect cd.casedetail_ID, cd.cases_ID, cd.qty, cd.price, pi.products_ID into #mytablefrom (Select casedetail_ID, cases_ID, qty, price, (select top 1 product_id from product_items pi where pi.product_code=cd.product_code) as product_id from casedetail cd) T MadhivananFailing to plan is Planning to fail |
|
|
NewCents
Starting Member
19 Posts |
Posted - 2005-08-04 : 05:51:34
|
Thank you for the quick response!For some reason I get:The column prefix 'cd' does not match with a table name or alias name used in the query.The column prefix 'pi' does not match with a table name or alias name used in the query. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-04 : 06:01:18
|
This should workSelect casedetail_ID, cases_ID, qty, price, products_ID into #mytablefrom (Select casedetail_ID, cases_ID, qty, price, (select top 1 product_id from product_items pi where pi.product_code=cd.product_code) as product_id from casedetail cd) T MadhivananFailing to plan is Planning to fail |
|
|
NewCents
Starting Member
19 Posts |
Posted - 2005-08-04 : 16:33:09
|
madhivanan,I can't thank you enough!, this works perfectly! |
|
|
|
|
|