Here is the code for the application, which you can add to the IDE tool menu, which can help. He was posted some time ago to one of the CodeGear newsgroups as a member of TeamB Peter Below:
program ClipToStringConst;
// Remove the dot from the line below for a console app,
// per Rob Kennedy comment. It works fine without being
// a console app.
{. $ APPTYPE CONSOLE}
uses
Windows
Classes
Sysutils,
APIClipboard
const
cIndent = ''; // 2 spaces
cSingleQuote = '' '';
EndChar: array [Boolean] of Char = ('+', ';');
procedure process;
var
SL: TStringlist;
i, max: Integer;
begin
if ClipboardHasFormat (CF_TEXT) then
begin
SL: = TStringlist.Create;
try
SL.Text: = ClipboardAsString;
max: = SL.count-1;
for i: = 0 to max do
SL [i]: = cIndent +
AnsiQuotedStr (TrimRight (SL [i]) + # 32, cSingleQuote) +
EndChar [i = max];
StringToClipboard (SL.Text);
finally
SL.Free;
end; {Finally}
end;
end;
begin
try
Process
except
on E: Exception do
ShowException (E, ExceptAddr);
end;
end.
Just select the text in the SQL management tool after you have tested it and copied it to the clipboard. Switch to the Delphi code editor, place the insertion point where you want the text to appear, select "Clipboard To Const" or whatever you called in the "Tools" menu, and then Ctrl + V to paste it into the editor.
This is a pretty handy tool. You can also change it to work the other way around (ConstantToClipboard) to remove the original formatting and return to raw SQL, although I have not bothered to do this yet.
EDIT: skipped block (APIClipboard). Obviously, this should be a separate unit. Again, thanks to Peter Below:
{== Unit APIClipboard =================================================} {: Clipboard access routines using only API functions @author Dr. Peter Below @desc Version 1.0 created 5 Juli 2000<BR> Current revision 1.0<BR> Last modified 5 Juli 2000<P> This unit provides simply clipboard access routines that do not rely on the VCL Clipbrd unit. That unit drags in Dialogs and Forms and a major part of the VCL as a consequence, not appropriate for simple console or non-form programs. This unit uses only API routines, the only VCL units used are Classes (for exceptions and streams) and SysUtils. } {=====================================================================} unit APIClipboard; interface uses Windows, Classes; procedure StringToClipboard( const S: String ); function ClipboardAsString: String; procedure CopyDataToClipboard( fmt: DWORD; const data; datasize: Integer; emptyClipboardFirst: Boolean = true ); procedure CopyDataFromClipboard( fmt: DWORD; S: TStream ); function ClipboardHasFormat( fmt: DWORD ): Boolean; implementation uses Sysutils; type {: This is an internal exception class used by the <see unit=APIClipboard> } EClipboardError = class( Exception ) public constructor Create( const msg: String ); end; resourcestring eSystemOutOfMemory = 'could not allocate memory for clipboard data.'; eLockfailed = 'could not lock global memory handle.'; eSetDataFailed = 'could not copy data block to clipboard.'; eCannotOpenClipboard = 'could not open the clipboard.'; eErrorTemplate = 'APIClipboard: %s'
Using an example:
Prepare the text in an SQL editor, text editor, or something else:
SELECT
lname
fname
dob
FROM
employees
Select all the text and copy it to the clipboard using Ctrl + C.
Switch to the IDE code editor, start the ClipboardToStringConst application (using the "Tools" menu item you added or any other tools you want). Place the editor cursor (insertion point) where you want the constant text to be displayed, and press Ctrl + V to paste the text.
const
MySQLText = | // The pipe indicates the insertion point.
Result:
const
MySQLText = 'SELECT' +
'lname,' +
'fname,' +
'dob' +
'FROM' +
'employees';