T SQL alias column name based on query from a table - tsql

I have to display a column name from a select query from table, e,g, 'Column 1' is stored in table table1, I want to display 'Column 1' as column name. I cannot hardcode 'Column 1' as it might be unknown when the code is developed.
Column 1 Column 2
a b
Any idea?
Update
It allows user to define column name.
It has to generate via T SQL

Add alias as a separate column or use dynamic SQL
This doesn't make sense because doing you should know what the column is called: you don't store information about columns and build queries on your own metadata

IF you don't know the name of the column at design time, then use a resource file or similar in the presentation layer to hold the value that you will display as the label for column1. It should be noted, however, you are going to have a very hard time writing any code if you don't know the names of the columns in your database, unless you are select * on everything.

Column names are fairly restricted and should be named with the SQL admin and application developer as the user of the names. It is not the intent of a column name to be descriptor for the end user interface.
Select dbColumn1 as [Customer Name] from tableMain
Even that is not a good practice and then user input is part of the TSQL and you are opening yourself up to SQL injection attacks and it is just not very good control on you query. A better practice is to pass parameters.

Related

Handling the output of jsonb_populate_record

I'm a real beginner when it comes to SQL and I'm currently trying to build a database using postgres. I have a lot of data I want to put into my database in JSON files, but I have trouble converting it into tables. The JSON is nested and contains many variables, but the behavior of jsonb_populate_record allows me to ignore the structure I don't want to deal with right now. So far I have:
CREATE TABLE raw (records JSONB);
COPY raw from 'home/myuser/mydocuments/mydata/data.txt'
create type jsonb_type as (time text, id numeric);
create table test as (
select jsonb_populate_record(null::jsonb_type, raw.records) from raw;
When running the select statement only (without the create table) the data looks great in the GUI I use (DBeaver). However it does not seem to be an actual table as I cannot run select statements like
select time from test;
or similar. The column in my table 'test' also is called 'jsonb_populate_record(jsonb_type)' in the GUI, so something seems to be going wrong there. I do not know how to fix it, I've read about people using lateral joins when using json_populate_record, but due to my limited SQL knowledge I can't understand or replicate what they are doing.
jsonb_populate_record() returns a single column (which is a record).
If you want to get multiple columns, you need to expand the record:
create table test
as
select (jsonb_populate_record(null::jsonb_type, raw.records)).*
from raw;
A "record" is a a data type (that's why you need create type to create one) but one that can contain multiple fields. So if you have a column in a table (or a result) that column in turn contains the fields of that record type. The * then expands the fields in that record.

"Invalid column name" when "Specify database fields" in table output is unchecked in pentaho PDI V7

I'm trying to insert data to SQL database. I have all the columns in the same order as the data flow. But I'm getting this "Invalid column name name_of_the_actual_data_column" error
Column order won't matter, but exact column names will. Your SQL implementation probably isn't picky enough to require case-sensitive matches, but spaces and punctuation will matter. With Specify database fields unchecked, all field names MUST exist as columns in the target table.
I've found a good way to troubleshoot SQL inserts is to put a Select step before the Table output and make sure that you're really only getting the columns you want to insert.
You can also right-click on the Table output step and choose Input fields... to see the column metadata being passed into the step.

Changing the order of headers in Firebird SQL

I am a novice at Firebird SQL.
Can anyone advise how I specify the order of field name in Firebird? If I change the name of the header, then it places that field at the end.
In another situation the field names appear differently in Excel that they do in the preview. (Can't see any particular pattern, not even aphabetical or alphabetical per table).
Any hints are appreciated.
I understand your question as you want to change the fields order in a table, permanently.
For that, Firebird FAQ gives this DDL-statement:
ALTER TABLE table_name ALTER field_name POSITION new_position;
Positions are numbered from one. If you wish to exchange two fields, make sure you run the statement for both of them.
If you want to change the fields order temporary, e.g. in a query, you can obviously just define the fields order in the select statement. But I think, this is one of the first things a SQL novice learns.
By SQL
ALTER TABLE TABLE_NAME ALTER COLUMN FIELD_NAME POSITION x;
http://firebirdsql.org/refdocs/langrefupd20-alter-table.html#langrefupd20-at-position
Far as I know, there is no option to automatically arrange the fields. But you may use your preferred tools like Flame Robin, IBExpert etc. to do this manually. They have this functionality.
For example Flame Robin :

table naming and organizing of tables in postgresql

I'm trying to organize my PosgtresSql tables according to the application components they correspond to. For example, tables related to 'story' such as 'story_contents', 'story_comments', 'story_layout': would it best to keep a simple '' naming convention as presented? Would there be any drawback to using a '.' instead of ''? ... or is there a best practice that I'm completely overlooking?
Short answer:
Sure, if you place the entire database/table/column name reference in quotes
Long answer:
In Postgres, and most other databases, the dot is used to separate database name from table name, and table name from column name. For example, if you had a database called MyDB, with a table called MyTable and column in that table MyCol, then you could write the following SELECT statement:
SELECT MyDB.MyTable.MyCol
FROM MyDB.MyTable
However, if your database, table, and/or column names themselves had dots in them, then doing a SELECT might not work. In this case, I believe you can escape the fully qualified name (or portion) with quotes. So, if you had a column called MyCol.Col1, you could do the following:
SELECT "MyDB.MyTable.MyCol.Col1"
FROM MyDB.MyTable
The comment by #vector seems to be pointing in the right direction (no pun intended), and you should lean towards using underscores or some other character to separate out your schema names, rather than using a dot.

Is it possible to make a TABLE PARAMETER in CR?

I would like to ask , is that got possible to change the table name just by using the TABLE PARAMETER setting? but the column name consists in the both table are the same.
eg:
table 1(dailyregistration)
table 2(dayreg)
P.S: both table got same column name, just the table name had change to (dayreg)
select * from ????????
I'm not too sure what you mean by TABLE PARAMETER, but it is not possible to use a Crystal parameter as one of the tables in the report's source. You can only achieve this effect by either writing your own application to set the data location to the one you want, or by using a stored procedure containing dynamic SQL and the table name as a parameter.