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 unknown amount of records into table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

collie
Constraint Violating Yak Guru

399 Posts

Posted - 11/27/2013 :  07:16:31  Show Profile  Reply with Quote
Hi,

I have a table PlanOrders with information for a specific idno.

I need to get the information from that table and insert a row into Orders with that idno. OrderNo is an identity column.

I need to then retrieve the OrderNo from Orders and insert that OrderNo into table OrderDetails.

PlanOrders .Track 1= OrderDetails.orderNoplan 1

PlanOrders .track 2= OrderDetails.orderNoplan2

PlanOrders .track 3=OrderDetails.orderNoplan 3

For each value in PlanOrders i need to insert 3 records for the OrdeNo.

create table #PlanOrders (
idno int,
track1 int,
track2 int,
track3 int,
)
insert into #PlanOrders (idno,track1,track2,track3) values (1,30,40,50)
insert into #PlanOrders (idno,track1,track2,track3) values (2,0,0,100)
insert into #PlanOrders (idno,track1,track2,track3) values (3,30,10,40)
insert into #PlanOrders (idno,track1,track2,track3) values (4,0,0,0)

CREATE TABLE [dbo].Orders(
[OrderNo] [int] IDENTITY(1,1) NOT NULL,
[IdNo] [int] NOT NULL,
[CreateDate] [smalldatetime] NOT NULL)
CREATE TABLE [dbo].[OrderDetails](
[OrderNo] [int] NOT NULL,
[OrderNoPlan] [smallint] NOT NULL,
[RubricNo] [tinyint] NOT NULL,
[OrderNoPercent] [float] NOT NULL)Since idno 1 has 3 values in table PlanOrders I need to first insert the 3 values into OrderDetails according to their track (if track1 has a value<>0 then under orderplan I insert 1 and orderNoPercent=30) I need to insert the same data 3 times for each RubricNo (11,12,13) as in example below. How can I achieve this? Thanks

orderno OrderNoPlan RubricNo OrderNoPercent
1 1 11 30
1 1 12 30
1 1 13 30
1 2 11 40
1 2 12 40
1 2 13 40
1 3 11 50
1 3 12 50
1 3 13 50
2 3 11 100
2 3 12 100
2 3 13 100

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/27/2013 :  11:33:50  Show Profile  Reply with Quote

SELECT idno AS orderno,
REPLACE(Track,'Track','') AS OrderNoPlan,
RubricNo,
Val AS OrderNoPercent
FROM
(
SELECT idno,Track,Val
FROM #PlanOrders po
UNPIVOT(Val FOR Track IN ([Track1],[Track2],[Track3]))u
)m
CROSS JOIN (VALUES(11),(12),(13)) n (RubricNo)
WHERE Val > 0
ORDER BY orderno,
OrderNoPlan,
RubricNo


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 11/27/2013 11:36:30
Go to Top of Page

collie
Constraint Violating Yak Guru

399 Posts

Posted - 12/01/2013 :  01:52:05  Show Profile  Reply with Quote
Thanks. It works.
Can you please explain why you used cross join?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/01/2013 :  03:58:48  Show Profile  Reply with Quote
quote:
Originally posted by collie

Thanks. It works.
Can you please explain why you used cross join?



you need cross join for repeating each record with RubricNos 11,12 & 13. cross join will take cartesian product and repeats every row in resultset 3 times 1 for each rubricno

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

collie
Constraint Violating Yak Guru

399 Posts

Posted - 12/03/2013 :  03:44:10  Show Profile  Reply with Quote
Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/03/2013 :  06:30:23  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.1 seconds. Powered By: Snitz Forums 2000