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 |
|
chrisbucci8
Starting Member
2 Posts |
Posted - 2010-05-14 : 10:31:35
|
| Here is what I am trying to do:Write a script that will loop through a column in a given table and insert a series of values until the entire column is filled with the appropriate values. Here is the real time example that I will use to explain better.I did this for testing to see if it in fact did what I needed it to do. When I saw the one line altered in the database I then went into the app to verify the change I wanted was made which it did.insert into TABLE NAME (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7)values ('1', '2', '3', '4', '5', '6', '7')Thenselect distinct COLUMN2 from TABLE NAMEwhich gave me a count of 2580.Last the values in COLUMN3 would only range between the same 30 numbers. This is how the loop would know when to end. Once it added that 30th value it would then go to the next value in COLUMN2.Make sense? Any help would be great. I'm sure I need to use a insert into with a select statement or could create a function but any help would be greatly appreciated. Thanks.Chris |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
chrisbucci8
Starting Member
2 Posts |
Posted - 2010-05-14 : 11:20:10
|
| Here is what I entered:select * from DiagSpecialty where DiagID = 20343The results where: DiagSpecialtyID DiagID SpecialtyMID Created CreatedBy LastModified LastModified By1 92 20343 188 2010-05-14 cbucci 2010-05-14 cbucci2. 93 20343 1716 2010-05-14 cbucci 2010-05-14 cbucci3. 94 20343 1717 2010-05-14 cbucci 2010-05-14 cbucci4. 95 20343 1718 2010-05-14 cbucci 2010-05-14 cbucci5. 96 20343 1719 2010-05-14 cbucci 2010-05-14 cbucci6. 97 20343 1720 2010-05-14 cbucci 2010-05-14 cbucci7. 98 20343 1721 2010-05-14 cbucci 2010-05-14 cbucci8. 99 20343 1722 2010-05-14 cbucci 2010-05-14 cbucci9. 100 20343 1723 2010-05-14 cbucci 2010-05-14 cbucci10 101 20343 1724 2010-05-14 cbucci 2010-05-14 cbucci11. 102 20343 1814 2010-05-14 cbucci 2010-05-14 cbucci12. 103 20343 2935 2010-05-14 cbucci 2010-05-14 cbucci13. 104 20343 12281 2010-05-14 cbucci 2010-05-14 cbucci14. 105 20343 12282 2010-05-14 cbucci 2010-05-14 cbucci15. 106 20343 12283 2010-05-14 cbucci 2010-05-14 cbucci16. 107 20343 12284 2010-05-14 cbucci 2010-05-14 cbucci17. 108 20343 12285 2010-05-14 cbucci 2010-05-14 cbucci18. 109 20343 12286 2010-05-14 cbucci 2010-05-14 cbucci19. 110 20343 12287 2010-05-14 cbucci 2010-05-14 cbucci20. 111 20343 12288 2010-05-14 cbucci 2010-05-14 cbucci21. 112 20343 12289 2010-05-14 cbucci 2010-05-14 cbucci22. 113 20343 12290 2010-05-14 cbucci 2010-05-14 cbucci23. 114 20343 12291 2010-05-14 cbucci 2010-05-14 cbucci24. 115 20343 12292 2010-05-14 cbucci 2010-05-14 cbucci25. 116 20343 12293 2010-05-14 cbucci 2010-05-14 cbucci26. 117 20343 12294 2010-05-14 cbucci 2010-05-14 cbucci27. 118 20343 12295 2010-05-14 cbucci 2010-05-14 cbucci28. 119 20343 12296 2010-05-14 cbucci 2010-05-14 cbucci29. 120 20343 12297 2010-05-14 cbucci 2010-05-14 cbucci30. 121 20343 12298 2010-05-14 cbucci 2010-05-14 cbucciThen I ran this for another DiagIDselect * from DiagSpecialty where DiagID = 20344The results were DiagSpecialtyID DiagID SpecialtyMID Created CreatedBy LastModified LastModified By1 122 20344 188 2010-05-14 cbucci 2010-05-14 cbucci2. 123 20344 1716 2010-05-14 cbucci 2010-05-14 cbucci3. 124 20344 1717 2010-05-14 cbucci 2010-05-14 cbucci4. 125 20344 1718 2010-05-14 cbucci 2010-05-14 cbucci5. 126 20344 1719 2010-05-14 cbucci 2010-05-14 cbucci6. 127 20344 1720 2010-05-14 cbucci 2010-05-14 cbucci7. 128 20344 1721 2010-05-14 cbucci 2010-05-14 cbucci8. 129 20344 1722 2010-05-14 cbucci 2010-05-14 cbucci9. 130 20344 1723 2010-05-14 cbucci 2010-05-14 cbucci10. 131 20344 1724 2010-05-14 cbucci 2010-05-14 cbucci11. 132 20344 1814 2010-05-14 cbucci 2010-05-14 cbucci12. 133 20344 2935 2010-05-14 cbucci 2010-05-14 cbucciSo what I need to do is basically insert values for lines 13-30 for this DiagIDNext string I ran was:select distinct DiagID from DiagSpecialtywhich returned DiagID1. 203432. 203443. 203454. 203465. 20347...2578. 229202579. 229212580. 22922So for each DiagID I need to add lines 13-30That a little more clear? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-14 : 11:41:35
|
Is DiagSpecialtyID an autonum / identity column? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-14 : 11:54:48
|
Just an idea, but you have to test:while exists(select top 1 DiagID from DiagSpecialty group by DiagID having count(*)=12)begininsert DiagSpecialty(DiagID, SpecialtyMID, [Created], [CreatedBy], LastModified, [LastModified By])select(select top 1 DiagID from DiagSpecialty group by DiagID having count(*)<30 order by DiagID),SpecialtyMID, [Created], [CreatedBy], LastModified, [LastModified By]from DiagSpecialtywhere DiagID = 20343 and DiagSpecialtyID > 103end No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|