How to use Column Alias in Informix database table - informix

I want to use column aliases while selecting the column in an informix database table.
For example in the following query:
SELECT hire_dt as "Hire Date" FROM employee
Column hire_dt should be displayed as Hire Date. How can I do this in informix?

This is fine as long as you have the DELIMIDENT environment variable set. Read up about it in the documentation.

In you connection string you can use:
Database=mydatabase;Host=192.168.100.1;Max Pool Size=3;
Min Pool Size=1;Pooling=True;Protocol=onsoctcp;Password=informix;
Server=ol_server;Service=1256;User ID=informix;delimident=y
delimident=y

Related

The query is not working?

In my application I use sqlite as a Backing store.For my use I create Two tables for Surgeon and Schedule with surgeon having columns Name(VARCHAR),id(int) and Schedule having id(int),Surgeon(int),Values(VARCHAR).
The Surgeon column in Schedule table is pointing to the id column in Surgeon table. I use The Below query to select values from the Schedule table for the Surgeon with id=1
SELECT Schedule.Values,Name from Schedule,Surgeon where Schedule.Surgeon==Surgeon.id and Surgeon.id=2
But I got error as below
SQLiteManager: Likely SQL syntax error: SELECT Schedule.Values,Name from Schedule,Surgeon where Schedule.Surgeon==Surgeon.id and Surgeon.id=2 [ near "Values": syntax error ]
I don't know where it went wrong, I have't used the database before so forgive me if the question is much basic
You have named one of the columns in Schedule  as values Change it to something else that is not a key word for sqlite. you should not use key string to name the column  First you have to take a look at the keys in sqlite
If u want to take the Name, then try Surgeon.Name

.group not returning all columns

I have a .group query that is not returning all the columns in the select and I was wondering if someone could validate my syntax.
Here is a query with a .group and the result from my console;
Expense.select('account_number, SUM(credit_amount)').group(:account_number).first
Expense Load (548.8ms) EXEC sp_executesql N'SELECT TOP (1) account_number, SUM(credit_amount) FROM [expenses] GROUP BY account_number'
(36.9ms) SELECT table_name FROM information_schema.views
Even though I select two columns, I'm only getting the first one to return. I'm wondering if I may be dealing with an db adapter problem.
Try giving your sum an alias:
expense = Expense.select('account_number, SUM(credit_amount) AS credit_amount').group(:account_number).first
puts expense.credit_amount
ActiveRecord doesn't create a default alias for aggregation operations such as SUM, COUNT etc... you have to do it explicitly to be able to access the results, as shown above.
The SUM(credit_amount) column from the SQL has no alias and will not have a column name by default. If you change it to have an alias SUM(credit_amount) As 'A' for example and select the alias name, it should pick it up.

Safety SQL Dynamic Column Query in Postgresql (No Sql Injection)

I'm using Rails to get data from Postgresql by passing dynamic column and table name.
I cannot use ActiveRecord because the shape data that is imported from shapefile is dynamic both table and column name.
I have to use double quote with a column name in the query to avoid problem such column name: "addr:city" for example.
def find_by_column_and_table(column_name, shape_table_name)
sql = "SELECT \"#{column_name}\" FROM \"#{shape_table_name}\" WHERE \"#{column_name}\" IS NOT NULL"
ActiveRecord::Base.connection.select_one(sql)
end
2 examples of generated sql statement:
SELECT "place" FROM "shp_6c998258-32a6-11e0-b34b-080027997e00"
SELECT "addr:province" FROM "shp_6c998258-32a6-11e0-b34b-080027997e00"
I want to make sure there is no sql injection in the query.
Could anyone point me how to solve this issue?
The recommended way to prevent injection, speed up your query and catch errors is to use positional parameters or stored proceedures. Anything less is asking for trouble.
http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/
http://www.postgresql.org/docs/9.0/static/sql-expressions.html#AEN1834

Updating joined queries with ADO

I have a select query that joins 3 tables to display information. If I want to update a field is do
ADOQuery.Edit.
<Set fields Values>
ADOQuery.Post.
I get the following error howwever 'Insufficient key column information for updating or refresing' and I'm not sure how to proceed.
Thank you.
Pieter
Try this OnCreate:
YourADODataSet.Properties['Unique Table'].Value := 'YourTable';
This will let the Engine send only one (instead of two) query from joined tables.

Create missing auto increment attribute with rails migration

I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with change_column or something?
You need to execute an SQL statement.
statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT"
ActiveRecord::Base.connection.execute(statement)
Note this is just an example. The final SQL statement syntax depends on the database.
If you're on postgesql, a single request won't make it. You'll need to create a new sequence in the database.
create sequence users_id_seq;
Then add the id column to your table
alter table users
add id INT UNIQUE;
Then set the default value for the id
alter table users
alter column id
set default nextval('users_id_seq');
Then populate the id column. This may be quite long if the table has many rows
update users
set id = nextval('users_id_seq');
Hope this helps postgresql users...
The Postgres answer by #jlfenaux misses out on the serial type, which does all of it for you automatically:
ALTER TABLE tbl add tbl_id serial;
More details in this related answer.

Resources