3

I was hoping that within the WebMatrix (C#.net) environment (using SQL Server Compact) that there was a way to search all tables and fields for a value. I have a bunch (like 100) of tables, connected to via WebMatrix and I am trying to look for a table that holds some information I need.

I have been here: http://blogs.thesitedoctor.co.uk/tim/2007/11/02/How+To+Search+Every+Table+And+Field+In+A+SQL+Server+Database.aspx

And here, on stackoverflow: search all tables, all columns for a specific value SQL Server

As well as here: How do I find a value anywhere in a SQL Server Database?

Unfortunately I am not seeing how to implement these methods in my current environment, but I realize there may not really be a way to do what I am asking.

Whether there is or there isn't a way to do what I'm asking, I would at least like to know, so I can look for another method.

Thanks!

-----------------------------SQL SubQuery That Works In SQL CE-----------------------------

SELECT * FROM UserProfile JOIN webpages_UsersInRoles ON UserProfile.UserID = webpages_UsersInRoles.UserId WHERE (RoleId <> 6) AND Email NOT IN (SELECT Email FROM UserProfile JOIN webpages_UsersInRoles ON UserProfile.UserID = webpages_UsersInRoles.UserId WHERE RoleId = 6) ORDER BY Email

Community
  • 1
  • 1
VoidKing
  • 6,282
  • 9
  • 49
  • 81
  • What version of SQL Server CE are you using? – Cameron Tinker Mar 04 '13 at 20:26
  • How are you "trying to look for a table"? What are you "not seeing"? Every one of those links seem to do what you asked; have you tried them? What happened? SQL CE itself should be able to do what you want, why must you go through WebMatrix? – Dour High Arch Mar 04 '13 at 20:31
  • @CameronTinker I hope this answers your question, but I am using WebMatrix 2, if that helps. – VoidKing Mar 04 '13 at 20:31
  • I always use WebMatrix - For Web Apps, that is – VoidKing Mar 04 '13 at 20:32
  • @CameronTinker I haven't really tried because I don't know how to query them. Can you use a wildcard in the FROM clause? – VoidKing Mar 04 '13 at 20:33
  • The only reason I ask about your SQL CE version is that there are new functions introduced in SQL CE 4.0 that are not available in SQL CE 3.5. – Cameron Tinker Mar 04 '13 at 20:43
  • @CameronTinker Let me see if I can find out. One sec... – VoidKing Mar 04 '13 at 20:45
  • @CameronTinker It is SQL CE 4.0 – VoidKing Mar 04 '13 at 20:45
  • I am looking into a solution for querying without needing to create tables. This will be less efficient however. – Cameron Tinker Mar 04 '13 at 21:01
  • @CameronTinker That is fine, performance will not be an issue. I won't have to do it very often at all. – VoidKing Mar 04 '13 at 21:02
  • Do you have access to the database file itself? It seems that SQL CE is very limited in what can be done in querying. Perhaps if you have access to the database file itself, you can write a small app to iterate through the result sets. – Cameron Tinker Mar 04 '13 at 22:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25562/discussion-between-cameron-tinker-and-voidking) – Cameron Tinker Mar 04 '13 at 22:44

2 Answers2

4

I've adapted the SQL from Syn123's answer and here is what I have so far:

SELECT c.TABLE_NAME, c.COLUMN_NAME
FROM   INFORMATION_SCHEMA.COLUMNS AS c 
       INNER JOIN INFORMATION_SCHEMA.Tables AS t ON t.TABLE_NAME = c.TABLE_NAME
WHERE  (c.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext')) AND (t.TABLE_TYPE = 'TABLE')

The problem I'm having is that I can't do subqueries with SQL CE and thus can't select from a result set. If you have access to your .MDF database file, you can write a small console app to search for a particular keyword using the set returned by the above SQL. You need a way to create dynamic SQL and SQL CE doesn't support EXEC so this is difficult and most likely not possible by itself.

EDIT:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLCESearch
{
    class Program
    {
        static void Main(string[] args)
        {
            SearchText("Nancy");
            Console.ReadKey();
        }

        private static void SearchText(string searchText)
        {
            string connStr = "Data Source=Northwind40.sdf;Persist Security Info=False;";
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT c.TABLE_NAME, c.COLUMN_NAME ";
                sql += "FROM   INFORMATION_SCHEMA.COLUMNS AS c ";
                sql += "INNER JOIN INFORMATION_SCHEMA.Tables AS t ON t.TABLE_NAME = c.TABLE_NAME ";
                sql += "WHERE  (c.DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'text', 'ntext')) AND (t.TABLE_TYPE = 'TABLE') ";

                SqlCeDataAdapter da = new SqlCeDataAdapter(sql, connStr);
                da.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                    string dynSQL = "SELECT [" + dr["COLUMN_NAME"] + "]";
                    dynSQL += " FROM [" + dr["TABLE_NAME"] + "]";
                    dynSQL += " WHERE [" + dr["COLUMN_NAME"] + "] LIKE '%" + searchText + "%'";

                    DataTable result = new DataTable();
                    da = new SqlCeDataAdapter(dynSQL, connStr);
                    da.Fill(result);
                    foreach (DataRow r in result.Rows)
                    {
                        Console.WriteLine("Table Name: " + dr["TABLE_NAME"]);
                        Console.WriteLine("Column Name: " + dr["COLUMN_NAME"]);
                        Console.WriteLine("Value: " + r[0]);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }


        }
    }
}

There's a really quick and dirty console application that will print out any table/column that contains a value of text. You can adapt it to fit your needs. I hope this helps.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • I will update my post with an example I KNOW works that is used in a web application on a regular basis. – VoidKing Mar 04 '13 at 22:55
  • I'm interested to see how you achieved subqueries with SQL CE :). I've seen some solutions use joins, but that's really inefficient for a dataset as large as yours. – Cameron Tinker Mar 04 '13 at 22:58
  • Okay, it's posted, but maybe I am oversimplifying what you mean by subQuery, because this is an example from SQL Server (non CE) I didn't even modify it for CE. Anyway, it is 5 where I am and I have to go, but I will return to this post first thing tomorrow morning (I get to work at 8 (CST)) Thanks again, see you tomorrow! – VoidKing Mar 04 '13 at 23:00
  • No problem. Glad to help as I can. – Cameron Tinker Mar 04 '13 at 23:01
  • I've updated my answer to include an example console application that will spit out table\column info for any found text. I hope that helps. – Cameron Tinker Mar 04 '13 at 23:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25571/discussion-between-cameron-tinker-and-voidking) – Cameron Tinker Mar 05 '13 at 03:05
0

I'm not sure about CE, but I use this method:

http://www.dbforums.com/microsoft-sql-server/972792-find-text-string-database.html

namely the third one down. Read and understand the syntax as you use it, it's highly modifiable and can filter by data type.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • Thank you for your example, and please forgive this question if it is a dumb one, but I am familiar with sql querying (SELECT * FROM ... WHERE ...) and such. In the example in the link you posted, is that just a sql query? If so, it has some keywords in it that I have never seen before. If not, what language is it? – VoidKing Mar 04 '13 at 20:31
  • It's a MS SQL query that uses a cursor to loop through SQL system tables, I'm sure it can be made more legible. The big question with my answer is will it work in CE, let me know if it doesn't. – RandomUs1r Mar 04 '13 at 20:46
  • I would love to try this method, but I'm not sure I understand enough about the syntax to modify it for use with my database. Certainly it can't be as simple as a copy/paste, can it? – VoidKing Mar 04 '13 at 20:49
  • Also, another error that I see is that I will not be allowed to Create_Table because we have read-only access from this end. – VoidKing Mar 04 '13 at 20:53
  • you shouldddd be able to create temp tables, so follow http://www.sqlteam.com/article/temporary-tables and add a # in front of the query. It's as simple as copy paste and then put your value into the Sargable, so the line SELECT Sargable = 'Beer' is the one you'd replace the string on with your search term – RandomUs1r Mar 04 '13 at 21:06
  • I tried what you said, but I get another error: Invalid object name 'myTable99'. After putting the "#" in front of "myTable99" – VoidKing Mar 04 '13 at 21:14
  • Declare statements are not supported by SQL CE. Also, temp tables don't exist in SQL CE. – Cameron Tinker Mar 04 '13 at 21:15
  • @Syn123 Well, I guess that means I'm probably out of options :( Thank you so much for trying to help. At least I know now. – VoidKing Mar 04 '13 at 21:18
  • Don't give up yet. I've almost figured something out. – Cameron Tinker Mar 04 '13 at 21:19
  • @CameronTinker Okay, I won't, Thank you so much for helping! If it helps any further, this is not something I even need a 'coded' solution for inherently. The thing is I think I am pulling data from the wrong table somewhere, and need to check to see if another table has these values (But there are hundreds! probably close to if not exceeding a thousand, or maybe even more, I stopped counting at 200 in the "b's"). I really only need to find this table once. No one else seems to know anything about its contents, because it is 3rd party software. – VoidKing Mar 04 '13 at 21:22
  • @CameronTinker Just realized that was you up there that told me that SQL CE doesn't support declare statements or temp tables. Sry about that. – VoidKing Mar 04 '13 at 21:28
  • That's unfortunate, well if you ever need the solution for MS SQL, it'll be here. It does sound like Cameron is aware of the version specific nuances of CE, what the guy in my link is going is creating a base table of all the tables in the database and then iterating through them with a like clause on the value you're seeking returning all positive matches. – RandomUs1r Mar 04 '13 at 21:31
  • No problem. I will have an answer for you shortly. – Cameron Tinker Mar 04 '13 at 21:32
  • @CameronTinker Great! If you come up with a solution, don't forget to post as an answer so I can accept :) – VoidKing Mar 04 '13 at 22:11