Is it possible to search multiple columns from multiple tables by using variables for references? - sql

I am using a search algorithm to find tables in my database that meet certain criteria. The output looks like this:
TableName | ColumnName
--------------+---------------
Table1 | Column1
Table1 | Column2
Table1 | Column3
Table2 | Column4
Table2 | Column1
... | ...
Table652 | Column873
I then try to use that table as input for another query where I attempted to use either:
WHERE Tablename.ColumnName LIKE 'Post 2013 - %'
Or
WHERE ((TableName).(ColumnName)) LIKE 'Post 2013 - %'
Or
SELECT ...
CONCAT(TableName, '.', ColumnName) AS TabCol
FROM search
WHERE TabCol LIKE 'Post 2013 - %'
The problem is that my code is returning errors at each of these WHERE statements. Is it possible for me to use the first output as a way to scan through those tables for a specific value, returning the TableName and ColumnName where it found the value?

To do something like that there are generally two methods that I have found that work. One is to extract the entire database to text file and use a text searching tool for the data you seek.
The other is to use the system tables to identify all the tabels / fields. Then have some code loop through those fields running your query. You may want to check the data type first to eliminate things like date, numeric or integer data types. Or you could do this manually. But there is not really as standard Sql way to do this.

If your using SQL Server as Database :- You can use dynamic queries.
you can write a stored procedure or Function which has logic similar to that in the screenshot which return table data which the desired results. And you can pass Tablename , Columnname, Conditions as the parameters . Assuming you have all the data in the table.
Here is a sample query i tried with above scenario.
select * from search;
select * from table2;
Declare #sql varchar(max)
select #sql='select * from '+ tablename+' where '+columname+'='+'''john-her'''
from search where tablename='table2' and columname='namedata2'
select #sql
exec(#sql)
I just tried with sample ...there may be multiple scenario with conditions. Hope this helps you in giving you an idea how to solve your problem!!

Related

How to get the data in SQL Server for string concat value compare to int value without using like operator

I have table with data like this:
Id | StringValue
----+-------------
1 | 4,50
2 | 90,40
I will get input StringValue like 4. I need to fetch the data exact matched record. When I am using LIKE operator, select query is returning two rows, but I need exact matched data record only.
Can anybody please help me with this?
SELECT *
FROM Table1
WHERE StringValue like '%4%'
But that returns two rows - both ID 1 and 2.
My expectation is I need to get ID = 1 row only
Storing delimited data like this is a well documented anti-pattern, violates basic normalisation principles and prevents the database engine from fully utilising an index.
What you can do is delimit your search value and also ensure the expression to search is correctly delimited; this is an unsargable expression however and the strorage engine will have to scan all rows every time -
declare #valueToFind varchar(10) = '4';
select *
from t
where Concat(',', t.StringValue, ',') like Concat('%,' #valueToFind, ',%');
for SQL Server 2016 and later you can use STRING_SPLIT or earlier version of SQL Server, there are many alternative, just do a search for it.
Or, you can simply do
SELECT * FROM Table1 where ',' + StringValue + ',' like '%,4,%'

How to use "Includes" functionality in SQL?

I require following functionality, How can I frame it in a query?
Select *
from table
where column 4 includes column3
Example:
Please find attachment for table screenshot.
.
I want to extract all the rows where column 4 includes column 3.
Select * from table where column4=column3
Use a LIKE statement with the "value" from column3
select *
from the_table
where column4 like '%'||column3||'%'
|| is the ANSI SQL operator for string concatenation, but not all DBMS respect that.
I would probably use a string find function or something along those lines, and as I saw this being tagged with Mysql, you should be able to use LOCATE
SELECT
*
FROM
table
WHERE
LOCATE(column3, column4) > 0

Mysql Query data filled check

I had created the table with 200 columns and i had inserted data
Now i need to check that specific 100 columns in one row are filled or not,how can we check this using mysql query .the primary key is defined .please help me out how to resolve this.
select * from tablename where column1 != null or column2 != null ......
That is a lot of columns so at the risk of being mysql server version specific you can use the information schema to get the column names and then write a SQL procedure or something in your chosen shell / language that iterates over them performing a test.
select distinct COLUMN_NAME as 'Field', IS_NULLABLE from information_schema.columns where TABLE_SCHEMA="YourDatabase" and TABLE_NAME="YourTableName" and TABLE_NAME not like "%view%" escape '!' ;
The example above will tell you the column name as "Field" and tell you if it can hold a NULL. Having the field name may give you a better way of automating a field name specific test.

Pattern match within the IN operator (MySQL)

I would like to match patterns inside an IN expression like this:
... WHERE ... IN ('%alma%','%sajt%')
Is it possible somehow and if not, how could I achieve this some other way?
You would need to either use multiple LIKE statements
(columnName LIKE '%alma%' or columnName LIKE '%sajt%' or columnName LIKE '%etc%')
Or look into Full Text Search: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
It's not possible. IN is designed to search in potentially large sets, not to make pattern searches.
You should use multiple LIKE conditions separated by OR operators.
Other posters have showed you how to use multiple LIKE clauses combined with OR to get this effect. Here are two other thoughts:
If you find yourself searching inside text fields it is often an indication that you are storing too much information in those fields. If possible, consider breaking out those values into separate fields and / or tables to make searching and validation easier.
Plain SQL does not excel at searching inside text fields. If the text is large, you have more than a relatively small number of records, and performance is important for this query, consider adding full text indexing to your database.
As far as I'm aware this is not allowed. You'd have to use multiple LIKE expressions, as in
SELECT *
FROM SOME_TABLE
WHERE SOME_VALUE LIKE '%alma%' OR
SOME_VALUE LIKE '%sajt%';
Share and enjoy.
If you're doing something like me, it might be helpfull to insert the patterns in a secondary table.
Then you may use 'like' with a 'join':
create table patterns ( p varchar(50));
insert into patterns values ( 'to%'), ('lo%');
create table table1 ( txt varchar(50));
insert into table1 values ('toto'),('titi'),('tom'),('louis');
select txt from table1
inner join patterns on
txt like p;
| toto |
| tom |
| louis |
This works for me but oddly enough it is much slower than several calls (on my 5.5 system at least):
select txt from table1 where txt like 'to%';
select txt from table1 where txt like 'lo%';
...

SQL set column names from another query

I'm am using MS Reporting Services to graph some data from an Oracle database. I want to name the columns in my select statement with values from another select statement. Is this possible?
Like instead of
Select Column1 As 'Test' From Table1
could I do something like
Select Column1 As (Select column2 from Table2 where Value = 1) From Table1
?
I would think you'd have to query out separately, then form the query dynamically. Interested to see if there is a different answer.
My PL/SQL's a little rusty, so what follows is more pseudocode than compilable & tested code. And this is completely off the top of my head. But if you know the specific ordinal location of the column in the table, you may try this:
columnName varchar2(50) :=
Select column_name
From all_tab_columns c
Where lower(table_name) = '<% Your Table2 Name %>' And
column_id = 9 -- The appropriate ordinal
Order By column_id;
Select Column1 As columnName From Table1;
There may be more column values drawn from "all_tab_columns" that'll help you as well. Take a look around and see.
I hope this helps.
You can query all needed column names into separate report dataset, create hidden multivalue report parameter vColumns, set dataset with columns as a parameter default values, and use it as a string array:
Parameters!vColumns(0).Value - will be the first column etc. So you can use them as a query parameters.
See Lesson 4: Adding a Multivalue Parameter