SQLite Forum

Only Column Names read from table
Login
If you are referring to SQL Server, you might be using SSMS. 

If so, expand the database to show all table names and then expand any given table, you will see a <b>Columns</b> option. Click on <b>Columns</b> and drag it to the query window. <i>This will enumerate the columns names in that given table separated by comma at the cursor position.</i> (easier/faster than writing queries for an equivalent result!).

For SQLite, using the shell, try the following (<i>which is a copy of my session - note the version I am using</i>):

```
SQLite version 3.35.4 2021-04-02 15:20:15
Enter ".help" for usage hints.
sqlite>
sqlite> /* Open your database ... chinook.db in my example */
sqlite> .open ./db/chinook.db
sqlite> /* Initialize parameters */
sqlite> .param init
sqlite> /* set the value of parameter my#Table to table name artists */
sqlite> .param set :myTable artists
sqlite> /* Set the columns names as a SELECT statement */
sqlite> select 'select ' || group_concat(replace('[#]','#', name),',') || ' from ' || :myTable || ';' as SelStmt from pragma_table_info(:myTable);
SelStmt
--------------------------------------
select [ArtistId],[Name] from artists;
sqlite> /* try another table, say, employees Remember parameter names are case-sensitive */
sqlite> .param set :myTable employees
sqlite> select 'select ' || group_concat(replace('[#]','#', name),',') || ' from ' || :myTable || ';' as SelStmt from pragma_table_info(:myTable);
SelStmt                                                                                                                                                         
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
select [EmployeeId],[LastName],[FirstName],[Title],[ReportsTo],[BirthDate],[HireDate],[Address],[City],[State],[Country],[PostalCode],[Phone],[Fax],[Email] from employees;
sqlite>
```

The relevant results were:

>select [ArtistId],[Name] from artists;

and 

> select [EmployeeId],[LastName],[FirstName],[Title],[ReportsTo],[BirthDate],[HireDate],[Address],[City],[State],[Country],[PostalCode],[Phone],[Fax],[Email] from employees;

Change the value of parameter :myTable (as desired) for scripting other tables

```
sqlite> .param set :myTable invoices
sqlite> select 'select ' || group_concat(replace('[#]','#', name),',') || ' from ' || :myTable || ';' as SelStmt from pragma_table_info(:myTable);
SelStmt                                                                         
-------------------------------------------------------------------------------------------------------------------------------------------------------
select [InvoiceId],[CustomerId],[InvoiceDate],[BillingAddress],[BillingCity],[BillingState],[BillingCountry],[BillingPostalCode],[Total] from invoices;
```
The relevant result:

>select [EmployeeId],[LastName],[FirstName],[Title],[ReportsTo],[BirthDate],[HireDate],[Address],[City],[State],[Country],[PostalCode],[Phone],[Fax],[Email] from employees;

This forum frowns upon the wrapping of column names within square brackets. It works with the SQLite shell and I always use them; I think square brackets add clarity. Optionally, you might consider editing the script to remove them.

<b>PS:</b> Using a parameter for the name of the table means that you specify the table name <i>once</i> only.