LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2022, 09:31 PM   #1
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 21

Rep: Reputation: Disabled
perl script with db query output to file


Hello, I would like to setup a perl script that connects to a microsoft sql database server executes a query then outputs to a specified file on the machine that runs the perl without the column header. I would like it to overwrite the file each time it is run.

example file contents starting with first line
cow
dog
mouse
horse
boar



Any pointers would be greatly appreciated.

I am running perl 5.16.

Thanks in advance.
 
Old 08-24-2022, 11:20 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,353
Blog Entries: 1

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
Have you got MsSql client programs (sqlcmd) installed?
https://docs.microsoft.com/en-us/sql...l-server-ver16
 
1 members found this post helpful.
Old 08-25-2022, 01:20 AM   #3
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Have you got MsSql client programs (sqlcmd) installed?
https://docs.microsoft.com/en-us/sql...l-server-ver16
How can I check what version is installed if any?
 
Old 08-25-2022, 01:26 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,353
Blog Entries: 1

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
As a start, try command `sqlcmd -?`
 
1 members found this post helpful.
Old 08-25-2022, 03:18 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
Quote:
Originally Posted by solidbulk View Post
How can I check what version is installed if any?
looks like you need to learn a lot. Why do you want to connect at all (is this some kind of homework)? Where is this database running?
There are a lot of different ways to connect to a database and retrieve some data, but without details hard to say anything.
this is probably a way: https://www.easysoft.com/developer/l..._dbd_odbc.html
 
2 members found this post helpful.
Old 08-25-2022, 10:08 AM   #6
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
As a start, try command `sqlcmd -?`
Yes this (MsSql client programs (sqlcmd) is available on the machine.
 
Old 08-25-2022, 11:16 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,417
Blog Entries: 3

Rep: Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359
Quote:
Originally Posted by solidbulk View Post
I am running perl 5.16.
And which distro is this on, including version?
 
Old 08-25-2022, 12:28 PM   #8
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
And which distro is this on, including version?
rhels 7.9
 
Old 08-26-2022, 10:33 AM   #9
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 21

Original Poster
Rep: Reputation: Disabled
@NevemTeve I was able to output cmdline query to a file. I removed the header using -h -1 option , and placed set no count on in the beginning of the query to remove the (rows affected).
 
1 members found this post helpful.
Old 08-26-2022, 12:04 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,928
Blog Entries: 4

Rep: Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587Reputation: 3587
As others have observed, you do not have to "write a Perl script" in order to do something so trivial, especially if it is a "one-off one-time" requirement. There are plenty of both command-line and GUI tools which can run a query and send the query output to a file (in a variety of formats, with or without headers).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL query output to excel file using perl script listenerit Linux - Newbie 3 02-17-2014 02:35 AM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
Perl script/mysql select query from a file mcdrr Programming 12 06-05-2007 12:00 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration