Informix DB; query output, forcing table output? - database

All..
I am currently using some systems that has an Informix DB on some older IBM AIX OS based systems. I have found myself needing to use the command line "dbaccess" feature to make some quick queries. Informix has this really annoying habit of return output in this format:
employee -1
record_desc Update
field_id 2
value
opr_activity_date 20150831
opr_activity_time 1
employee -1
record_desc Update
field_id 2
value
opr_activity_date 20150831
opr_activity_time 1
employee -1
record_desc Update
field_id 2
value
opr_activity_date 20150831
opr_activity_time 1
MySQL, MSSQL, etc.. all output something more readable in table format..
city state zipcode
Sunnyvale CA 94086
San Francisco CA 94117
Palo Alto CA 94303
Redwood City CA 94026
Los Altos CA 94022
Mountain View CA 94063
Palo Alto CA 94304
Redwood City CA 94063
I noticed that Informix will/can output in a column/table format, but I have not figured out any rhyme or reason as to how it decides the flat versus the table format.
Any idea how I can force Informix to always display in column/table output via the command line?
Obviously, this is not an issue when I am near my computer and can use my GUI tool to query the DB...

Unfortunately, there's no way to control this behaviour in DB-Access.
If the width of the selected columns (plus a little white space) exceeds the width of the terminal, DB-Access switches to that block format, because it doesn't support sideways scrolling. That's the rhyme and reason.
You can try messing around with your terminal settings so that DB-Access is aware on start-up that the terminal width is wider than 80 characters, but I've always found there's more luck than science to that, and you'll still trigger the behaviour on some queries and not others.
When I need to do what you're describing - ad hoc, simple queries for troubleshooting etc - I tend to work within VIM rather than DB-Access, and use a macro to run the query and format the output. (This is using DBI::Shell behind the scenes.) I've also got a program that accepts either a table name or SQL statement and outputs tab-delimited, CSV or an old-school ASCII character formatted table of the results. This is also perl based. I could publish either of these if there's interest in them.
I think Jonathan Leffler's SQLCMD program can also be used in place of DB-Access to generate arbitrarily wide output.

Ok..
While I found the answers RET provided to be correct and pretty much sums up what I have been able to find on the net, I also found some work-arounds that enable the ability to get what you want, but in a kludgy way! Thanks Informix! :(
Open two terminal windows to your DB system, and launch the dbaccess and authenticate and connect to your database.
Next perform the following:
unload to /home/(user)/out ...the query...
Example:
unload to /home/jewettg/out select * from books_checked_in;
It will output the query results to the file and return the row-count of the return result.
On the second terminal, and here is the cool thing, run the following command:
column -t -s '|' /home/(user)/out
This will grab the content of the "out" file, and convert the pipe-delimited content to space-delimit content and output it to the screen.
Like I said, kludgy, but it works!

You can do this by setting the DBACCESS_COLUMNS environment variable. It is supported from version 12.10.xC9.
Example:
export DBACCESS_COLUMNS=1000

Related

"?" character in MSSQL DB getting replaced with (capital A with grave accennt) when displayed by ASP script

I'm attempting to provide support for a legacy ASP/MSSQL web application - I wasn't involved in the development of the software (the company that built it no longer exists) & I'm not the admin of the server where it's hosted, I just manage the hosting for the owners of the site via a reseller account. I'm also not an ASP developer (more a PHP guy), and am not that familiar with it beyond the basics - updating DB connection strings after server migrations, etc.
The issue is that the site in question stores the content of individuals pages in an MSSQL database, and much of the content includes links. Almost all of the internal links on the site are format like "main.asp?123" (with "123" being the ID of a database row). The problem is, starting sometime in the last 8 months or so*, something caused the links in the DB content to show up as "main.aspÀ123" instead - in other words, the "?" character is being replaced by the "À" character (capital A with grave accent). Which, of course, breaks all of those links. Note that Stackoverflow won't allow me to include that character in the post title, because it seems to think that it indicates I'm posting in Spanish...?
(*unfortunately I don't know the timing beyond that, the site owners didn't know when the issue started occurring, so all I have to go by is an archive.org snapshot from last October, where it was working)
I attempted to manually change the "?" character in one of the relevant DB records to "?" (the HTML entity for the question mark), but that didn't make any difference. I also checked the character encoding of the HTML code used to display the content, but that doesn't seem to be the cause either - the same ASP files contain hard-coded links to some of the same pages (formatted exactly the same way), and those work correctly: the "?" doesn't get replaced.
I've also connected to the database directly with the MSSQL Management Studio Express application, but couldn't find any charset/character encoding options for either the database or the table.
And I've tried contacting the hosting provider, but they (M247 UK, in case anyone is curious) have been laughably unhelpful. The responses from them have been along the lines of "durrrrrr, we checked a totally different link that wasn't actually the one that you clearly described AND highlighted in a screenshot, and it works when we check the wrong link, so the problem must be resolved, right?" Suffice it to say, I wouldn't recommend them - used to be a customer of RedFox hosting, and the quality of customer has dropped off substantially since M247 bought them.
Any suggestions? If this were PHP/MySQL, I'd probably start by creating a small test script that did nothing but fetch one of the relevant records and display it's contents, to narrow down the issue - but I'm not familiar enough with ASP to do that here, at least not without a fair amount of googl'ing (and most of the info I can find is specific to ASP.net instead).
Edit: the thread suggested as a solution appears to be for character encoding issues when writing to MSSQL, not reading from it - and I've tried the solutions suggested in that thread, none make any difference.
Looks like you're converting from UNICODE to ASCII somewhere along the line...
Have a look at this to get a quick demo of what happens. In particular, pay attention to the ascii derived from inr, versus the ascii derived from unicode...
SELECT
t.n,
ascii_char = CHAR(t.n),
unicode_char = NCHAR(t.n),
unicode_to_ascii = CONVERT(varchar(10), NCHAR(t.n))
FROM (
SELECT TOP (1024)
n = ROW_NUMBER() OVER (ORDER BY ao.object_id)
FROM
sys.all_objects ao
) t
WHERE 1 = 1
--AND CONVERT(varchar(10), NCHAR(t.n)) ='À'
;
I found a workaround that appears to do the trick: I was previously trying to replace the ? in the code with &#63 (took out the ; so that it will show the code rather than the output), which didn't work. BUT it seems to work if I use &quest instead.
One thing to note, it seemed that I was originally incorrect in thinking that the issue was only affecting content being read/displayed from the MSSQL DB. Rather, it looks like the same problem was also occurring with static content being "echo'd" by code in the ASP scripts (I'm more of a PHP guy, not sure the correct term is for ASP's equivalent to echo is). Though the links that were hardcoded as static (rather HTML being dynamically output by ASP) were unaffected. Though chancing the ? to &quest worked for those ones too (hardest part was tracking down the file I needed to edit).

Zeppelin: How to see text results from JDBC query

On SQL queries zeppelin 0.8.1 provide table output and several visualizations of data out of the box:
And it is very useful most of the time.
But sometimes I want just select text for presentation.
Said for query SELECT version();. There table output is annoying:
What very interesting, there already implemented text output, for example for EXPLAIN:
Off course ideally for EXPLAIN query you may also expect more visualize for nods, cost and so on, but it is absolutely another question.
So, main question: How I can switch output to text form for some of my SQL queries except explain but in similar form?
Additionally, If I run some maintenance commands like VACUUM and ANALYZE I can see output in many IDE, but in zeppelin it is empty!
/*'EXPLAIN '*/ select version();
An ugly workaround can be used while JDBCInterpreter contains EXPLAIN_PREDICATE
private static final String EXPLAIN_PREDICATE = "EXPLAIN ";
String results = getResults(resultSet,
!containsIgnoreCase(sqlToExecute, EXPLAIN_PREDICATE), isComplete);
In future it's will be nice to manage output type via paragraph properties.
VACUUM and ANALYZE send messages which should be caught via Statement#getWarnings

Replace is not working for weird character

I use UPDATE a SET GR_P = REPLACE(GR_P,'','') FROM mytable a to replace things.
But replace function is not working for below charter:
In Query analyzer it works but when I used SSIS Execute SQL task or OLEDB Source then it is giving me error:
No Connection manager is specified.
In Toad against Oracle (since that's one of your tags), I issued this (pressing ALT-12 to get the female symbol) and got 191 as a result. note selecting it back using CHR(191) shows an upside-down question mark though.
select ascii('♀') from dual;
Given that, this worked but it's Oracle syntax, your mileage may vary.
UPDATE mytable SET GR_P = REPLACE(GR_P, CHR(191));
Note if it does not work, that symbol could be for another control character. You may need to use a regular expression to eliminate all characters not in a-zA-Z0-9, etc. I suspect you'll need to update your tags to get a more accurate answer.
Maybe this info will help anyway. Please post back what you find out.

I have an issue with sql query to extract install date of software in computer

I have a SQL query that will query for all the products installed on a computer, with the product publisher, name, and version. I have been trying to get the installed dates of the programs but since I'm a newbie at this, I'm really bad at this. So far they only extract the Product company, name and version. I need the dates of when they were installed in the computer.
SELECT DISTINCT
SYS.Netbios_Name0, SYS.Rsqlesource_Domain_OR_Workgr0,
SP.CompanyName, SP.ProductName, SP.ProductVersion
FROM
fn_rbac_GS_SoftwareProduct(#UserSIDs) SP
JOIN
fn_rbac_R_System(#UserSIDs) SYS ON SP.ResourceID = SYS.ResourceID
WHERE
SYS.Netbios_Name0 LIKE #variable
ORDER BY
SP.CompanyName, SP.ProductName, SP.ProductVersion
I don't know what exactly the base for whatever is inventoried in fn_rbac_GS_SoftwareProduct (or v_GS_SoftwareProduct for that matter) is but unfortunately it does not include the installation dates.
There are two views
v_GS_ADD_REMOVE_PROGRAMS
and
v_GS_ADD_REMOVE_PROGRAMS64
that get populated with the data from
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
and
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
respectively. They might be of use to you, although they have several caveats.
First of all as they come from the local machine part of the registry
they would not include software that was installed for only one user
(In MSI this is the Option "Install for Everyone" vs "Install only
for me").
Second you have to use both if you want the full information because one will only contain 32Bit programs and one only 64Bit programs.
Finally although they do include the field InstallDate it is literally based on the REG_SZ InstallDate in one of those keys and this is neither a mandatory field, nor a standardized one. This means A Program is basically allowed to just create it's uninstall key and not use this property. If it is created there is also no guarantee that the format will always we the same (e.g. in our company I found two formats YYYYMMDD and MM-DD-YYY) so if you want to process this column further you might have to sanitize it.
While this all sounds a little hopeless, it is perfectly possible that for a given scenario it is no problem at all. For example if you install all your applications as msi via sccm it is fair to assume that the InstallDate key will allways be present and in the same format.
The full query for this would be something like
SELECT
SYS.Name0, ARP.DisplayName0, ARP.Publisher0, ARP.InstallDate0
FROM
v_GS_ADD_REMOVE_PROGRAMS ARP
LEFT JOIN
v_R_System SYS ON SYS.ResourceID = ARP.ResourceID
WHERE
SYS.Name0 = #variable
AND NOT
(ARP.DisplayName0 like '%Update%' OR ARP.DisplayName0 like '%Service Pack%')
UNION
SELECT
SYS.Name0, ARP.DisplayName0, ARP.Publisher0, ARP.InstallDate0
FROM
v_GS_ADD_REMOVE_PROGRAMS_64 ARP
LEFT JOIN
v_R_System SYS ON SYS.ResourceID = ARP.ResourceID
WHERE
SYS.Name0 = #variable
AND NOT
(ARP.DisplayName0 like '%Update%' OR ARP.DisplayName0 like '%Service Pack%')
Note that I included some filter against Windows updates which would be included as well. Also I did not use the rbac functions but rather the views directly, I hope that is ok.

Script output to file when using SQL-Developer

I have a select query producing a big output and I want to execute it in sqldeveloper, and get all the results into a file.
Sql-developer does not allow a result bigger than 5000 lines, and I have 100 000 lines to fetch...
I know i could use SQL+, but let's assume I want to do this in sqldeveloper.
Instead of using Run Script (F5), use Run Statement (Ctrl+Enter). Run Statement fetches 50 records at a time and displays them as you scroll through the results...but you can save the entire output to a file by right-clicking over the results and selecting Export Data -> csv/html/etc.
I'm a newbie SQLDeveloper user, so if there is a better way please let me know.
This question is really old, but posting this so it might help someone with a similar issue.
You can store your query in a query.sql file and and run it as a script. Here is a sample query.sql:
spool "C:\path\query_result.txt";
select * from my_table;
spool off;
In oracle sql developer you can just run this script like this and you should be able to get the result in your query_result.txt file.
#"C:\Path\to\script.sql"
Yes you can increase the size of the Worksheet by change the setting Tool-->Preferences - >Database -> Worksheet -> Max rows to print in a script(depends on you).
Mike G answer will work if you only want the output of a single statement.
However, if you want the output of a whole sql script with several statements, SQL*Plus reports, and some other output formats, you can use the spool command the same way as it is used in SQL*Plus.

Resources