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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 dynamic sql statement to insert child records

Author  Topic 

jojo2006
Starting Member

5 Posts

Posted - 2006-05-17 : 20:39:07
Hi...how can I write a dynamic sql statement to insert x number of child records? Thx in advance

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-17 : 20:45:43
[code]
insert into MyTable ( Col1,Col2,Col3)
select
Col1 = 'value1',
Col2 = 'value2',
Col3 = 'value3'
union all
select
Col1 = 'value4',
Col2 = 'value5',
Col3 = 'value6'
union all
select
Col1 = 'value7',
Col2 = 'value8',
Col3 = 'value9'
union all
...
etc.

[/code]

CODO ERGO SUM
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-18 : 01:58:18
or Use While loop to insert as many records as you want to insert

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jojo2006
Starting Member

5 Posts

Posted - 2006-05-18 : 13:53:14
Could you show me how to write the while loop if the number of records is unknown?
I'm unsure about how to create the parameter for the child data, too.

Thanks again!
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-05-18 : 13:59:06
>> number of records is unknown?
U want to pass as a parameter the # of records to be inserted ?

Also are u OK with same set of data or u want to have different data all the time ?
If u need different data, u may need to write code (to generate data).




Srinika
Go to Top of Page
   

- Advertisement -