Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

pass MS SQL server scripts into Win32::ODBC

by Anonymous Monk
on Jan 23, 2013 at 22:07 UTC ( #1015037=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks and mongers!

while using Win32::ODBC to query a MS SQL server database, simple select statements work very well with $connection->Sql. I have a need of executing lengthy prebuilt SQL query files or scripts.

I can easily do this by shelling out to the command line to execute an osql or sqlcmd and run the script, capture the results in an array or hash

But... I would prefer to use Win32::ODBC and keep it all in Perl if I can

All attempts I have made, that work with simple select statements error, when I substitue the select statements with the script file name or a variable which contains the name of the file.

Any guidance will be appreciated

Comment on pass MS SQL server scripts into Win32::ODBC
Re: pass MS SQL server scripts into Win32::ODBC
by Corion (Pope) on Jan 23, 2013 at 22:12 UTC

    You will have to read the files and execute each statement through the database handle.

    Maybe DBIx::RunSQL does what you want?

      Thanks, I will take a look at that

Re: pass MS SQL server scripts into Win32::ODBC
by roboticus (Canon) on Jan 24, 2013 at 02:02 UTC

    Knowing what the eror messages are might be helpful. But in any case, it should be achievable. Using DBI and DBD::ODBC I routinely do updates, stored procedure calls in MSSQL, so I expect that it should be easy enough with Win32::ODBC. You might try checking the tests for Win32::ODBC to see how the author does it.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you for your input, it is appreciated.

      with Win32::ODBC, I am able to execute stored procedures and basic queries routinely. It wasn't until I had the need to run a prebuilt script file, where the problem arose.

      after much research tonight, it appears that this module isn't built to handle it and it was suggested to use DBI and DBD:ODBC, as you have.

      I did some testing tonight using the DBI DBD:ODBC combination in which I read the file in and prepare each line in my loop and then at file end issue the execute.

      This produces errors on the prepare, "incorrect syntax near...", I then tried to execute each line after the prepare and still get similar errors.

      I know the sql script is solid and working. I can run it from the management console of osql/sqlcmd without problems. I must be ordering something incorrectly, not sure. I have a good understanding of the both modules, but am better with Win32::ODBC, but it seems simple enough to pick up with the DBI

      I looked for tests on Win32::ODBC, but didn't find anything in addition to the perdoc and info on CPAN.

      The SQL script looks like this:

      use db

      set nocount on

      declare @MRN varchar(255)

      set @MRN = 'xxxxxxxx'

      ;

      select distinct id as patientid, firstname, lastname

      from person P join patient_iorg PI on id = personid

      where OrganizationMRN = @MRN;

      The error is either on the from or the where depending on where I position the execute statement..

        None of these statements are valid SQL:

        use db set nocount on declare @MRN varchar(255) set @MRN = 'xxxxxxxx' ;

        Most likely, they are interpreted by whatever command line shell you use to connect to your database, and they most likely only set up output parameters and template variables that will get replaced in the real SQL later. That functionality does not live in the ODBC component of the database driver, or the database driver.

        Either you shell out to the console and let it run the script, or you find out how to replicate the shell functionality with Perl.

        Some of these statements are transact-SQL and cannot be used over ODBC in the way you are doing it. You could create a procedure over ODBC which can use t-sql statements.

        Of course, I'm presuming there is more to the script than what you posted as it is effectively just a select statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015037]
Approved by mildside
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-08-22 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls