如何找到所有数据库表中常见的值?

时间:2015-02-27 06:50:46

标签: c# mysql asp.net database

我正在开展一个项目,我已经在那些已完成数据输入的网站的网页上显示通知。数据条目保存在不同的数据库表下,主键名为"站点ID"。

如何才能找到所有表格中存在的网站ID?

请帮助,我在SQL quires中一点都不好,非常感谢您的帮助。

谢谢。

3 个答案:

答案 0 :(得分:2)

那么,如果您的数据库中有几个表,那么您可以编写一个存储过程来选择SiteName和SiteID,或者通过对具有唯一字段SiteID的所有表执行INNER JOIN来编写所需的列。

例如,您可以这样做:

Select S.SiteName,S.SiteID from 
Sites S
Inner JOIN AnotherTable AT ON S.SiteID=AT.SiteID
Inner JOIN AnotherTable2 AT2 ON S.SiteID=AT2.SiteID
.............

但您需要在查询中内连接所有表。 Inner Join将为您提供仅匹配记录的结果。 因此,在此查询中,只有当SiteName和SiteID与下面指定的所有表匹配时,才会从Sites表中选择SiteName和SiteID。 我认为这会有所帮助。

答案 1 :(得分:1)

您可以使用INNER JOIN过滤器的所有表格尝试Where

DECALRE @valueToSearch VARCHAR(10) = 'BIH004'
Select site .SiteName,site .SiteID 
from 
    Sites site Inner JOIN Table1 t1
        ON site.SiteID = t1.SiteID
    Inner JOIN Table2 t2 ON site.SiteID = t2.SiteID
    .....
WHERE 
     site.SiteID = @valueToSearch 

但是如果有很多表,那么你可以尝试以下查询。

DECLARE @SearchStr VARCHAR(10)= '100000'
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630)) -- Table to hold the results

SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET  @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
    SET @ColumnName = ''
    SET @TableName = 
    (
        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
        FROM    INFORMATION_SCHEMA.TABLES
        WHERE       TABLE_TYPE = 'BASE TABLE'
            AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
            AND OBJECTPROPERTY(
                    OBJECT_ID(
                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                         ), 'IsMSShipped'
                           ) = 0
            AND TABLE_NAME IN (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'SiteID')-- This is optional if your column name is same trough out all tables
    )
    --This is optional if your column name is same trough out all tables then you can remove the loop and directly use the insert query.
    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL) -- You can avoid this loop if the column name is uniq through out all tables as `SiteID`
    BEGIN
        SET @ColumnName =
        (
            SELECT MIN(QUOTENAME(COLUMN_NAME))
            FROM    INFORMATION_SCHEMA.COLUMNS
            WHERE       TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                AND TABLE_NAME  = PARSENAME(@TableName, 1)
                AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
                AND QUOTENAME(COLUMN_NAME) > @ColumnName
        )

        IF @ColumnName IS NOT NULL
        BEGIN
            INSERT INTO #Results
            EXEC
            (
                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) 
                FROM ' + @TableName + ' (NOLOCK) ' +
                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
            )
        END
    END 
END

SELECT ColumnName, ColumnValue FROM #Results

这将为您提供结果,

  schema.tablename.columname   valueofthatcolumn

答案 2 :(得分:0)

如果要查找site_Id存在的表,则可以编写查询:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE 'site_Id'