How to create a C structure using table description.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create a C structure using table description.
# 1  
Old 11-13-2009
How to create a C structure using table description.

There is table 'DEPT' in the database with the following desciption:

Name Null? Type
------- -------- ------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME NULL VARCHAR2(14)
LOC NULL VARCHAR2(13)

Using shell script, I need to create a structure for the above table as follows and put this structure in dept.h file.
struct dept
{
int deptno;
char dname[15];
char loc[14];
};

Please help how this is created using shell script.
# 2  
Old 11-13-2009
Quote:
Originally Posted by ehari
There is table 'DEPT' in the database with the following desciption:

Name Null? Type
------- -------- ------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME NULL VARCHAR2(14)
LOC NULL VARCHAR2(13)

Using shell script, I need to create a structure for the above table as follows and put this structure in dept.h file.
struct dept
{
int deptno;
char dname[15];
char loc[14];
};

...
Since you haven't mentioned your database, I shall assume that it's Oracle. And that the table is the DEPT table of the SCOTT sample schema.

Code:
$ 
$ ## show the SQL script
$ cat -n test.sql
     1  --       
     2  with t as (
     3    select table_name, column_name, data_type, data_length
     4    from user_tab_columns                                 
     5    where table_name = 'DEPT')                            
     6  --                                                      
     7  select distinct
     8         'struct '||lower(table_name)||' {'
     9  from t
    10  union all
    11  select decode(data_type,'NUMBER','  int ','VARCHAR2','  char ')||
    12         lower(column_name)||
    13         decode(data_type,'VARCHAR2','['||to_char(data_length+1)||']')||';'
    14  from t
    15  union all
    16  select '};'
    17  from dual
    18  /
    19
$
$ ## show the shell script
$ cat -n getcstruct.sh
     1  #!/bin/bash
     2  sqlplus -s /nolog <<EOF
     3  connect scott/tiger
     4  set heading off feed off
     5  @test.sql
     6  EOF
     7
$
$ ## execute the shell script
$ . getcstruct.sh

struct dept {
  int deptno;
  char dname[15];
  char loc[14];
};
$
$

Also, I've assumed that you have 9i or higher; subquery factoring doesn't work in versions lower than that.

tyler_durden
# 3  
Old 11-16-2009
How to create a C structure using table description.

Many thanks to you tyler_durden, your solution worked!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

2. Shell Programming and Scripting

Extract hive table structure

Hi, I need to extract only the create table structure with columns alone. for eg hive_table show create table hive_table: create table hive_table(id number,age number) OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 'hdfs:/path/' I need only below ... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

3. UNIX and Linux Applications

Help in copying table structure to another table with constraints in Oracle

hi, i need to copy one table with data into another table, right now am using create table table1 as select * from table2 i want the constraints of table1 to be copied to table2 also , can anyone give me some solution to copy the constraints also, now am using oracle 10.2.0.3.0... (1 Reply)
Discussion started by: senkerth
1 Replies

4. Shell Programming and Scripting

Create a flat file and directory structure

Hi All, is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt Current Directory structure |-------------files |---------------Share |-----------------dir1 |-----------------dir2... (11 Replies)
Discussion started by: heros
11 Replies

5. Shell Programming and Scripting

Create Directory structure

Hello ; ) again Now I have my file like this : DIR2/DIR3 DIR2 DIR2/DIR3/DIR4/DIR5 I am looking for help to create a loop that will create the directory structure. I need something like this : If "DIR2" does not exist > Create IF "DIR2" exist already > check if onther "DIR"... (5 Replies)
Discussion started by: Aswex
5 Replies

6. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies

7. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

8. Shell Programming and Scripting

How to create a directory structure with getting input from a file.

Hi How to create a directory structure with getting input from a file. I have file in that following lines are written. ./activemq-4.1.2/activemq-core-4.1.2.jar ./activemq-4.1.2/backport-util-concurrent-2.1.jar ./camel-1.4.0/apache-camel-1.4.0.jar ./camel-1.4.0/lib/activation-1.1.jar... (12 Replies)
Discussion started by: itsjoy2u
12 Replies

9. Shell Programming and Scripting

Help to Create this file format structure

This data comes form the table and exported into the file in this format File1 Format weboffercode1,sourcecode1,1,1,1,1,1,1 weboffercode1,sourcecode2,1,1,1,1,1,1 weboffercode1,sourcecode1,1,1,1,1,1,1 weboffercode1,sourcecode3,1,1,1,1,1,1 weboffercode1,sourcecode3,1,1,1,1,1,1 ... (4 Replies)
Discussion started by: enigma_83
4 Replies
Login or Register to Ask a Question