I have a table datatable with following data
No Data
1 access author basic chapter data database information
2 author basic black data chapter creating overview project scope using code
3 basic data edition author special any assumed been book contained corporation damages
I am trying to write a query which does pattern matching with the help of LIKE clause
this is my query
rs=s.executeQuery("SELECT No from datatable where Data LIKE '%author basic data%');
which is supposed to retrieve all the records that is No 1, 2 and 3 but it returns nothing however
if i execute this query
rs=s.executeQuery("SELECT No from datatable where Data LIKE '%author basic%');
which gives output as No 1 and 2 but not No 3
so how can i write such a query which gives me the desirable output
thanks in advance
You query checks if there is a full substring like %author basic data%. Try this :
SELECT
No
FROM
datatable
WHERE
Data LIKE '%author%' AND
Data LIKE '%basic%' AND
Data LIKE '%data%'
Looks like you have something like
CREATE TABLE datatable('No' INT, 'Data' VARCHAR(...))
by grouping a lot of attributes together in a single column (VARCHAR, CLOB, BLOB...) you cannot harness the power of SQL.
If you rather do something like
CREATE TABLE <meaningful name>('No' INT, AUTHOR VARCHAR(...), TITLE VARCHAR, ...)
you avoid using LIKE for all your queries and you can, as a commenter said, create indices on the columns most often used to access the data to speed up access.
Other benefits include using aggregating functions on numerical data and joining with other tables.
Related
I am dealing with a specific problem of identifying the dependent db objects for any SSRS RDL.
I have a good understanding of if any dataset have stored procedure as the query in a RDL then I can reference the associated stored procedure and get all the dependent objects (details can be found here: Different Ways to Find SQL Server Object Dependencies)
But I am looking specifically for the datasets with text query or inline query for any rdl. I am able to extract the CommandText from the XML of the rdl but I am not sure how to extract db objects like sp, table, views columns form a command text which is inline query in the rdl.
For example if I extract below query from XML commandText (this is a hypothetical query, names are not standardized in the database like vw_ for views , udf_ for functions):
-----This query serves Report ABC
SELECT DATE
,[amount]
,teamID = (SELECT TeamID FROM Sales.[getSalesPerson](r.date) as s WHERE R.[SalesPersonName] = S.[SalesPersonName])
,[channel]
,[product]
,[Item]
,r.[M_ID]
,Amount
,M.[Type]
FROM dbo.FactTable AS R
LEFT JOIN sp_Channel C ON R.[Channel_ID] = C.[Channel_ID]
LEFT JOIN Marketing.vw_M M ON R.[M_ID] = M.[M_ID]
Is there a way to identify that this query have dependent object as below:
ObjectName ObjectType
------------------------------------------
dbo.FactTable Table
sp_Channel Stored Procedure
Marketing.vw_M View
Sales.[getSalesPerson] Function
It is not easy to extract object names from an SQL command since they may be written in different ways (with/without schemas, databases name included ...)
But there are many option to extract objects from an SQL query that you can try:
Using Regular expressions, As example: You have to search for the words located after the following keywords:
TRUNCATE TABLE
FROM
UPDATE
JOIN
The following code is a C# example:
Regex regex = new Regex(#"\bJOIN\s+(?<Retrieve>[a-zA-Z\._\d\[\]]+)\b|\bFROM\s+(?<Retrieve>[a-zA-Z\._\d\[\]]+)\b|\bUPDATE\s+(?<Update>[a-zA-Z\._\d]+)\b|\bINSERT\s+(?:\bINTO\b)?\s+(?<Insert>[a-zA-Z\._\d]+)\b|\bTRUNCATE\s+TABLE\s+(?<Delete>[a-zA-Z\._\d]+)\b|\bDELETE\s+(?:\bFROM\b)?\s+(?<Delete>[a-zA-Z\._\d]+)\b");
var obj = regex.Matches(sql);
foreach(Match m in obj)
{
Console.WriteLine(m.ToString().Substring(m.ToString().IndexOf(" ")).Trim());
}
Output
Then you have to clean and join the result with the sys.objects tables from the SQL Server database.
Using a SQL parser, as example:
SQL Parser
SQL Parser - Code Project
You can refer to the following very helpful links for additional information:
Regular expression to find all table names in a query
Parsing SQL code in C#
If your reports are connecting to SQLServer and you have access you could try to get the execution plan with SET SHOWPLAN_XML ON and parse it.
Relevant thread for the parsing:extracting-data-from-sql-servers-xml-execution-plan
I want to create an interface between 2 databases on SQL Server 2008+ to copy several similar named tables into one.
I have n tables that all have the same naming convention, for example:
SalesInvoicePlanning2014ver1
SalesInvoicePlanning2015ver1
SalesInvoicePlanning2015ver2
etc.
The numbers can vary and do not have a set start (or end), but are always of the "int"-Datatype.
I also have a table "tabledir" that contains all table names as list. (one field) There are a total of 30-40 entries in that list with (for me) undesired entries. In the above example I would need 3 of the 30 tables.
The plan is to use a loop container to
select Top 1([name]) from [tabledir] where name like 'SalesinvoicePlanning%'
and then use the result as variable in the following SSIS Data transfer task:
Select * from [variable]
However, I'm stuck with the SQL statement to give me the desired tablename on each iteration.
Performance is not really an issue. Any advice? Am I wrong trying to use a loop-container?
You can follow below steps -
Step 1 - You can first create SQL task to get all table names into one variable lets say, TableNames of type Object(recordset) using you query.
e.g. select ([name]) as TableName from [tabledir] where name like 'SalesinvoicePlanning%'
Step 2 - Add foreach loop container to iterate over this variable TableNames to take single table name into new variable current_table and add data flow into the container to import data to destination table. Your source query will be expression like -
Select column_names from current_table
I've been trying to figure this problem out for a while now. Hope anyone can help here please.
I have an semicolon separated CategoryIDs in my Article table (e.g. ;2;34;5;9;), and I have a Category table with the old CategoryID column and the new CategoryID column. Now I'd like to update the CategoryIDs in the Article table with some new CategoryID based on the original CategoryIDs. e.g. (e.g. ;2;34;5;9;) will be updated to (e.g. ;564;344;753;944;)
The reason that I need this is because I'm merging tables from two databases with the same tables set ups.
I know this is a bad design, but it's been like this for ages. I can't really think of a solution for this, any help would be really appreciated.
Yes it is a bad design.
I suppose you use MySQL. I've used GROUP_CONCAT MySQL featured function but you can emulate it in MSSQL also. And I suppose you need to replace ALL old Id's with new.
SQLFiddle demo
Tables:
create table article(id int, CategoryID varchar(200));
create table Category(OldCategoryId int, newCategoryId int);
and update to replace groups:
update article,
(select
article.id,
CONCAT(';',
GROUP_CONCAT(cast(newCategoryID as char) separator ';'),
';') newGroups
from category
left join article on CategoryId like CONCAT('%;',CAST(oldCategoryId as char),';%')
group by article.id) t1
SET article.CategoryId=t1.newGroups
where article.id=t1.id
For such matters MySQL provides the function find_in_set. However SQL Server (if that's what you are working with) does not directly support an IN predicate for CSV, neither it provides find_in_set. It needs a rowset for IN predicate to work...
Check on the above link that CTE examples.
You need a script / program that construct old & new CategoryID mapping, and go through your Article table and replace it by looking up the map.
Maybe procedural program might do (if your DBMS supports procedural like stored proc / PLSQL), otherwise maybe it's easier to do this using Java / C# / Php
I have two tables in a PostgreSQL database.
The first table contains an ID and a text field with up to 200 characters and the second table contains a data definition table which has a column that contains smileys or acronyms and a second column which converts them to plain readable English.
The number of records in table 1 is about 1200 and the number in table two is about 300.
I wish to write a SQL statement which will convert any text speak in column 1 in table one into normal readable language based on the definitions in Table 2.
So for example if the value in table 1 reads as: Finally Finished :)
The transformed SQL would be something like: Finally Finished Smiles or smiling,
where the definition is pulled from the second table.
Note the smiley could be anywhere in the text in column one and could one of three hundred characters.
Does anyone know if this is possible?
Yes. Do you want to do it entirely in SQL, or are you writing a brief bit of code to do this? I'm not entirely sure of how to do it all in SQL but I would consider something like what is below:
SELECT row.textToTranslate FROM Table_1
oldText = row.textToTranslate
Split row.textToTranslate by some delimeter
For each word in row.textToTranslate:
queryResult = SELECT FROM Table_2 WHERE pretranslate=word
if(queryResult!=Null)
modifiedText = textToTranslate.replace(word, queryResult)
UPDATE Table_1 SET translatedText=modifiedText WHERE textToTranslate=oldText
I am using this vb.net code file to call a procedure to get dates (among other things). When I execute this procedure in SQL 2005 Server Management Studio, I get about 10 columns. However when I execute this code, the dataset seems to only have one index value, maybe I am misunderstanding something. When I change this
ds.Tables(0).Rows.Count.ToString
to
ds.Tables(1).Rows.Count.ToString <---changing the index value to 1
(or higher)
I get no information. So my question is;
Does the DataSet contain the other columns, only not indexed like an array? What I want to do is choose which of the columns to filter out and turn the rest into JSON formatted strings. Any suggestions on how to accomplish that would be greatly appreciated as well
You're talking about "columns" of data, but your code snippet is dealing with Tables of data (resultsets).
i.e.
if your sproc does something like this:
SELECT Column1, Column2, Column3
FROM YourTable
WHERE ID = 123
then you'll have 1 DataTable in your dataset as 1 resultset is returned: ds.Tables(0). This will contain 3 columns: ds.Tables(0).Columns(n) where n is 0 to 2.
When you do ds.Tables(i).Rows.Count, you're going through the rows that were returned. What exactly are you trying to do?
For the latter part of your question, JSON, have a look at
Microsoft JavascriptSerializer
Its then simply a case of Serialize(ds.Tables(0)) to get yourself a JSON representation
edit
You can also try this:
ds.Tables(0).Rows(0).Items.Count.ToString
and
ds.Tables(0).Rows(0).Items(0).ToString
prior
You are checking the rows not the columns. What does
ds.Tables(0).Columns.Count.ToString
return?