July 3, 2006 at 2:38 pm
July 3, 2006 at 4:08 pm
Probably... but I wouldn't... have you ever worked with a TEXT datatype before? It's a pain in the sitting-muscles. I'd likely setup some table with the results of the different sprocs I made for monitoring, etc, and join them like anything else.
If you are using System Sprocs a lot, you can store their results by doing the following...
Create a table with the same columns as the output of the sproc.
INSERT INTO thattable (columnlist...)
EXEC somesproc
--Jeff Moden
July 4, 2006 at 11:38 am
Although I could imagine a procedure that builds dynamic SQL to execute a query, output its results to a temp table, then iterate over the columns of the result to generate a SELECT statement which generates a string consisting of all columns of the result table concatenated together, where each column is CAST to VARCHAR of the appropriate length........ this procedure sounds too complicated to be worth it. It's easier to just read the data from ODBC in (say) Perl. (The quoting mechanism below works in Cygwin; using ActiveState Perl, you would have to save the below as a file and run the file using perl scriptFileName.) Note, remove the space before each string ODBC below (DBD and dbi); it was causing a smiley.
perl <<"EOS"
use DBI;use DBD:: ODBC;
$dbh=DBI->connect('dbi: ODBC:driver={SQL Server};Server=.;database=pubs');
$sth=$dbh->prepare('SELECT TOP 1 * FROM pubs..employee');
$sth->execute();
@columns=@{$sth->{NAME}};
print join("\t",@columns),"\n";
while (my @row = $sth->fetchrow_array) {
print join("\t",@row),"\n";
}
EOS
The output looks like this (assuming localhost has a running SQL database, allows integrated security, and the current user has login permissions to the database server and to pubs):
emp_id fname minit lname job_id job_lvl pub_id hire_date
PMA42628M Paolo M Accorti 13 35 0877 1992-08-27 00:00:00.000
You can easily store a list of queries in a Perl script this way, as I have for a feed processing script. You could concatenate it all to a string (instead of print) and then do an INSERT into a table when done.
The same could of course be done in Visual Basic (or C#, etc), but the code would be longer.
July 4, 2006 at 10:15 pm
I guess the real question to Josh would be... why is this needed in a single string?
--Jeff Moden
July 5, 2006 at 8:53 am
July 5, 2006 at 8:54 am
... and then it became a challenge.
July 6, 2006 at 10:04 am
I thought of converting to and from XML, but wasn't sure that was easy to do. Note that xml element names are case-sensitive (at first I was creating the document with <root> but trying to query it with /ROOT).
Try this:
use pubs
go
declare @docHandle INT
DECLARE @xml NVARCHAR(MAX) ;
select @xml = N'<root>' + cast ( (Select *
from authors
FOR XML AUTO
) AS NVarchar(MAX)) + N'</root>';
exec sp_xml_preparedocument @docHandle OUTPUT, @xml
select * from openxml(@dochandle,N'/root/authors')
WITH authors
exec sp_xml_removedocument @docHandle
The biggest problem I see with it is the WITH clause, which requires either a table name or a schema definition, eg (from books online):
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer',1)
WITH (CustomerID varchar(10),
ContactName varchar(20))
If your queries are ad-hoc SQL, you'd have to somehow determine the schema (select into to a temp table and read syscolumns?) and save that along with the XML document, then retrieve it and build the openxml query as a string and exec it as dynamic SQL.
See SQL Server 2005 Books Online:
Using FOR XML and OPENXML to Publish and Process XML Data
(if local help is installed, the url is
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/ea58e90c-0a2a-4c8f-a7d6-d61618c2d11c.htm
July 6, 2006 at 3:21 pm
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply