How to Assign the Output of an SQL Query to a Variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Assign the Output of an SQL Query to a Variable?
# 1  
Old 05-31-2017
How to Assign the Output of an SQL Query to a Variable?

Hi

iam new to shell scripting

how to declare variables as redshift query and I have to compare two counts by using if condition .

ex:count=select count(*) from prd;

Code:
select count(*) from prd;
 
select count(*) from tag;

can any one help me .
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments (as required by forum rules).

Last edited by Don Cragun; 06-01-2017 at 12:47 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 06-01-2017
Quote:
Originally Posted by sam526
Hi

iam new to shell scripting

how to declare variables as redshift query and I have to compare two counts by using if condition .

ex:count=select count(*) from prd;

Code:
select count(*) from prd;
 
select count(*) from tag;

can any one help me .
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments (as required by forum rules).
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in the Homework & Coursework Questions forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. Furthermore, unless you tell us what operating system and shell you're using (and, when using a database, what database software you're using), it is very hard for us to guess at what might work in your environment. Showing us what you have tried to do to solve this problem on your own always helps us help you.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sql query output count

Hi Team, below sql rerturn 20 records, the result set i am going to assign to one variable and it showing count is 1. and i don't use count() in sql query... based on count, i need to fail the script. No_of_step=`echo ${g_count} | wc -l` function gf_count() { g_count=`sqlplus -s... (8 Replies)
Discussion started by: bmk123
8 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. Shell Programming and Scripting

Assign the return value of the SQL to a variable in UNIX

Hi I am new to UNIX. I am trying the below and getting the error. I am trying to assign the variable with the value of the query result. I want this value to use in the next steps. Created UNIX file (Batch.sh) as below #!/bin/ksh sqlplus callidus/callidus4u@attstcal @Batch.sql ... (2 Replies)
Discussion started by: KrishBalu
2 Replies

4. Shell Programming and Scripting

How to Assign SQL Query Results to Variables in Linux?

Hi, I am new to linux... How to Assign SQL Query Results to Variables in Linux,i want ti generate it in param files, Can anyone please explain me. Ex: SQL> Select * from EMP; O/P: Emp_No Emp_Name 1 AAA 2 BBB 3 CCC and I want expected... (5 Replies)
Discussion started by: Sravana Kumar
5 Replies

5. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

6. UNIX for Dummies Questions & Answers

Assign SQL result in shell variable

Hi im trying to assign the result of the db2 command to a variable inside a shell script... : tab_cnt=`db2 "select count(*) from syscat.tables where tabname = 'ABC' and tabschema = 'MATT01'" |head -4|tail +4|cut -c 11` : echo $tab_cnt when i echo im getting a blank value.. im expecting... (1 Reply)
Discussion started by: matt01
1 Replies

7. Shell Programming and Scripting

bash assign mysql query single field to variable

I'm running a bash script query and assigning the output to a variable like this: exists=`mysql -u $USER_NAME --password=$PASSWORD -D "somedb" \ -e "SELECT * FROM somedb.sometable WHERE field1 ='$a' \ AND field2 ='$b' LIMIT 0 , 30";` which returns something like: echo... (2 Replies)
Discussion started by: unclecameron
2 Replies

8. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

9. Shell Programming and Scripting

How to store the sql query's output in a variable

Hi, My requirement is : We are calling an sql statement from a UNIX session, and fetching data into some variables from a table .. now we are unable to access these variables from outside the SQL part. Please let me know how can I achieve this. Can you please share a code snippet which... (4 Replies)
Discussion started by: venkatesh_sasi
4 Replies

10. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies
Login or Register to Ask a Question