Is it possible in SQL Server 2008 to select columns not by their names, but in the order as they appear in the table?
The reason is that i want to select the first 5 oder 6 columns of a table, no matter what's the content, because it is possible that their names or the columns self can be changed or moved.
For the first 5 columns you can try this:
select column_name,ordinal_position
from information_schema.columns
where table_schema = ...
and table_name = ...
and ordinal_position <= 5
Hope this works now.
Solution found here.
Edit: Updated answer - old one only selected first 5 rows, not columns.
Related
I have a table that is partitioned on one or more columns. I can do ...
SHOW PARTITIONS table_db.table_1
which gives a list of all partitions like this,
year=2007
year=2015
year=1999
year=1993
but I am only interested in finding which columns the table is partitioned on, in this case, year. And I would like to be able to do this of multiple tables at once, giving me a list of their names and partitioned columns somewhat like this.
table_name partition_col
table_1 year
table_2 year, month
I tried the solutions here...
https://docs.aws.amazon.com/athena/latest/ug/querying-glue-catalog.html#querying-glue-catalog-listing-partitions
SELECT * FROM table_db."table_1$partitions"
does give me results with one column for each partition...
# year
1 2007
2 2015
3 1999
4 1993
...but I couldn't extract the column names from this query.
Try this.
SELECT table_name,
array_join(array_agg(column_name), ', ') as partition_col
FROM information_schema.columns
WHERE extra_info = 'partition key'
GROUP BY 1
Get metadatas with AWS client provided in your language, like boto3 athena for python
import boto3
client = boto3.client()
table = client.get_table_metadata(
CatalogName=catalog,
DatabaseName=database,
TableName=name
)["TableMetadata"]
partition_keys = table["PartitionKeys"]
it seems solution is for mysql not SQL Server.
We need to provide list of columns for around 50 tables/views names (which will be given in one excel). I am using below query for which I need to replace table name and run the query for 50 times. Is there a one shot query whose where clause will have all the table names? e.g.
Select table_name,column
from
where table_name in(Mytableq,Mytable,2...Mytable50)
Query I am using currently:
1.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwPDPSubCase'
2.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwDDPcase'
.
.
.
50.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwXYZcase'
My question is, is it possible to list all the columns from the whole database not just in specific tables based on 3 different criteria which ones are in an "OR" relationship. so for example I have database called "Bank" and I have 3 criterias "Criteria1; Criteria2; Criteria3" and if any of them is true so the relation between them should be OR and not AND than I will get back all the columns matching the criterias and the output put should provide "account_id" or "customer_id" from the same table.
How do I procced in this case?
It is possible, but you probably don't want to do it. Anyway, you could write a stored procedure that finds all tables that contain the columns you want:
select distinct table_name from user_tab_cols utc
where exists (select * from user_tab_cols where table_name = utc.table_name
and column_name = 'ACCOUNT_ID')
and exists (select * from user_tab_cols where table_name = utc.table_name
and column_name = 'CUSTOMER_ID');
Given the tables you could run a query where you append table name and your criteria:
execute immediate 'select account_id, customer_id from agreement where '
|| your_criteria_here;
A bit messy, inefficient and treat this as pseudo-code. However, if you really want to do this for an ad-hoq query it should point you in the right direction!
Hi i need to concat all rows from my table.
I have this query select * from table1; this table contains 400 fields
i cannot do this select column1 ||','||column2||','||.....from table1
can someone help e to fix it using select * from table1 to concatinate all rows.
And thank you.
In Oracle (and similar in other DBMS) you could use system tables.... and do this in two steps:
Assuming you want to combine all the columns into 1 column for X rows...
STEP 1:
SELECT LISTAGG(column_Name, '|| Chr(44)||') --this char(44) adds a comma
within group (order by column_ID) as Fields
--Order by column_Id ensures they are in the same order as defined in db.
FROM all_tab_Cols
WHERE table_name = 'YOURTABLE'
and owner = 'YOUROWNER'
--Perhaps exclude system columns
and Virtual_Column = 'NO'
STEP 2:
Copy the results into a new SQL statement and execute.
The would look something like Field1|| Chr(44)||Field2|| Chr(44)||Field3
SELECT <results>
FROM YOURTABLE;
which would result in a comma separated list of values in 1 column for all rows of YOURTABLE
If the length of all the columns (along with , space and ||) would exceed the 4000 characters allowed... we can use a clob data type instead through the use of XML objects...
* Replace step 1 with *
SELECT RTRIM(XMLAGG(XMLELEMENT(Column_ID,Column_Name,'|| Chr(44)||').extract('//text()') order by Column_ID).GetClobVal(),'|| Chr(44)||') fields
FROM all_tab_Cols
WHERE table_name = 'YOURTABLENAME'
and owner = 'YOUROWNER'
--Perhaps exclude system columns
and Virtual_Column = 'NO';
Syntax for the above attributed to This Oracle thread but updated for your needs.
Is there any way to prefix all the columns in a table when doing a select without doing the following with 44 fields from each table?
BEGIN
SELECT a.example, a.something, b.example as c_example, b.something as c_something
INTO AEC_CIS_SVC_PIPE_COMP
FROM AEC_CIS_SVC_PIPE_V V
FULL OUTER JOIN AEC_CIS_SVC_PIPE_EXT E
ON V.Serv_pipe_num = E.Serv_Pipe_Num
END;
Addition:
Or a suffix would work too (ie. b.example as example_c)
Thanks,
Troy
No, you have to list all the columns out.
One thing you can generate the list by running a query in Oracle:
select 'a.'||column_name||' as a_'||column_name||', '
from all_tab_cols
where table_name = 'whatever';
And then copy the results into your query.