How to write select query if the condition contains single quotes(') - sql

I Want to retrive the data from Oracle DB where name contins single quote(')
Select * from table_name where name in ('Joel D'Silva','O'neil Dsa');

You could pass string to q'[]' in following:
Select * from table_name where name in (q'[Joel D'Silva','O'neil Dsa]');
or you could use quote ' twice in following:
Select * from table_name where name in ('Joel D''Silva'',''O''neil Dsa');

Replace ' with '':
Select * from table_name where name in ('Joel D''Silva','O''neil Dsa');

Related

How to find every table/columns in SQL with a string/number

I'm Searching for a query to find all possible columns and table names for a specific piece of string/number.
Let's say I have a string/number as 100031-16-FWMD-D20, is there a query to find all the columns and table names which has the value in my database?
Column name-----
Select * from information_schema.columns where column_ name like '%string%'
Table name----
Select * from information_schema.tables where table_name like '%string%'
I would suggest you string up the columns for text datatype from information_schema.columns and after that use the query which is creatd to manually query the search string
Step 1:
select 'select '+column_name+' as search_text,'''+table_name+''' as table_name from '+TABLE_SCHEMA+'.'+TABLE_NAME+' union all'
from INFORMATION_SCHEMA.columns
where TABLE_SCHEMA='dbo'
and data_type like '%char%'
Step 2:
select *
from(<output of Step 1 after removing the last 'union all')x
where x.search_text like '%100031-16-FWMD-D20%'

Oracle SQL using variable in Select or For-Loop

I need to verify data across several tables. In essence i want to write a loop for the below statement with all of the fields in a given table.
sql> select fld1, count(*)
from table1
group by fld1
;
I'm thinking that I need to create at least 2 variables. The first variable would be prompted to provide the table name.
The second var would be something based on the result of :
select column_name from user_tab_col_statistics where table_name = table_variable
Should I also create a temp table and select into that?
As per my understanding you can store the values in the temporary table.
1.Fetch Table Name & Column Name put in Variable Var_Table_name,Var_Col
select Var_Col, count(*) into Var1,Var2 from Var_Table group by fld1 ;
for all the tables columns .You can create a temporary table and insert the values as mentioned below and store in the temp table .
1.Var_Table_name
2.Var_Col
3.Var1
4.Var2
PL/SQL does not prompt. On the other hand SQL*Plus will prompt for substitution variables. See following example.
MPOWEL01> #stack
MPOWEL01>
MPOWEL01> select table_name, column_name from user_tab_col_statistics where table_name = upper('&tbl_nm')
2 order by column_name;
Enter value for tbl_nm: marktest
old 1: select table_name, column_name from user_tab_col_statistics where table_name = upper('&tbl_nm')
new 1: select table_name, column_name from user_tab_col_statistics where table_name = upper('marktest')
TABLE_NAME COLUMN_NAME
------------------------------ ------------------------------
MARKTEST FLD1
MARKTEST FLD2
MARKTEST FLD3
MARKTEST FLD4
MPOWEL01>
set verify off will eliminate the substitution message line from the output.
In PL/SQL you either need to SELECT INTO variables or use a cursor.
I think you might just be able to use SQL to generate the SELECT statements you want to run, but what exactly do you mean by 'verify data'? Verify how? Using what standard?

Table name alias in netezza

I use the following commands in netezza SQL to define table name alias while running a query through command line NZSQL
\set catgtable 'table_a'
\echo table name is :catgtable
and I use the table name alias catgtable wherever required as in:
select *
from :catgtable where col1 is not NULL
My problem is how to use the alias name in these 2 situations:
select ':catgtable' as table_name, col1,col2
from table_x
This gives me an error, since it requires the actual table name.
select table_name, column_name
from _v_sys_columns
where table_name = ':catgtable' and column_name like '%merch_cat_%'
Here again the alias: catgtable doesn't work and it requires the actual table name.
Let me know if there is a workaround.
When you want to use the contents of a variable as a literal in nzsql you need to include an additional set of single quotes, escaped with backslashes, around the literal text when you set the variable.
TESTDB.ADMIN(ADMIN)=> \set tvar '\'BLAH\''
TESTDB.ADMIN(ADMIN)=> select :tvar col_alias;
COL_ALIAS
-----------
BLAH
(1 row)

Find all tables whose name ends with a certain suffix

I have thousand of tables in database. Some names end with _History.
For example :
abc_History
bcd_History
123_History
How do I find all tables which name is end with _History.
Some thing like:
SELECT
table_name
FROM sys.tables WHERE table_name LIKE '_History%'
And
error : Invalid column name 'table_name'.
Try this:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_NAME LIKE '%_History'
OR
SELECT name
FROM sys.tables
WHERE name LIKE '%_History'
Thanks to #Saharsh-shah.
To get table name with all column names in the same query, use this :
SELECT
`TABLE_NAME`,
group_concat(`COLUMN_NAME` separator ',') AS `COLUMNS`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME` IN(
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE `TABLE_NAME` LIKE '%_History'
)
GROUP BY `TABLE_NAME`
You can push it in multidimensional PHP array very simply with the following :
$tables['columns'] = explode(',', $tables['columns']);
Hope it can help some.

Query to pull all the table names and column names

I need a query (ORACLE) to pull all the table names and column names in a database for a given value?
Example: If I give a value as "TEST", I need a query which pulls all the TABLE_NAMES and COLUMN_NAMES which has the value "TEST".
select table_name, null column_name from all_tables where table_name like '%TEST%'
union all
select null, column_name from all_tab_columns where column_name like '%TEST%';
Another way is using bind variables within a procedure such
DEFINE vSearch = '%TEST%'
ACCEPT vSearch char PROMPT 'Enter a search value: '
SELECT *
FROM USER_TAB_COLS
WHERE column_name LIKE '&&vSearch'
OR table_name LIKE '&&vSearch';
View All columns of a particular table user use "ALL_TAB_COLUMNS"
If you wants to describe a particular table user use "DESC Table_name;"
Kindly Try this..
You can use the USER_TAB_COLUMNS tables
This should get you columns and tables and views:
SELECT 'Column: '||owner||'.'||table_name||'.'||column_name
FROM dba_tab_columns
WHERE column_name = 'TEST'
UNION ALL
SELECT 'Table: '||owner||'.'||table_name
FROM dba_tables
WHERE table_name = 'TEST'
UNION ALL
SELECT 'View: '||owner||'.'||view_name
FROM dba_views
WHERE view_name = 'TEST';
Note you can also use the ALL_* dictionary views if you don't have access to the DBA_ views, but you'll only see objects you have access to.