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 |
|
digitalslavery
Starting Member
6 Posts |
Posted - 2007-04-18 : 06:35:33
|
| Hi everyone thanks for taking the time to look at my issue.I have a stored proc that retrieves the values from 2 tables and joins them together to output a single dataset, not too uncommon. However this is where it gets interesting, I need to take the results from that stored proc and then use them as values to insert into a new table. I was hoping to use INSERT SELECT, however since I am using UNION to join up the results to get my single dataset I am not sure how to do this, also the table I want to put the results into does not have the same column names, since I need to add some additional data to this new table.here is what I have so far.CREATE PROCEDURE CE_Snapshots_Create(@tidmi_id int)AS-- Take the data from the two deficiencies tables and insert the data into this snapshot table.INSERT INTO CE_Snapshot(shortDesc, deficiency, partForService, qty, manHours, pricing_01, GETDATE(), @tidmi_id)SELECT Pricing_Master.shortDescription, TI_Deficiencies.deficiency, Parts_Master.shortDescription, Parts_Master.partNumber, TI_DEF_END_ITEMS.qty, Parts_Master.manHours, Parts_Master.pricing_01FROM TI_DEF_END_ITEMS INNER JOINPricing_Master ON TI_DEF_END_ITEMS.price_id = Pricing_Master.price_id INNER JOINTI_Deficiencies ON TI_DEF_END_ITEMS.ti_def_id = TI_Deficiencies.ti_def_id LEFT OUTER JOINParts_Master ON TI_DEF_END_ITEMS.partMaster_id = Parts_Master.partMaster_idWHERE (TI_DEF_END_ITEMS.tidmi_id = @tidmi_id)/* SET NOCOUNT ON */ UNIONSELECT cItem, cDef, cPart, cPartNum, cQty, cManHours, cPriceFROM TI_DEF_CUS_END_ITEMSWHERE tidmi_id=@tidmi_id/* SET NOCOUNT ON */ RETURNSELECT * FROM CE_Snapshot where tidmi_id=@tidmi_idRETURNHowever when I try and save the sp I get an error saying Invalid syntax near '('. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 06:46:39
|
quote: Originally posted by digitalslavery Hi everyone thanks for taking the time to look at my issue.I have a stored proc that retrieves the values from 2 tables and joins them together to output a single dataset, not too uncommon. However this is where it gets interesting, I need to take the results from that stored proc and then use them as values to insert into a new table. I was hoping to use INSERT SELECT, however since I am using UNION to join up the results to get my single dataset I am not sure how to do this, also the table I want to put the results into does not have the same column names, since I need to add some additional data to this new table.here is what I have so far.CREATE PROCEDURE CE_Snapshots_Create(@tidmi_id int)AS-- Take the data from the two deficiencies tables and insert the data into this snapshot table.INSERT INTO CE_Snapshot(shortDesc, deficiency, partForService, qty, manHours, pricing_milsys, GETDATE() -- specify actual column names here, @tidmi_id)SELECT Pricing_Master.shortDescription, TI_Deficiencies.deficiency, Parts_Master.shortDescription, Parts_Master.partNumber, TI_DEF_END_ITEMS.qty, Parts_Master.manHours, Parts_Master.pricing_milsysFROM TI_DEF_END_ITEMS INNER JOINPricing_Master ON TI_DEF_END_ITEMS.price_id = Pricing_Master.price_id INNER JOINTI_Deficiencies ON TI_DEF_END_ITEMS.ti_def_id = TI_Deficiencies.ti_def_id LEFT OUTER JOINParts_Master ON TI_DEF_END_ITEMS.partMaster_id = Parts_Master.partMaster_idWHERE (TI_DEF_END_ITEMS.tidmi_id = @tidmi_id)/* SET NOCOUNT ON */ UNIONSELECT cItem, cDef, cPart, cPartNum, cQty, cManHours, cPriceFROM TI_DEF_CUS_END_ITEMSWHERE tidmi_id=@tidmi_id/* SET NOCOUNT ON */ RETURNSELECT * FROM CE_Snapshot where tidmi_id=@tidmi_idRETURNHowever when I try and save the sp I get an error saying Invalid syntax near '('.yAy
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
digitalslavery
Starting Member
6 Posts |
Posted - 2007-04-18 : 07:14:15
|
| Thanks for the quick response to this issue. Ok so I made the changes recomended, however now I get this error:The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.Which I understand, but I need find out how should I approach adding the other 2 additional columns that I need since I specifically need the date stamp to be added when this is created the other column is a fk that is needed for referencing which inspection this dataset is related to.yAy |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 07:20:57
|
| You have to add two column last to your select list.GETDATE() and @tidmi_idPeter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 07:21:16
|
| [code]INSERT INTO CE_Snapshot(shortDesc, deficiency, partForService, <Part-Number-Column>, qty, manHours, pricing_milsys, SomeCol1, SomeCol2)Select shortDescription, deficiency, shortDescription, partNumber, qty, manHours, pricing_milsys, getdate(), @tidmi_idFrom (SELECT Pricing_Master.shortDescription, TI_Deficiencies.deficiency, Parts_Master.shortDescription, Parts_Master.partNumber, TI_DEF_END_ITEMS.qty, Parts_Master.manHours, Parts_Master.pricing_milsys FROM TI_DEF_END_ITEMS INNER JOIN Pricing_Master ON TI_DEF_END_ITEMS.price_id = Pricing_Master.price_id INNER JOIN TI_Deficiencies ON TI_DEF_END_ITEMS.ti_def_id = TI_Deficiencies.ti_def_id LEFT OUTER JOIN Parts_Master ON TI_DEF_END_ITEMS.partMaster_id = Parts_Master.partMaster_id WHERE (TI_DEF_END_ITEMS.tidmi_id = @tidmi_id) /* SET NOCOUNT ON */ UNION SELECT cItem, cDef, cPart, cPartNum, cQty, cManHours, cPrice FROM TI_DEF_CUS_END_ITEMS WHERE tidmi_id=@tidmi_id /* SET NOCOUNT ON */ ) as Temp[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
digitalslavery
Starting Member
6 Posts |
Posted - 2007-04-18 : 08:16:07
|
| wow that really makes sense, it will be a few hours before I can get back and try this out, but I will let you know. Thanks again for everyones help!yAy |
 |
|
|
digitalslavery
Starting Member
6 Posts |
Posted - 2007-04-18 : 15:59:50
|
All right as promised here is an update. I had to make a few tweaks but the code is working it now takes my input param and grabs the data from both of my tables that are joined and inserts the data into a new table. Thanks for all the help hope this can be useful for someone else!USE [table]GO/****** Object: StoredProcedure [c2gear].[CE_Snapshots_Create] Script Date: 04/18/2007 21:54:51 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [table].[CE_Snapshots_Create](@tidmi_id int)ASINSERT INTO CE_History(shortDesc, deficiency, partForService, partNum, qty, manHours, pricing_01, createdDate, tidmi_id)Select shortDescription, deficiency, longDescription, partNumber, qty, manHours, pricing_01, getdate(), @tidmi_idFrom (SELECT Pricing_Master.shortDescription, TI_Deficiencies.deficiency, Parts_Master.longDescription, Parts_Master.partNumber, TI_DEF_END_ITEMS.qty, Parts_Master.manHours, Parts_Master.pricing_milsys FROM TI_DEF_END_ITEMS INNER JOIN Pricing_Master ON TI_DEF_END_ITEMS.price_id = Pricing_Master.price_id INNER JOIN TI_Deficiencies ON TI_DEF_END_ITEMS.ti_def_id = TI_Deficiencies.ti_def_id LEFT OUTER JOIN Parts_Master ON TI_DEF_END_ITEMS.partMaster_id = Parts_Master.partMaster_id WHERE (TI_DEF_END_ITEMS.tidmi_id = @tidmi_id) /* SET NOCOUNT ON */ UNION SELECT cItem, cDef, cPart, cPartNum, cQty, cManHours, cPrice FROM TI_DEF_CUS_END_ITEMS WHERE tidmi_id=@tidmi_id /* SET NOCOUNT ON */ ) as Temp yAy |
 |
|
|
|
|
|
|
|