Variable table or column names - sql

INFORMIX-SQL or any other SQL-based DB:
Suppose I have an app where depending on the value of some columns, example:
company.code char(3) {abc}
company.branch char(2) {01}
Can I construct table name "abc01" for inclusion in SELECT * FROM abc01; ?
In other words, a variable table name.. same question applies for column names.

Only in a language which can manipulate character strings and handle Dynamic SQL. It has to create the statement on the fly.
You can only use placeholders in queries for values, not for the structural elements of the query such as the table name or column name.

Only if you use dynamic sql. It's like you build sql code in your app, then use something like execute immediate.
sprintf(cdb_text1, "create table %s (field1 char(3));", usr_db_id);
EXEC SQL execute immediate :cdb_text;
If you use dynamic sql, it's bad because of sql injections.

Related

Can an SQL query select variables into different tables witn an INTO clause?

I'm working myself through this guide to how to convert several character variables at once to numeric using SAS. The guide uses PROC SQL, the SQL interface of SAS. What got me wondering is the part following the INTO-clause below:
proc sql noprint; select
trim(left(name)), trim(left(newname)),
trim(left(newname))||'='||trim(left(name))
into :c_list separated by ' ', :n_list separated by ' ',
:renam_list separated by ' '
from vars;
Essentially, the clause seems to select each variable into a different table. Are you allowed to do this in SQL, or is this only a feature of PROC SQL in SAS?
I tried the syntax at several SQL sandboxes, but couldn't get it to work.
It is a feature of PROC SQL in SAS to read values into SAS Macro Variables.
Only the CREATE TABLE clause specifies the name of the table the query should create from the result set delivered by the SELECT clause.
The INTO clause in SAS' implementation of SQL is different than then INTO clause implemented in other flavors of SQL.
From SAS documentation
INTO Clause
Stores the value of one or more columns for use later in another PROC SQL query or SAS statement.
Restriction: An INTO clause cannot be used in a CREATE TABLE statement.
...
INTO macro-variable-specification
< , … macro-variable-specification>
Compare the above with Microsoft Transact-SQL INTO clause
[ INTO new_table ]
...
new_table
Specifies the name of a new table to be created, based on the columns in the select list and the rows chosen from the data source.
For these two the general patterns are
SAS: CREATE TABLE abc as SELECT ...
MS: SELECT ... INTO abc
The SAS/MS comparison for selecting a column into a variable is
SAS (: indicates into variable)SELECT expression-1,...,expression-N
INTO :macro-variable-1,...,:macro-variable-N
MS (# indicates into variable)SELECT expression-1,...,expression-N
INTO #:tsql-variable-1*,...,#tsql-variable-N
The variables the values are selected into have different scope contexts depending on system or implementation.
AFAIK there is no method to create multiple tables with a single query in SAS or ANSI SQL.
However, this is trivial within a SAS data step using a variety of methods.
The most basic:
data male female;
set sashelp.class;
if sex='M' then output male;
else if sex='F' then output female;
run;

BigQuery CREATE TABLE with dynamic table name [duplicate]

I am trying to write a BigQuery script that I can store as a procedure, I would like one of the arguments I pass to be used in the table name that is written out by the script, for example:
DECLARE id STRING;
SET id = '123';
CREATE OR REPLACE TABLE test.id AS(
SELECT * FROM dataset.table
)
However, in this example the table is created with the name id rather than the value of the "id" variable, 123. Is there any way I can dynamically create a table using the value of a declared variable in the BigQuery UI?
Why not just use Execute Immediate with concat if you know the table schema?
EXECUTE IMMEDIATE CONCAT('CREATE TABLE `', id, '` (column_name STRING)');
So far we have officially announced BigQuery scripting document, which is still in Beta phase, leveraging usage of dynamic parameters (variables) as a placeholders for values in SQL queries . However, according to Parameterized queries in BigQuery documentation, query parameters can't be used for SQL object identifiers:
Parameters cannot be used as substitutes for identifiers, column
names, table names, or other parts of the query.
Maybe you can use a wildcard table. You would create a wildcard table with all subtables you want to query and use the WHERE clause to select any subtable you want. Just be careful, the tables must have the same schema.

Create table using a variable name in the BigQuery UI

I am trying to write a BigQuery script that I can store as a procedure, I would like one of the arguments I pass to be used in the table name that is written out by the script, for example:
DECLARE id STRING;
SET id = '123';
CREATE OR REPLACE TABLE test.id AS(
SELECT * FROM dataset.table
)
However, in this example the table is created with the name id rather than the value of the "id" variable, 123. Is there any way I can dynamically create a table using the value of a declared variable in the BigQuery UI?
Why not just use Execute Immediate with concat if you know the table schema?
EXECUTE IMMEDIATE CONCAT('CREATE TABLE `', id, '` (column_name STRING)');
So far we have officially announced BigQuery scripting document, which is still in Beta phase, leveraging usage of dynamic parameters (variables) as a placeholders for values in SQL queries . However, according to Parameterized queries in BigQuery documentation, query parameters can't be used for SQL object identifiers:
Parameters cannot be used as substitutes for identifiers, column
names, table names, or other parts of the query.
Maybe you can use a wildcard table. You would create a wildcard table with all subtables you want to query and use the WHERE clause to select any subtable you want. Just be careful, the tables must have the same schema.

SQL - adding new column withouth effecting existing scripts

We have a table on the SQL Server 2008 which gets populated by various stored procedures. The problem is that the authors of these stored procedures used some poor choice in code and populated this table using the following syntax:
INSERT INTO persistant_table
SELECT *
FROM #temp_table_with_data
Basically they would create a #temp_table_with_data in the script and the columns would be in the same order and with the same name as they are in the persistant_table.
Now I need to add another column to this persistant_table, but if I do that, I will break all the stored procedures.
Is there a way for me to add a column to this table without breaking all the stored procedures? (In the long run, we will change the stored procedures).
Thank you
No I think.select * will pick all columns and column number should match.
I don't think it's big effort to change the line to have particular columns only or select statement to have default value for column or null and then * to store into columns sequentially. But at least 1 line to be changed
The "ALTER TABLE" is a SQL statement that allows you to make datatype changes to a database table (i.e. change datatype as well as Size columns from an existing table).
ALTER TABLE TableName ALTER COLUMN ColumnName NVARCHAR(200)
You cannot do it without affecting old scripts. This is why 'SELECT *' is not good practice. You'd better create new scripts with explicit column names like
SELECT column1, column2 ....

Deleting a temp table based on a string value?

Is there a way in sql server 2008 to delete #tt using a string:
delete temp table with name = 'tt'
edit: what would really be nice if there was a function like: getString(#tt) that returned 'tt'
You could do this with dynamic SQL:
EXEC('DROP TABLE #' + #MyTempTable)
If you go this route I would give this article a good read to get an understanding of the pros and cons of dynamic sql.
Although you can do this with dynamic SQL, I'm not sure how you are going to create varying temp tables in a parent session scope with non-dynamic SQL and then use dynamic SQL within a child session scope.
Can you give an example of what you are doing to create the tables and where you want them to be dropped?