proc doSql {{sheetonly {}}} {

	global dbpipe1 DB moreRec
	global execCmd contFlag Vdata
	global ResClear showText CmdText Data DataCount VecCt Plotting
	global outxess outRes Plot Data
	global Sheet SheetT CellValues CellFormulae TitleRow curVal curCell curRow

	set contFlag 1
	if {![winfo exists $SheetT]} {
		set Sheet 0
	}
	set sheet $Sheet

	if {$sheetonly == ""} {set sheetonly 0}

	set execCmd "set contFlag 0"
	.m.mb.exec configure -text "Cancel" -state active

	# first make a dash line, 256 chars long
	set d [replicate "----------------" 16]

	set txtindx ""
	set txtcols ""
	set txtlens ""
	set row ""
	set cnt 0
	set sql_str [$CmdText get 1.0 end]
	if {[string trim $sql_str] == ""} {
		return
	}
	set sql_filt ""

	clearoutput $ResClear

	# filter out lines beginning with "#" or lines with "go"
	foreach f [split $sql_str \n] {
		# filter out comments
		set	ex1 [regexp -nocase "^#.*$|^ *#.*$" $f]
		# filter out "go"s
		set	ex2 [regexp -nocase "^go.*$|^ *go.*$" $f]
		if !$ex1$ex2 {
			append sql_filt "$f\n"
		} else {
			append sql_filt "\n"
		}
	}

	if {[string length $sql_filt] == 0} {
		setMsg .m.msg "No SQL to execute"
		set execCmd doSql
		.m.mb.exec configure -text "Execute" -state active
		return
	}

	insSql

	setMsg .m.msg "Running SQL"
	set sqlcmd "$sql_filt"
	set sqt NONE
	set moreRec 0
	set res [queryDB $sqlcmd]

	if {[lindex $res 0] == "ERROR"} {
		setMsg .m.msg "$res"
		set execCmd doSql
		.m.mb.exec configure -text "Execute" -state active
		return
	} else {
		setMsg .m.msg "SQL finished, getting results"
		${DB}chkMsg
	}
	if {!$moreRec} {
	setMsg .m.msg "NO RECORDS $res"
		set execCmd doSql
		return
	}

	if {$Plotting} {
	set firstv $VecCt
	keylset Data($DataCount) sql [string trim $sql_filt] time [clock seconds] sample V$firstv V$firstv RowCt
	upvar #0 V$firstv V$firstv
	vector V$firstv
	set Vdata(V$firstv) $DataCount
	incr VecCt
	}

#	puts stderr "$res"
	set colinfo [getCols]
	set lastcolin $colinfo

# why do we grab this one row and throw it away?
# should we only do this for Sybase?	just stop doing it!
	set fmt ""
#	if {$moreRec} {
#		set row [nextRec]
#		${DB}chkMsg
#	}


	if {$colinfo == ""} {
#		puts stderr "no column info, NO SHEET"
		set sheet 0
	}
		
	set cols [keylkeys colinfo]

	if {$sheet} {
#	do we have enough columns for the data?
	set sc [$SheetT cget -cols]
	set diff [expr {($sc -1) - [llength $cols]}]
	if {$diff < 0} {
#	puts stderr "had to add [abs $diff] cols for data"
	$SheetT insert cols end [abs $diff]
	keySheet $SheetT
	}
	}

#	puts stderr "COLS $cols"

	while {$contFlag && $moreRec} {

#	puts stderr "PROCESS RECORD moreRec is $moreRec"

	set row [nextRec]
#	puts stderr "ROW $row"

	set colinfo [getCols]

	if {[string compare $lastcolin $colinfo] != 0} {
#		puts stderr "COLINFO CHANGED!"
#		puts stderr "New colinfo $colinfo"
		set fmt ""
		set txtindx ""
		set txtcols ""
		set txtlens ""
		set lastcolin $colinfo
		set cols [keylkeys colinfo]
		if {$colinfo == ""} {
#			puts stderr "no column info, NO SHEET"
			set sheet 0
		}
	}
	${DB}chkMsg

	if {[string length $fmt] == 0} {

#	puts stderr "SET FORMAT"
	# extract text columns into separate areas
	set ntcols ""
	set ntypes ""
	set ntlens ""
	set txtcols ""
	set txtlens ""
	set types ""
	set lens ""
	foreach c $cols {
		lassign [keylget colinfo $c] t l
		lappend types $t
		if {$t == "text"} {
			lappend txtcols $c
			lappend txtlens $l
			lappend lens 0
		} else {
			lappend ntcols $c
			lappend ntypes $t
			lappend ntlens $l
			lappend lens $l
		}
	}
	set fmt "[formatCols $cols $types $lens]\n"
	if {$fmt == ""} {
		set fmt "%s"
	}
#
	if {$outxess} {
		outXess "$cols"
		outXess "{} {} {}"
	} 
#
	if {$Plotting} {
		checkPlot $cols $types
	}
#
	if {$outRes} {

		if {$sheet} {
#		puts stderr "    Put Header to Sheet"
		set i 1
		foreach c $cols {
			set CellValues($curRow,$i) $c
			incr i
		}
		$SheetT tag row heads $curRow
		incr curRow
		set sr [$SheetT cget -rows]
		set diff [expr {($sr - 1) - $curRow}]
		if {$diff < 0} {
#			puts stderr "add 10 more rows for new data"
			$SheetT insert rows end 10
			keySheet $SheetT
		}
		} 

	if {!$sheetonly} {
#		puts stderr "    Put Header to Main"
		.m.o.out insert end [eval format \"$fmt\" $cols]
		set dash $cols
		for {set i 0} {$i < [llength $dash]} {incr i} {
		set dash [lreplace $dash $i $i $d]
		}
		.m.o.out insert end [eval format \"$fmt\" $dash]
	}

	}

	}

	catch {eval lassign \$row $cols}
#	puts stderr "PAST LASSIGN"

	if {[string length $row] == 0} {

		set fmt ""
#		puts stderr "Blank Row, reset fmt NULL"

	} else {

#		puts stderr "OUTPUT record"
#
		if {$outxess} {
		outXess "$row"
		} 
#
		if {$Plotting} {
		plotData "$row"
		V$firstv append $cnt
		}
#
		if {$outRes} {

		if {$sheet} {
#		puts stderr "   To Sheet"
		set i 1
		foreach v $row {
			set CellValues($curRow,$i) $v
			incr i
		}
		incr curRow
#	if you are clearing results as you go, then view the top.
#	otherwise, view the latest.
		if {!$ResClear} {
		$SheetT see end
		}
		set sr [$SheetT cget -rows]
		set diff [expr {($sr - 1) - $curRow}]
		if {$diff < 0} {
#			puts stderr "add 10 more rows for new data"
			$SheetT insert rows end 10
			keySheet $SheetT
		}
		} 

		if {!$sheetonly} {
#			puts stderr "   To Main"
			.m.o.out insert end [eval format \"$fmt\" $row]
			if {$showText} {
			if {[llength $txtcols] > 0} {
			foreach tc $txtcols {
#			puts stderr "GOT TEXT COL $tc at end of rec"
			.m.o.out insert end "$tc :"
#			.m.o.out insert end [string range $d 0 30]
			catch {.m.o.out insert end [join [strFold [set $tc] 80] "\n\t"]}
			.m.o.out insert end "\n" 
			}
			}
			}
			if {!$ResClear} {
			.m.o.out see end
			}
		}
		incr cnt
		}
#	should cnt really be incremented only if outres?

		}

#		set row [nextRec]	
		${DB}chkMsg

		if {$cnt % 20 == 0} {
			setMsg .m.msg "$cnt rows so far..."
			update
		}

#	puts stderr "BOTTOM of Loop moreRec is $moreRec"

	}

	if {$Plotting} {
		popDlist $DataCount
		incr DataCount
	}

#	puts stderr "ESCAPED MAIN LOOP, contFlag is $contFlag"

	if {$outxess} {
	xs flush
	}

	if {$contFlag == 1} {
		setMsg .m.msg "SQL finished, $cnt rows returned "
	} else {
		setMsg .m.msg "SQL interrupted, $cnt rows returned "
		sybCancel 1
	}

	set execCmd doSql
	.m.mb.exec configure -text "Execute" -state normal

	update idletasks
#	puts stderr "Restored main screen to normal, return from doSql"

}

