Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Cursors, Stored Procedure, Union question 1

Status
Not open for further replies.

fredp1

MIS
Jul 23, 2002
269
AU
DB is SQL2k.
In a previous posting I raised, thread183-758412, I didn't get any suggestions so decided to go down the Cursor route with some sucess.

I've got the stored procedure which outputs dynamic sql.
The output is simply a series of Select and Union statements. e.g a sanitised version of the output is
using a Print command withing the sp is

Select field1 from table1
union all
Select field2 from table1
union all
Select field3 from table1
union all
(repeated about 200 times)
etc

How can you execute the whole batch of sql in a sp?

each line is upto 600 chars
and having 200 select/union statements is typical.

Thanks

P.S.
If anyone want to suggest a alternative to using cursors maybe you can use the original thread.

 
You can excute your individual pieces, inserting the results into a table, as you build them. Something like this:

create table #ErrList (Partnumber c(5), ItemType c(5), ColumnName c(10), CurrentValue integer, CorrectValue integer)

declare @FinalSQL varchar(8000)

set @FinalSQL = ..... (build your string here)
insert into #ErrList exec (@FinalSQL)

set @FinalSQL = ..... (build your string here)
insert into #ErrList exec (@FinalSQL)

set @FinalSQL = ..... (build your string here)
insert into #ErrList exec (@FinalSQL)
...
select * from ErrList
drop table #ErrList
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top