• You could output to a file and run that. Any reason to do so instead of just executing it as dynamic SQL?

    If you really insist on the output a file step, you could do the whole thing in a PowerShell script, or something like that. Or write that query as a View and use BCP to output it then use PowerShell to run it.

    Easier would be to create a cursor for that Select statement, then step through it and execute each line as a command.

    Or if you can be sure it'll all fit in one command, use the For XML Path('') trick to turn it into a single statement and execute it all at once. I'd use a cursor, though. Easier and more reliable.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon