Give a title to each sql query output - sql

I have a sql script which is nothing but a combination of multiple "Select" queries like:
Select * from ABC
Select * from CD
Select * from EN
Now when I execute it, I use to get output like
<output 1>
<output 2>
<output 3>
Requirement: I need some title to be displayed for each of the output.
To be more clear,I want output like:
Heading for Output of SQL query 1
output 1
Heading for Output of SQL query 2
output 2
Heading for Output of SQL query 3
output 3
Database is SQL Server 2008 R2

There're a lot of ways for achieving this. What exactly do you need this for?
1.
SELECT 'ABC' As title
Select * from ABC
SELECT 'CD' As title
Select * from CD
SELECT 'ABC' As title
Select * from EN
2.
Select 'ABC' As title, * from ABC
Select 'CD' As title, * from CD
Select 'EN' As title, * from EN
3.
Works for SQL Server. Not sure about other db's
PRINT 'ABC'
Select * from ABC
PRINT 'CD'
Select * from CD
PRINT 'ABC'
Select * from EN

Related

SQL - Run Select Statement Based On Where Query

Hi i want to create a query which does the following. When the paramter 25 is selected it only runs part A of the query, if any other number is selected run both Table A and B select queries.
Example Below:
DECLARE #Type varchar (200)
select * from
(SELECT sort_code FROM dbo.Test 1
WHERE FUNDING_YEAR = 26)
union
(SELECT sort_code FROM dbo.Test 2
WHERE FUNDING_YEAR = 26)
Where case when #Type = 25 then select * from table2 else table 1
You just need to reference the variable in the WHERE clause
SELECT *
FROM TableA
WHERE #Type = 25
UNION
SELECT *
FROM TableB
The query above will always select everything in TableB and will only select everything in TableA when the variable is equal to 25.
Since you are using SSRS, what I would do is write the query to return all of the rows and then apply a filter in the SSRS report when the Paramater is 25. I wouldn't pass a paramater value to the SQL side unless it greatly reduces the run time of the query.
(I would have put this in a comment.)

How to see query with the same SQL_TEXT but different parameter in history in v$sql?

I want to check history of queries to specific table( let's call it specific.table) in oracle sql.
I have a query:
Query 1:SELECT sql_id, sql_text FROM v$sql WHERE sql_text like "%specific.table%"
Let's say I did one query like this
Query 2: SELECT * FROM specific.table WHERE specifc.table.something = :1 where :1 is parameter.
Now I can use Query 1 to check Query 2 in history and it will be there.
But then when I make Query 2 with different bind variable, it is not visible in history returned by Query 1. Why the second Query 2 is not visible? Is there a way to check parameter :1 used in sql every time I use Query 2?
Edit:
Query I am using for checking bind variables:
SELECT * FROM v$sql_bind_capture WHERE sql_id = "id"
Edit 2:
Responding to Rene:
I want two make two queries like this:
select * from ngm_gebruiker where naam = 'A';
select * from ngm_gebruiker where naam = 'B'
and then check v$sql for the table:
SELECT sql_id, sql_text FROM v$sql WHERE sql_text like '%ngm_gebruiker%
but the result is
3 g71fx4fyyku6n select * from ngm_gebruiker where naam = :"SYS_B_0"
and when I check bind variable for g71fx4fyyku6n, I can only get A.
I would like to check bind variable for both queries.
Here's an example with three different queries on the same table:
select * from ngm_gebruiker;
select * from ngm_gebruiker where naam = 'Piet';
select * from ngm_gebruiker where naam = 'Piet' and idc_monitor='N';
Result of querying v$sql for that table:
SELECT sql_id, sql_text FROM v$sql WHERE sql_text like '%ngm_gebruiker%'
2 a0q4f137bffs6 select * from ngm_gebruiker
3 g71fx4fyyku6n select * from ngm_gebruiker where naam = :"SYS_B_0"
4 4vpyf8g2q711h select * from ngm_gebruiker where naam = :"SYS_B_0" and idc_monitor=:"SYS_B_1"

Bash For loop logic in oracle sql

I am looking for something similar to the bash for loop in oracle
for i in 1 5 3 8; do echo "print $i"; done
so this would result as
print 1
print 5
print 3
print 8
I want something similar logic in Oracle SQL like
for i in 1 5 3 8; do echo " select * from TABLE where column1='$i';"; done
so this would result as
select * from TABLE where column1='1';
select * from TABLE where column1='5';
select * from TABLE where column1='3';
select * from TABLE where column1='8';
So how do I get something similar logic in Oracle SQL
I hope that I understood your request correctly:
SELECT r FROM (
SELECT ROWNUM r
FROM DUAL
CONNECT BY ROWNUM <= 8)
WHERE r IN (1, 5, 3, 8)
Basically, we're creating a column of all numbers from 1 through 8 and then selecting the ones you requested. If you're going to work with strings or with a large range of numbers (but a small amount of rows), this solution won't be very efficient, and then you should store your values in a table.
You can build a loop in plsql, for example:
begin
for i in (
select 1 as num from dual union all
select 5 from dual union all
select 3 from dual union all
select 8 from dual
)
loop
dbms_output.put_line('number: ' || i.num);
/* do whatever you need */
end loop;
end;
/
If you need this to run a query, maybe you can simply use the IN:
select *
from table
where column1 in (1, 5, 3, 8)
or, to build a more complex list:
select *
from table
where column1 in (
select something
from ...
)

Redirecting query based on Sql (Inserting values into Dual)

My Idea is to insert multiple rows into dual table.
I have a query in which my requirement is when I enter % as my parameter it should got to 1 query else it should go to another query . It is showing a error at (&a1='%')
When I execute this query it asks for prompt and I will enter % and it should got to the query "select module_package, module_name from ops$glimsne.tim_modules"
My code is as follows
DECLARE
ab tim_modules_tbl%rowtype;
a11 tim_modules_tbl%rowtype;
begin
(
select a1, b1 into ab,a11 from (
case
when &a1='%' Then
(select module_package, module_name from ops$glimsne.tim_modules)
else
(select module_id, module_name from ops$glimsne.tim_modules)
end
)dual );
END;
It should display all values in Dual table

Union All Command in SQL

SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID from terminal b;
This is the code , but in the first statement as you can see there is no PROFILEID column. I would like to write " null " to that column because we don't know the PROFILEID of them. How can I write "null" to that column ?
Try this Query:
SELECT a.VPNID||'|'||a.PUBLICNUMBER||'|'||a.PRIVATENUMBER||'|'||'NULL'
from virtualterminal a
UNION ALL
SELECT b.VPNID||'|'||b.PUBLICNUMBER||'|'||b.PRIVATENUMBER||'|'||b.PROFILEID
from terminal b;