sql server full text search not returning correct result - sql

I have full text catalogue defined on below table on all_metadata row:
ft_id record_id am_changestamp all_metadata
21 42 2012-09-11 17:07:25.553 Photos Project 1234 5678 |Mockups|pictures|Abbot| testing Revenue migration testing Getty Images Abbot g test.txt
22 43 2012-09-11 17:11:06.147 Photos Project 1234 5678 |Mockups|pictures|Abbot| testing Revenue migration testing Getty Images Abbot g test1.txt
The SQL I am running is:
select f0.record_id from ft_all_metadata as f0,
containstable(ft_all_metadata, all_metadata, N'Images') as kt where f0.ft_id = kt.[key]
The SQL returns the first row but not the second!!!! I am really baffled!
I have tried re-creating and re-building the full-text catalogue with no luck.
Your help is appreciated,
Bruce

I had relied on SQL Server Manager GUI to do this and must have done something incorrectly.
So decided to do this through commands and this works now:
exec sp_fulltext_catalog 'Catalogu_Name', 'create'
exec sp_fulltext_table 'table_name', 'create', 'Catalogu_Name', 'table_index_name'
exec sp_fulltext_column 'table_name', 'column_name', 'add'
exec sp_fulltext_table 'table_name', 'activate'
exec sp_fulltext_catalog 'Catalogu_Name', 'start_full'
I created a separate index for table_index_name on the primary key.

Related

How to replace column names with a value of another table

I have table A like below:
ID QuestionCode Question
1 Q1 What is your name
2 Q2 How old are you
3 Q3 Where are you from
Also, I have table B like below:
UserId Q1 Q2 Q3
1 Ahmad 22 Egypt
2 John 31 USA
3 Daniel 32 Mexico
What I want is to create a view using the question itself instead of the its code in the column name like:
UserId What Is Your Name How old are you Where are you from
1 Ahmad 22 Egypt
2 John 31 USA
3 Daniel 32 Mexico
What is the best way to do it? Any help would be appreciated.
THanks.
Updated ... Missed the 2016 tag
SQL Server is declarative by design and does not support macro substitution.
This leaves Dynamic SQL.
Here is a working example of the Dynamic SQL
Declare #tsql nvarchar(max) = N'Select top 1 * from Answers
Select #tsql = 'Select '+stuff((Select ',' +expr From (
select expr=concat(coalesce(quotename(Question),Name),'=',Name)
From sys.dm_exec_describe_first_result_set(#tsql,null,null ) A
Left Join Questions B on B.QuestionCode=A.name
) src For XML Path ('')),1,1,'')+' From Answers'
Exec(#tsql)
Results
The pivot you're describing isn't easy to do in straight SQL; you could write some dynamic SQL to create column names and subqueries for the second table based on the first, but to do this but can be rather fiddly and is certainly an easy way to break things if you get it wrong.
There's an alternative approach highlighted at https://www.red-gate.com/simple-talk/sql/t-sql-programming/easier-way-pivoting-data-sql-server/; whether that's simpler than the dynamic SQL option very much depends on your experience level.

PL/SQL-> inserting 500 users to a table from excel

We have a Oracle table which consists of following columns: user,code,date,number,origin.eg:
|user, code, date, number, origin|
|--------------------------------|
|stah, LK, 10-1, 20091, WEST |
|hats, LJ, 12-2, 30001, ESTA |
This table already has 1000 users. Now we want to add another 500 new users to this table and also grant all those 500 users same code->{'LD','LM','MK',CK'}.i.e each user should have same code (And date,number,origin can be null).
I have 500 users in excel. Can someone help me to figure out how to insert these into a table?
Build your script manually in excel by using formulars:
user code date number origin skript
stah LK 10-1 20091 West =CONCAT("INSERT INTO mytable values(""";A2;"""";""";B2;"""";""";C2;"""";""";D2;"""";""";E2;"""";");")
hats LJ 12-2 30001 ESTA INSERT INTO mytable values("hats"";B2;"";"12-2"";D2;"";"ESTA");
First row shows formular - second shows the result..
You could use SQL Developer to import data from spreadsheet into table. Look here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r30/SQLdev3.0_Import_Export/sqldev3.0_import_export.htm#t3
You can use free data migration tool PDI which is very helpful and
manageable
You can schedule it by using windows or linux scheduler
here is a tutorial link how it migrate excel to oracle
https://www.youtube.com/watch?v=RDxdy_8mOa0

Updating a database column based on its similarity to another database column

I have a database table (Customers) with the following columns:
ID
FIRST_NAME
MIDDLE_INIT
LAST_NAME
FULL_NAME
I also have a database table (ENG) with the following columns:
ID
ENG_NAME
I want to replace all of the ENG.ENG_NAME entries with a FULL_NAME entry from the CUSTOMERS table
Here is the problem.
The ENG_NAME was hand-jammed through a web form and, so, has no consistency. For instance, one row might contain "Robin Hood". Another "Hood, Robin L". An another "Robin L Hood".
I want to search the entries in the CUSTOMERS table, find a close match, then replace the ENG.ENG_NAME with the CUSTOMERS.FULL_NAME.
Example:
ENG table CUSTOMERS table
ID ENG_NAME ID FULL_NAME FIRST_NAME MIDDLE_INIT LAST_NAME
================ ==================================================================
1 Hood,Robin 1 Robin L Hood Robin L Hood
2 Rob Hood 2 Maid M Marion Maid M Marion
3 Marion M 3 Friar F Tuck Friar F Tuck
4 Rob Garza 4 Robert A Garza Robert A Garza
Based on the data above, I would want ENG_NAME columns to be replaced like this:
ENG table
ID ENG_NAME
====================
1 Robin L Hood
2 Robin L Hood
3 Maid M Marion
4 Robert A Garza
Any thoughts on how to do this?
Thanks
This is not going to be a simple task, I would start at finding a good C# (or any .NET) algorithm that detects similar strings portions.
Then look at Compiling C# Code into SQL Stored Procedures and Invoke that code using SQL Server. This CLR Code can then write the results to a table for you to analyze and do whatever you want with it.
For More: CLR SQL Server User-Defined Function
I would do it in .NET using Levenshtein distance.
Start at 1 and you are going to have some ties and you need to decide
Then move to 2,3,4...
You could do in a CLR but how are you going to deal with ties? And you are going to have ties. How are you going to decide when it is not a match at all?
And I would put it in new column so you have a history of original data
Or a FK reference to customers table

Self join/update of table with data in same table

Due to combining old and new system data in a table I have a list of data like this:
Work no Work name
========= =========
123456 James
123456 James, (123456)
And I want to update to:
Work_no Work_name
========= =========
123456 James
123456 James
I tried building an update statement, wasn't too confident in it so ran it as an equivalent select statement to see what returned and it seems to be running in an infinite loop (there's about 200k records and when I stopped it it was at somewhere in 2 Million returned!) although what it was returning at the start looked fine it just seemed to be duplicating or something:
UPDATE c1
set c1.Work_name = c.Work_name
FROM table c1
INNER JOIN table c ON c1.Work_no = c.Work_no
where charindex(',',c1.Work_name) > 0
Only got experience doing the simplest update statements - a bit stuck with this one if anyone could suggest what I am doing wrong and best way to rectify it?
Thanks
Andrew
Do you have an index built on work_no?
Did you try:
UPDATE #table set Work_name = (select top 1 c.Work_name FROM #table c where #table.Work_no = c.Work_no and charindex(',',c.Work_name) = 0) where charindex(',',c.Work_name) > 0

Rails console - output queries results table

On rails console (just for the record, rails 3.2), how can I make it output to STDOUT my queries results table (ie: the table containing the columns names and values)?
User Load (6.5ms) SELECT `users`.* FROM `users`
---------------------------
Id | Name | Address | Phone
---------------------------
1 | Sam | ZZZ 10 | 55555
---------------------------
2 | xxxx | xxxxxxx | xxxxx
Tks!
ps: Note that query.explain is NOT what i need.
I wrote a gem to do exactly this! http://tableprintgem.com
The most powerful feature of table_print is the ability to see your data in the context of other objects it relates to. You can reference nested objects with the method chain required to reach them. This example is showing data from three different tables:
name from the Author table (reached through author.name)
title from the Book table (reached through author.books.title)
caption from the Photo table (reached through author.books.photos.caption)
There's a short intro screencast at http://tableprintgem.com