why dbeaver puts ""(quotes) to my column name? - sql

I created a table in my SQLite database in DBeaver. When I generate SELECT query:
SELECT "col1", col2, col3, col4 FROM mytable;
there is double quotes on my first column name. I directly created it as col1 and in table view it shows as col1. But in SQL query it "col1". There is something that i don't know related to SQLite? col1,3,4 are TEXT by the way.
I rewrite its name in table view but its still same.

Related

SQL select and alter table in same statement

I have a table where I would like to select certain columns and then create transformed columns based on that selection. Due to security reasons, I'm not able to create a new table and thought there may be a way to SELECT and ALTER in the same statement.
My statement below runs, but the column is not produced. Am I doing something wrong/is this approach not possible? Is there a better approach?
SELECT * col1,col2,col3 FROM db
AS db2
ALTER TABLE db2 ADD col4 AS (transformation) PERSISTED
Guidance and recommendation is appreciated.
You likely need just perform a query or, if you want query data often, then view in database.
In SQL server and most likely in other SQL variants you can create view with next statement (db would be table name, db2 view name):
create view db2 as
select col1, col2, col3, (transformation) as col4
from db
Usually you need to grant some permissions to it, unless you are sole user of it.
Creating a view is one-time task, afterwards you can query data as follows:
select col1, col2, col3, col4
from db2
where ...

Rename a single column while using select * in oracle

I am selecting all column of my table (eg 40 columns) and i want to rename only one column (e.g col20 ). how can i rename this column in my select query while selecting all column using select *. I don't want to write the name of all column. one more thing i also don't want to change the order of column in my table
Short answer is, you can't.
Either you have to select all the columns individually, using
select col1, col2, col3, ..., col20 as NewCol, ...., col40 from table
or use
select * from table
You can't have both.
Another option is there which you can use like below, but this will add an extra column in the output.
select t.*, t.col20 as NewCol from table t

apache hive column comment with CTAS

Sorry for all the setup. This is a hive datatype and comment question.
I have a single file in HDFS which combines 4 sets of table data. Breaking the data out ahead of time is not my preferred option. The first 4 rows specify the column headers:
*1 col1, col2, col3
*2 cola, colb, colc, cold, col5e
etc....
data rows begin with matching number at position 1 of the header.
1 data, data, data,
2 data, data, data, data, data,
etc...
The base hive table is just col0 - col60 for the raw file. I've tried creating a CTAS table to hold all of the "1" columns and one for the "2" columns where I can specify data type, and comments. Since the column names vary, I cannot give the columns names on the base table nor can I comment them with column based metadata.
This DDL didn't work but giving an example of what I'm hoping to do. Any thoughts ?
CREATE TABLE foo (
col1 as meaningful_name string comment 'meaningful comment')
as
SELECT col1
FROM base_hive table
WHERE col1 = 1;
CREATE TABLE foo
as
SELECT col1 string comment 'meaningful comment'
FROM base_hive table
WHERE col1 = 1;
thanks TD
I dont understand much what you are trying to achieve here, but looking at your DDL, I can see some errors. For the correct CREATE TABLE AS SELECT implementation, pl use the below DDL:
CREATE TABLE foo (
col1 STRING COMMENT 'meaningful comment')
AS
SELECT col1 AS meaningful_name
FROM base_hive table
WHERE col1 = 1;

Exporting Result Set to Another Table?

How can you export a result set given by a query to another table using SQL Server 2005?
I'd like to accomplish this without exporting to CSV?
INSERT INTO TargetTable(Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM SourceTable
insert into table(column1, columns, etc) select columns from sourcetable
You can omit column list in insert if columns returned by select matches table definition. Column names in select are ignored, but recommended for readability.
Select into is possible too, but it creates new table. It is sometimes useful for selecting into temporary table, but be aware of tempdb locking by select into.
SELECT col1, col2, ...
INTO dbo.newtable
FROM (SELECT ...query...) AS x;

Need SQL help - How can I select rows to perform an insert?

I tried to make the title as clear as possible... here is my scenario:
I have 2 tables (let's call them table A and table B) that have a similar schema. I would like write a stored procedure that would select specific columns of data out of table A, and insert this data as a new record in table B.
Can someone point me in the write direction to make such a query? I am unsure how to "Hold" the values from the first query, so that I may then perform the insert.
I am trying to avoid making a query, processing it with C# and then making another query...
Thanks.
INSERT INTO B (Col1, Col2) SELECT Col1, Col2 FROM A
Is this what you mean?
You can do this as a single query from C# like this:
Insert into tableB (col1, col2, col3) select col1, col2, col3 from tableA where ...
The trick is that column names need to be in the same order and compatible types.
use a SELECT INTO
SELECT
[Col1],
[COl2]
INTO TableA
FROM TableB