Sponsored Content
Full Discussion: Dynamic SQL for where clause
Top Forums Shell Programming and Scripting Dynamic SQL for where clause Post 302350180 by TeSP on Thursday 3rd of September 2009 04:06:01 AM
Old 09-03-2009
Dynamic SQL for where clause

Hi,

I have an app which user can query the database based on 4 criteria, that is Field1, Field2, Field3 and Field4

Mya I know how to write a dynamic SQL where I can choose to retrieve data based on their selected value.

eg. where Field1=AAA
eg. where Field1=AAA and Field2=BBB
eg. where Field2=BBB and Field4=CCC, etc.

I have been trying to code:

mySQL="Select Field1, Field2, Field3, Field4 from table1 "

if [ $field1 != "All" ] ; then
myWhere=" where Field1='$field1' " ;
else
myWhere=" where Field1 like '%' " ;
fi

if [ $field2 != "All" ] ; then
myWhere= ${myWhere} || " minus " || ${mySQL} || ${myWhere} || " and Field2 <> '$field2' " ;
fi

if [ $field3 != "All" ] ; then
myWhere= ${myWhere} || " minus " || ${mySQL} || ${myWhere} || " and Field3 <> '$field3' " ;
fi

if [ $field4 != "All" ] ; then
myWhere= ${myWhere} || " minus " || ${mySQL} || ${myWhere} || " and Field4 <> '$field4' " ;
fi

mySQL=${mySQL} || ${myWhere} ;

sqlplus -S "$ORACCS" <<-__EOF__
${myWhere} ;
__EOF__


I am hitting syntax error while trying to compare Field2. Please advise how should I create a dynamic SQL. Thanks in advance.

Regards
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic SQl in KSH

My requirement is to create a KSH to generate the SQL select statement in oracle with all the columns and optional where condition if given the table name as input to the program Have any of you worked with a similar requirement? Can you give me some inputs? Regards, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies

2. UNIX for Dummies Questions & Answers

if clause

hi, pls could you help me with one program in KSH ( i have sunOS). I need to create an If clause, that prints an error message and filenames, when in a directory are found some files of null size (find . -type f -size 0 ). thanks (3 Replies)
Discussion started by: palmer18
3 Replies

3. UNIX for Advanced & Expert Users

Sql dynamic table / dynamic inserts

I have a file that reads File (X.txt) Contents of record 1: rdrDESTINATION_ADDRESS (String) "91 971502573813" rdrDESTINATION_IMSI (String) "000000000000000" rdrORIGINATING_ADDRESS (String) "d0 movies" rdrORIGINATING_IMSI (String) "000000000000000" rdrTRAFFIC_EVENT_TIME... (0 Replies)
Discussion started by: magedfawzy
0 Replies

4. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

5. Shell Programming and Scripting

Dynamic sql where contents

Hi all, I need to add the contents from a file into a sql stament in the where clause. file1: id 1 2 3 10 11 ... script should look like : select name from tab_user tus where tus.id in (1,2,3,10,11..) any ideas or suggetions will be appreciatte. (5 Replies)
Discussion started by: valigula
5 Replies

6. Shell Programming and Scripting

How to grep the where clause of a SQL?

Hi UNIX Gurus, I want to use extract the where clause of a SQL present in a file. Please suggest me how can I do it. Select * from emp where emp_id>10; cat <file_name> | grep -i "where" returns whole SQL. how can I extract only "where emp_id>10;" Thanks in advance (4 Replies)
Discussion started by: ustechie
4 Replies

7. Shell Programming and Scripting

How to extract WHERE clause from a SQL using UNIX???

Hi, I need to know how to extract entire WHERE clause from a SQL statement using UNIX command. For example: If following is the SQL then how to extract entire WHERE clause: UPDATE <TABLE_NAME_1> SET <FIELD_NAME> = VALUE WHERE CONDITION_1 AND CONDITION_2 = (SELECT FIELD_NAME FROM... (15 Replies)
Discussion started by: ustechie
15 Replies

8. Linux

Shell Script to generate Dynamic Param file Using SQL Plus Quey

Hi All, Can anyone give me Shell script sample script to generate Param file by Reading Values from SQL Plus query and it should assign those values to variables like.. $$SChema_Name='ORCL' Thanks in Advance... Srav... (4 Replies)
Discussion started by: Sravana Kumar
4 Replies

9. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

10. Shell Programming and Scripting

Replace sql with dynamic values

Hi Guys, I am using a function to replace the values dynamically to frame sql query by reading a file. My file will have column names like file.txt col_1 col_2 expected output: select id,col_1,col_2 from ( select a.id, a.col_1, rank() over (ORDER BY cast(a.col_1 AS double)... (5 Replies)
Discussion started by: Master_Mind
5 Replies
databaseintro(1grass)						Grass User's Manual					     databaseintro(1grass)

Database management in GRASS GIS
   Attribute management in general
       GRASS  can  be linked to one or many database management systems (DBMS).  The db.* set of commands provides basic SQL support for attribute
       management, while the v.db.* set of commands operates on the vector map (see Vector introduction).

   Available drivers
       Available drivers are listed in SQL support in GRASS GIS.

       Notes:
       The default DBF driver provides only very limited SQL support (as DBF is not an SQL DB) while the other DBMS backends (such as  PostgreSQL,
       MySQL etc) provide full SQL support since the SQL commands are sent directly to the DBMS.

   DB connection management
       The  current  database management settings are shown or modified with db.connect for current mapset. Available DBMI drivers are listed with
       db.drivers. Some DBMI backends require a user/password for driver/database to be set with  db.login.   In  order  to  test  a  driver,  run
       db.test.

   Attribute data import and export
       Attribute  data	can  be  imported  with  db.in.ogr from various formats and exported with db.out.ogr. To internally copy a a full table or
       selectively parts of it, use db.copy.

       Further conversion tools:

		     MDB Tools: Convert MS-Access data to SQL, DBF, etc.

		     Openoffice.org with SQL Databases

   SQL commands
       GRASS supports to main SQL operations, execution of an SQL statement (db.execute) and selection of data from a table (db.select).  See  the
       SQL help page for examples.

   Managing the default DBMI settings
       Per  default  vector  map attributes are stored in DBF table files. This default definition can be modified with db.connect. If an external
       DBMS is used, db.login may be required.

   Creating a database
       Specific commands are explained on the individual driver pages (these pages are only available if driver was  compiled  in  this  installa-
       tion):

		     DBF: see DBF page

		     SQLite: SQLite page

		     mySQL: meSQL pages

		     ODBC: ODBC page  (connect to Oracle, etc.)

		     PostgreSQL: PostgreSQL and PostGIS page

   Metadata
       All  columns  for  a  given  table  are listed with db.columns.	The command db.describe describes a table in detail. To list all available
       tables for a given database, run db.tables.

   Table maintenance
       To drop a column from a selected attribute table, use db.dropcol.  With db.droptable an attribute table can be deleted.

   Database Schema
       Currently schema support only works for PostgreSQL connections. Default schema can be set with db.connect. Note	that  the  default  schema
       will be used by all db.* modules.

       db.tables returns 'schema.table' if schemas are available in the database.

   Migrating to a different database engine
       To  migrate  a GRASS database table (or a GRASS vector map) to a different DBMI engine, the best solution is to create a new MAPSET, define
       the DBMI settings accordingly with db.login.  Then the table of interest can be copied over with db.copy from the  original  MAPSET.  Like-
       wise, a vector map including its table(s) are copied from the original MAPSET to the current MAPSET with g.copy.

   See also
		     Introduction to GRASS vector map processing

		     Introduction to GRASS raster map processing
       Main index - database index - full index

       (C) 2008-2011 GRASS Development Team

GRASS 6.4.2														     databaseintro(1grass)
All times are GMT -4. The time now is 09:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy