I need a stored procedure which in parameter have name of table(varchar) and it return names of columns in this specific table.
It is possible ? I think about some SELECT which retrieve this names from table but I am weak at SQL :/
I add that I use Firebird 1.5 :/
You don't need stored procedure. Just a simple request can make this :
SELECT r.RDB$FIELD_NAME AS field_name
FROM RDB$RELATION_FIELDS r WHERE
r.RDB$RELATION_NAME='TABLE_NAME' --table name
ORDER BY r.RDB$FIELD_POSITION;
you can learn more here : http://www.alberton.info/firebird_sql_meta_info.html
Related
I am trying to build bigquery stored procedure where I need to pass the table name as a parameter. My code is:
CREATE OR REPLACE PROCEDURE `MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES` (table_name STRING)
BEGIN
----step 1
CREATE OR REPLACE TABLE `MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES_01` AS
SELECT DISTINCT XX.HH_ID, A.ECR_PRTY_ID, XX.ANCHOR_DT
FROM table_name XX
LEFT JOIN
(
SELECT DISTINCT HH_ID, ECR_PRTY_ID
FROM `analytics-mkt-cleanroom.Master.EDW_ECR_ECR_MAPPING`
WHERE HH_ID NOT LIKE 'U%'
AND ECR_PRTY_ID IS NOT NULL
)A
ON XX.HH_ID = A.HH_ID----one (HH) to many (ecr)
;
END;
CALL MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES(`analytics-mkt-cleanroom.MKT_DS.Home_Services_Multi_Class_Aesthetic_Baseline_Final_Training_Sample`);
I followed a couple of similar questions here and here, tried writing an EXECUTE IMMEDIATE version of the above but not able to work out the right syntax.
I think issue is; the SELECT statement in my code is selecting multiple columns XX.HH_ID, A.ECR_PRTY_ID, XX.ANCHOR_DT and the EXECUTIVE IMMEDIATE setup is meant to work only for one column. But I'm not sure. Please advise. Thank you.
I am basically trying to write stored procedures for data pipeline building.
Hope below is helpful.
pass a parameter as a string.
CALL MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES(`analytics-mkt-cleanroom.MKT_DS.Home_Services_Multi_Class_Aesthetic_Baseline_Final_Training_Sample`);
-->
CALL MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES('analytics-mkt-cleanroom.MKT_DS.Home_Services_Multi_Class_Aesthetic_Baseline_Final_Training_Sample');
use EXECUTE IMMEDIATE since a table name can't be parameterized as a variable in a query.
----step 1
EXECUTE IMMEDIATE FORMAT("""
CREATE OR REPLACE TABLE `MKT_DS.PXV2DWY_CREATE_PROPERTY_FEATURES_01` AS
SELECT DISTINCT XX.HH_ID, A.ECR_PRTY_ID, XX.ANCHOR_DT
FROM `%s` XX
LEFT JOIN
(
SELECT DISTINCT HH_ID, ECR_PRTY_ID
FROM `analytics-mkt-cleanroom.Master.EDW_ECR_ECR_MAPPING`
WHERE HH_ID NOT LIKE 'U%%'
AND ECR_PRTY_ID IS NOT NULL
)A
ON XX.HH_ID = A.HH_ID----one (HH) to many (ecr)
;
""", table_name);
escape % in a format string with additional %
LIKE 'U%'
-->
LIKE 'U%%'
see PARSE_DATE not working in FORMAT() in BigQuery
Hi I'm a junior developer, I just want to ask Is it possible to add a new column in a stored procedure in db2? what i mean like an alter table for adding a new column but in stored procedure?
Yes, it's possible but you have to use dynamic sql.
--# SET TERMINATOR #
create table test_add_col(a int) in userspace1#
begin
execute immediate 'alter table test_add_col add b int';
end#
select colname
from syscat.columns
where tabschema=user and tabname='TEST_ADD_COL'#
The result is:
COLNAME
--
A
B
I want to retrieve data in Delphi using a stored procedure. I used the below SQL statement and Initial as a parameter:
SELECT * FROM "PackUser" where Initials in (:Initial)
It didn't select any records when the user types A,B in my Edit box, because it sends a single string 'A,B' to the stored procedure. I want to add extra quotes in the middle: 'A','B'.
How can I do this?
This can be done like this:
input_string=',A,B,C.D'
SELECT * FROM "PackUser" where locate(concat(',', Initials), input_string);
I am doing calculations in my KLOG table.
However, my PRICES table has the data I need for the calculations in the KLOG table.
Example :
KLOG table has PRICE_ID field (integer). So does the PRICES table.
So I am trying to do something like this (oncalculatefields of the KLOG table ) :
if KLOG.FieldByName('PRICE_ID') = 1 then begin
KLOG.FieldByName('calculated_field_value_1').Value := KLOG.FieldByName('calculated_field_value_2').Value +5;
This (+5) however is a field value (BONUS) in my PRICES table where PRICE_ID =1.
So how can I reference this BONUS field in my oncalculate event of the KLOG table?
Can you use SELECT ? Something like :
KLOG.FieldByName('calculated_field_value_1').Value := KLOG.FieldByName('calculated_field_value_2').Value + (select BONUS from PRICES where PRICES.PRICE_ID = KLOG.PRICE_ID);
Not sure I am writing this properly.
You are not telling us what data access component you are using.
The recommended way to do this is using a Query component (TSQLQuery, TADOQuery, TIBQuery,...) , then have the SQL for that query retrieve all necessary information. All the fields that you want access to in you OnCalcFields() should be in that query.
It would be something like
select * from KLOG, PRICES
where KLOG.PRICE_ID=PRICES.PRICE_ID
Replace the * with the field names you actually want.
If you want to, you can even do simple calculation in the query already, e.g. something like
select [..], PRICES.PRICE*KLOG_QUANTITY as TotalPrice from KLOG, PRICES
where KLOG.PRICE_ID=PRICES.PRICE_ID
Using query components instead of table components is good practice anyway; (together with TClientDataSet...) it will generally allow you to retrieve smaller amounts of data than an entire table.
I have a TSimpleDataSet based on dinamically created SQL query. I need to know which field is a primary key?
SimpleDataSet1.DataSet.SetSchemaInfo(stIndexes, 'myTable' ,'');
This code tells me that i have a primary key with name 'someName', but how can i know which field (column) works with this index?
A Primary Key/Index can belong to several columns (not just one).
The schema stIndexes dataset will return the PK name INDEX_NAME and the columns that construct that PK/Index (COLUMN_NAME). INDEX_TYPE will tell you which index types you have (eSQLNonUnique/eSQLUnique/eSQLPrimaryKey).
I have never worked with TSimpleDataSet but check if the indexes information is stored in
IndexDefs[TIndexDef].Name/Fields/Options - if ixPrimary in Options then this is your PK. and Fields belongs to that index.
Take a look at the source at SqlExpr.pas: TCustomSQLDataSet.AddIndexDefs.
Note how TCustomSQLDataSet returns the TableName (and then the indexs information) from the command text:
...
if FCommandType = ctTable then
TableName := FCommandText
else
TableName := GetTableNameFromSQL(CommandText);
DataSet := FSQLConnection.OpenSchemaTable(stIndexes, TableName, '', '', '');
...
I think the simple data set does not provide that information.
However, i am sure there are components for that. Check, for Oracle database, Devart's ODAC.
Basically, it involves only one query to the database.
However, it is not something that components will offer by default as, because it involves a different query, it leads to slow response times.
For Oracle database, query on user_indexes.