Sql multi line comment /* shell interpretation issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sql multi line comment /* shell interpretation issue
# 1  
Old 07-01-2015
Sql multi line comment /* shell interpretation issue

Greetings Experts,

I do have some basic knowledge of Unix. The task I am trying to do through shell script is to generate the view script for all of the tables which is in YYYYMMDD format (I assume I am on Ksh).

I have certain tables that ends in YYYYMMDD format (eg: tbl_20150630)
For each month, there is a table with respective YYYYMMDD (eg: tbl_20150131, tbl_20150228, ...., tbl_20150630). There are many different tables (tbl_2_20150131,.....,tbl_2_20150630)
For each table of the same class (tbl_20150131, tbl_20150228, ...., tbl_20150630), I need to create the view (not a plain select on table; using some case and other logics) (vw_tbl_20150131, vw_tbl_20150228,...,vw_tbl_20150630)

Sample view structure and expected output (should be until tbl_20150630): (For simplicity using minimal columns and 2 tables)
Code:
create or replace view vw_tbl_20150131 as select (col1+col2) /* PAYMENTS */, (case when col3 = 'T' then col4*(0.9) else col4 end) /* DISCOUNT */ from tbl_20150131;

create or replace view vw_tbl_20150228 as select (col1+col2) /* PAYMENTS */, (case when col3 = 'T' then col4*(0.9) else col4 end) /* DISCOUNT */ from tbl_20150228;

Approach I followed is to pass the view_name and the list of YYYYMMDD views to be created and have the view body separated into some files with below content.

selectpart1.txt:
Code:
as select (col1+col2) /* PAYMENTS */, (case when col3 = 'T' then col4*(0.9) else col4 end) /* DISCOUNT */ from tbl_

yyyymmdd.txt: (please note that not all tables starts with 20150131; hence created them in separate file of valid values)
Code:
20150131
20150228
-----
20150630

What I have tried is (courtesy of my friends) the below (may not be the exact one as I cannot copy paste) which works fine. ( I am in mydir directory)

Code:
# $1 = vw_tbl_
touch mydir/view_tbl_creation.sql
for i in `cat mydir/yyyymmdd.txt`
do

temp=`cat mydir/selectpart1.txt`
echo "create or replace view "$1$i $temp$i ";\n" >> mydir/view_tbl_creation.sql

done

When I execute the above shell script, could see wherever the comments are present in the selectpart1.txt ( /* ) , there are list of files displayed in the output of the above one, which I guess is due to shell interpretation of /* (all files in the directory). When I replace the contents of file selectpart1.txt where comments are there ( /* ) with -- it works.

selectpart1.txt:
Code:
 as select (col1+col2) -- PAYMENTS , 
 (case when col3 = 'T' then col4*(0.9) else col4 end) -- DISCOUNT 
 from tbl_

and I get the output as expected. However, I prefer to retain the /* comments in the selectpart1.txt; How can I achieve this. Thanks for your time.

Note: I edit the selectpart1.txt to have the view body relative to the tables involved. (selectpart1.txt content doesn't change for vw_tbl_20150131, vw_tbl_20150228, ....., vw_tbl_20150630). But when the table class changes, I edit the selectpart1.txt accordingly which will be same for (vw_tbl_2_20150131, vw_tbl_2_20150228, ...., vw_tbl_2_20150630)
# 2  
Old 07-01-2015
Try:
Code:
awk '
        NR == FNR {
                A[$1]
                next
        }
        {
                for ( k in A )
                        print "create or replace view vw_tbl_" k " " $0 k ";"
        }
' mydir/yyyymmdd.txt mydir/selectpart1.txt > mydir/view_tbl_creation.sql

If selectpart1.txt is multi record file:-
Code:
awk '
        NR == FNR {
                A[$1]
                next
        }
        {
                S = S ? S RS $0 : $0
        }
        END {
                for ( k in A )
                        print "create or replace view vw_tbl_" k " " S k ";"
        }
'  mydir/yyyymmdd.txt mydir/selectpart1.txt > mydir/view_tbl_creation.sql


Last edited by Yoda; 07-01-2015 at 05:05 PM..
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

C cedilla Delimiter interpretation issue

Hi Folks, I am trying to generate a file with the C Cedilla delimiter. I have a file that uses the below DML (In Dev region, the file that i am trying to generate): decimal("Ç") acct_id; and so and so for new columns When I cat this file I get below output:... (3 Replies)
Discussion started by: eap714.com
3 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. Shell Programming and Scripting

Quick and easy way to comment out multi lined print statements

Is there a quick and easy way to comment out multi lined print statements? something like this? printf("3408 strings_line_tokens %s \n", strings_line_tokens); (6 Replies)
Discussion started by: cokedude
6 Replies

4. Shell Programming and Scripting

Reading Multi Line SQL in UNIX

Hello, Currently, I am reading few queries from the below .sql file --SOURCE TABLE NAME --SOURCE QUERY SEL COL1, COL2, COL3, COL4, COL5, COL6, COL7 WHERE COL5 = '2015-11-04 16:24:00.000000' FROM SOURCE TABLE; --TARGET TABLE NAME --TARGET QUERY SEL COLUMN1, COLUMN2, COLUMN3, COLUMN4,... (4 Replies)
Discussion started by: ronitreddy
4 Replies

5. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

6. Shell Programming and Scripting

Sql issue in shell scripting

HI friends , i am also facing an issue in mysql i ma trying to insert detail in a variable but not got success #!/bin/sh mysql -u<username> -p<password> <dbname> << EOF DEV=`mysql --skip-column-names <dbname> -e "SELECT timestamp from process_record where id = 1"` EOF echo $DEV ERROR... (3 Replies)
Discussion started by: sanjay833i
3 Replies

7. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

Multi Line 'While Read' command issue when using sh -c

Hi, I'm trying to run the following command using sh -c ie sh -c "while read EachLine do rm -f $EachLine ; done < file_list.lst;" It doesn't seem to do anything. When I run this at the command line, it does remove the files contained in the list so i know the command works ie... (4 Replies)
Discussion started by: chrispward
4 Replies

9. Shell Programming and Scripting

SQL sorrting issue, and unwanted blank line above heading

Hi, I am running a shell script on Compaq Tru64 UNIX V5.1A. I have attached the shill script with the sql script it is calling to extract some data(hyp_dta_Extr.sql), and the results. I need the file to be tab delimited. (please note that I have renamed hyp_dta_Extr.sql to hyp_dta_Extr.txt to... (1 Reply)
Discussion started by: dazz
1 Replies

10. Shell Programming and Scripting

comment and uncomment a line with Shell Script

Requirement is: 1. comment and uncomment the line with Shell Script: /opt/admin/fastpg/bin/fastpg.exe -c -=NET (using fastpg.exe as a search option) 2. display = "Commented" (when its commented) and display = "Uncommented" (when its uncommented) Its urgent, please let me asap!!! Thanks in... (2 Replies)
Discussion started by: anthonyraj75
2 Replies
Login or Register to Ask a Question