Home All Groups Group Topic Archive Search About

NEED HELP IN MS SQL SERVER 2005!!!

Author
3 Apr 2006 6:18 AM
SQL noob
I want to link 2 database together, that is database 1 "OMIM" and database 2
"Human" and i want to search the key words from this 2 database... but i
can't find it in the book.. can someone help mi out? is urgent.. thx a
million..

Author
3 Apr 2006 6:29 AM
Omnibuzz
what do you mean by keywords.. and are these databases in the same server.
Are all your drivers up to date? click for free checkup

Author
3 Apr 2006 7:24 AM
SQL noob
"Omnibuzz" wrote:

> what do you mean by keywords.. and are these databases in the same server.

i mean search by a word that related to the Omim_No.. the database is in the
same server.. i put everything into one database.. just want to search the
words from the database in the tables.. search in every tables..

pls help mi .. thx..
Author
3 Apr 2006 7:32 AM
Omnibuzz
if you are looking for a script that can search through all columns of all
tables for a word then to my knowledge the only way to do it is write the
query for all the tables.
By the way is it a one time operation or are you looking for an SP?
Author
3 Apr 2006 8:00 AM
SQL noob
"Omnibuzz" wrote:

> if you are looking for a script that can search through all columns of all
> tables for a word then to my knowledge the only way to do it is write the
> query for all the tables.
> By the way is it a one time operation or are you looking for an SP?

what is SP?
Author
3 Apr 2006 8:27 AM
Will
SP stands for stored procedure. I think Omnibuzz was asking whether or
not you needed to just get an answer out of your database just for a
particular case, or if you need to be able to use this several times
for different search criteria (e.g. searching on "HEART" and "LUNGS"
etc.).

As for searching all columns, there are 2 stored procedures sp_tables
and sp_columns. these will allow you to list all the tables in a
database and all the columns in a table. you could use this along with
the stored procedure sp_executesql to call some dynamic sql. However
this is getting into some more advanced sql than you seem to be
wanting.

Cheers
Will
Author
3 Apr 2006 8:41 AM
SQL noob
Show quote Hide quote
"Will" wrote:

> SP stands for stored procedure. I think Omnibuzz was asking whether or
> not you needed to just get an answer out of your database just for a
> particular case, or if you need to be able to use this several times
> for different search criteria (e.g. searching on "HEART" and "LUNGS"
> etc.).
>
> As for searching all columns, there are 2 stored procedures sp_tables
> and sp_columns. these will allow you to list all the tables in a
> database and all the columns in a table. you could use this along with
> the stored procedure sp_executesql to call some dynamic sql. However
> this is getting into some more advanced sql than you seem to be
> wanting.
>
> Cheers
> Will
>
> can u teach mi how to do it?
Author
3 Apr 2006 10:33 AM
Will
Here's some SQL that I'd use to dynamically search all text data in a
database. Like I said, I think you'll find it too complicated, but
perhaps it will give you a starting point:

DECLARE @ValueToFind varchar(100)
SET @ValueToFind = 'LIVER'




CREATE TABLE #tables
(
    TABLE_QUALIFIER    sysname,
    TABLE_OWNER    sysname,
    TABLE_NAME    sysname,
    TABLE_TYPE    varchar(32),
    REMARKS    varchar(254)
)

CREATE TABLE #Columns
(
    TABLE_QUALIFIER    sysname,
    TABLE_OWNER    sysname,
    TABLE_NAME    sysname,
    COLUMN_NAME    sysname,
    [DATA_TYPE]    smallint,
    TYPE_NAME    sysname,
    [PRECISION]    int,
    LENGTH    int,
    SCALE    smallint,
    RADIX    smallint,
    NULLABLE    smallint,
    REMARKS    varchar(254),
    COLUMN_DEF    nvarchar(4000),
    SQL_DATA_TYPE    smallint,
    SQL_DATETIME_SUB    smallint,
    CHAR_OCTET_LENGTH    int,
    ORDINAL_POSITION    int,
    IS_NULLABLE    varchar(254),
    SS_DATA_TYPE    tinyint

)

INSERT INTO #tables
exec sp_tables


DECLARE tables cursor
FOR
SELECT Table_name FROM #tables

OPEN tables

DECLARE @Tablename sysname

FETCH NEXT FROM tables INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO #Columns
    exec sp_columns @TableName

    DECLARE @SQL nvarchar(4000)
    SET @SQL = 'SELECT * FROM ' + @Tablename + ' WHERE 1=2 '

    DECLARE cols CURSOR
    FOR SELECT Column_name FROM #Columns WHERE TYPE_NAME LIKE '%char' or
type_name LIKE '%text'



    DECLARE @ColName sysname
    OPEN cols
    FETCH NEXT FROM Cols INTO @ColName

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @SQL = @SQL + ' OR [' + @ColName + ']  LIKE ''%' + @ValueToFind +
'%'' '
        FETCH NEXT FROM Cols INTO @ColName
    END


    CLOSE Cols
    DEALLOcate cols
    TRUNCATE TABLE #Columns
    FETCH NEXT FROM tables INTO @tablename

    exec sp_executesql @SQL
END

CLOSE tables
DEALLOCATE tables
DROP TABLE #tables
DROP TABLE #columns
Author
4 Apr 2006 1:28 AM
SQL noob
Show quote Hide quote
"Will" wrote:

> Here's some SQL that I'd use to dynamically search all text data in a
> database. Like I said, I think you'll find it too complicated, but
> perhaps it will give you a starting point:
>
> DECLARE @ValueToFind varchar(100)
> SET @ValueToFind = 'LIVER'
>
>
>
>
> CREATE TABLE #tables
> (
>     TABLE_QUALIFIER    sysname,
>     TABLE_OWNER    sysname,
>     TABLE_NAME    sysname,
>     TABLE_TYPE    varchar(32),
>     REMARKS    varchar(254)
> )
>
> CREATE TABLE #Columns
> (
>     TABLE_QUALIFIER    sysname,
>     TABLE_OWNER    sysname,
>     TABLE_NAME    sysname,
>     COLUMN_NAME    sysname,
>     [DATA_TYPE]    smallint,
>     TYPE_NAME    sysname,
>     [PRECISION]    int,
>     LENGTH    int,
>     SCALE    smallint,
>     RADIX    smallint,
>     NULLABLE    smallint,
>     REMARKS    varchar(254),
>     COLUMN_DEF    nvarchar(4000),
>     SQL_DATA_TYPE    smallint,
>     SQL_DATETIME_SUB    smallint,
>     CHAR_OCTET_LENGTH    int,
>     ORDINAL_POSITION    int,
>     IS_NULLABLE    varchar(254),
>     SS_DATA_TYPE    tinyint
>
> )
>
> INSERT INTO #tables
> exec sp_tables
>
>
> DECLARE tables cursor
> FOR
> SELECT Table_name FROM #tables
>
> OPEN tables
>
> DECLARE @Tablename sysname
>
> FETCH NEXT FROM tables INTO @tablename
>
> WHILE @@FETCH_STATUS = 0
> BEGIN
>     INSERT INTO #Columns
>     exec sp_columns @TableName
>
>     DECLARE @SQL nvarchar(4000)
>     SET @SQL = 'SELECT * FROM ' + @Tablename + ' WHERE 1=2 '
>
>     DECLARE cols CURSOR
>     FOR SELECT Column_name FROM #Columns WHERE TYPE_NAME LIKE '%char' or
> type_name LIKE '%text'
>
>
>
>     DECLARE @ColName sysname
>     OPEN cols
>     FETCH NEXT FROM Cols INTO @ColName
>
>     WHILE @@FETCH_STATUS = 0
>     BEGIN
>         SET @SQL = @SQL + ' OR [' + @ColName + ']  LIKE ''%' + @ValueToFind +
> '%'' '
>         FETCH NEXT FROM Cols INTO @ColName
>     END
>
>
>     CLOSE Cols
>     DEALLOcate cols
>     TRUNCATE TABLE #Columns
>     FETCH NEXT FROM tables INTO @tablename
>
>     exec sp_executesql @SQL
> END
>
> CLOSE tables
> DEALLOCATE tables
> DROP TABLE #tables
> DROP TABLE #columns

Is it paste this code then can work? i trying out nw..
Show quoteHide quote
>
>
Author
4 Apr 2006 1:31 AM
SQL noob
Is it paste this code then can work? i trying out nw..
Author
6 Apr 2006 10:05 AM
Will
The code won't work just as it is, I posted it as a starting point for
you. You'll need to learn SQL properly and modify it to suit your
particular requirements. As for books, the best way to learn SQL in my
opinion is to try things, then search news groups as you encounter
problems.
Author
3 Apr 2006 7:42 AM
Omnibuzz
Author
4 Apr 2006 8:52 AM
SQL noob
can someone pls help mi? thx a million..
Author
6 Apr 2006 2:14 AM
SQL noob
can someone pls help mi?
Author
6 Apr 2006 9:46 AM
SQL noob
is there any books i can refer to? pls help mi..

Bookmark and Share