after almost a year "SSC abstinence" I'm back with a question:
Here's the scenario (2008R2):
The task is to call a stored procedure with some parameter, format the result set as a simple html file and send it to a printer. SSRS or SSIS are not an option, since this would be the only task where we'd use so it would be "oversized", at least at the moment. Sounds easy enough.
I got to the point where I have the html file created (using bcp in a batch file and a cmdexec job step).
To print the html file I tried to use a program called printhtml (http://www.printhtml.com/
Since I couldn't get it to print from a SQL Server job, I created a simple batch file that calls bcp and after that the printhtml program (code see below).
When I run the batch from outside SQL Server, everything works fine and the html file will be printed.
But as soon as SQL Agent is involved, it will just hang forever at the printhtml program (the file will still be created) until I manually cancel the job.
I added a separate proxy to use my windows credentials instead of the SQL Agent account, but that didn't help either.
After a while I figured that the printhtml process will start (checked using task manager), but won't print. When killing this process, the SQL Agent job will finish immediately. Further "investigation" lead me to the assumption that this program does something like open the standard web explorer in a silent mode, print the file from there and close the app.
But the SQL Server Agent service seems to be unable to interact with another program that uses some sort of a GUI. To verify this assumption I changed the batch file and just opened the html file an an IE window instead of trying to print it. The job would still hang until I manually closed the IE tab.
My next approach was to try a PowerShell job step.
I tried the sample script based on http://stackoverflow.com/questions/7189299/print-automatically-html-file-with-powershell
But that didn't work either (disclaimer: my PowerShell experience is limited to the few lines of code I copied...).
Does anyone have an idea how to accomplish the task without using CLR, SSRS, SSIS? It would be a lot easier, if the application currently calling the sproc would be able to call a PS script, but that's not an option either. The print process needs to be started by the server, not the clients (the printer is installed at the server only).
Any ideas or hints? Am I hitting in the completely wrong direction?
Here's some very basic sample code:
-- procedure to create a html file
exec LutzM_printhtml 'some text'
CREATE PROC dbo.LutzM_printhtml
FOR xml PATH('test')
-- drop proc dbo.LutzM_printhtml
batch file (to be called using a cmdexec job step):
bcp "EXEC tempdb.dbo.LutzM_printhtml 'some text'" queryout c:\temp\file.html -c -T
LutzA pessimist is an optimist with experience. How to get fast answers to your question How to post performance related questions
Links for Tally Table
, Cross Tabs
and Dynamic Cross Tabs
, Delimited Split Function