列出包含特定员工姓名的所有表

时间:2015-06-20 14:59:31

标签: sql-server-2008

我有一个员工姓名列表和一个表列表。我需要迭代所有员工姓名并找到员工姓名所在的表。所有要迭代的表都存在于同一服务器和同一个数据库中。

示例数据结构

Create Table TableNamesToCheck (dbName varchar(15))

Insert Into TableNamesToCheck 
Values ('table1'), ('table2'), ('table3'), ('table4'), ('table5'), 
       ('table6'), ('table7'), ('table8'), ('table9'), ('table10')

Create Table EN (employeename varchar(100))

Insert Into EN 
Values ('Richard Marx'), ('Joseph Jones'), ('Mark Badcock'), 
       ('Frank Fins'), ('Richard James'), ('Fall Fren'), ('Hiu Hen')

谢谢@Giorgi ---我理解使用Cursor作为我的表名,但是如何加入这两个表,当它们没有相似的字段加入2个表时?这就是我的工作(正在进行中),如果我理解你在解释这是我的语法,但它给了我每个员工和每个表的以下错误......

  

Msg 207,Level 16,State 1,Line 1   列名称无效,且名称为' employeename'。   Msg 207,Level 16,State 1,Line 1   列名称无效' Richard'。

Declare @dbname varchar(25), @empname varchar(100), @sql varchar(Max)

Create Table TableNamesToCheck (dbName varchar(15))

Insert Into TableNamesToCheck 
Values ('table1'), ('table2'), ('table3'), ('table4'), ('table5'), 
   ('table6'), ('table7'), ('table8'), ('table9'), ('table10')

Create Table EN (employeename varchar(100))

Insert Into EN 
Values ('Richard Marx'), ('Joseph Jones'), ('Mark Badcock'), 
   ('Frank Fins'), ('Richard James'), ('Fall Fren'), ('Hiu Hen')


Declare c1 Cursor For
Select dbname
From TableNamesToCheck

Open c1
Fetch Next from c1 into @dbname

Declare c2 Cursor For
Select employeename 
From EN

Open c2
Fetch Next from c2 into @empname

While @@FETCH_STATUS = 0
Begin

    While @@FETCH_STATUS = 0
    Begin

        Set @sql =  'Select '+@dbname+' From TableNamesToCheck where employeename IN (Select '+@empname+' from EN)'

        Exec (@sql)

    Fetch Next From c1 Into @dbname
    Fetch Next From c2 Into @empname

   End

End


Close c1
Close c2
Deallocate c1
Deallocate c2

Drop Table TableNamesToCheck
Drop Table EN

1 个答案:

答案 0 :(得分:1)

不是100%肯定,但您可能正在寻找: -

Set Nocount On;

Declare  @Sql       Varchar(Max)
        ,@Total     Int
        ,@RowId     Int
        ,@EmpName   Varchar(100)
        ,@Total2    Int
        ,@RowId2    Int
        ,@TableName Varchar(15)

Select   @Sql = ''
        ,@EmpName = ''
        ,@TableName = ''

If Object_Id('tempdb.dbo.#TableNamesToCheck') Is Not Null
Begin
    Drop Table #TableNamesToCheck;
End

If Object_Id('tempdb.dbo.#EN') Is Not Null
Begin
    Drop Table #EN;
End

If Object_Id('tempdb.dbo.#EnTables') Is Not Null
Begin
    Drop Table #EnTables;
End

Create Table #TableNamesToCheck
(
     dbId           Int Identity(1,1) Primary Key
    ,dbName         Varchar(15)
)

Create Table #EN 
(
     RowId          Int Identity(1,1) Primary Key
    ,employeename   Varchar(100)
)

Create Table #EnTables
(
     Id             Int Identity(1,1) Primary Key
    ,EmployeeName   Varchar(100)
    ,dbName         Varchar(15)
)

Insert Into #TableNamesToCheck 
Values ('table1'), ('table2'), ('table3'), ('table4'), ('table5'), 
       ('table6'), ('table7'), ('table8'), ('table9'), ('table10')

Insert Into #EN 
Values ('Richard Marx'), ('Joseph Jones'), ('Mark Badcock'), 
       ('Frank Fins'), ('Richard James'), ('Fall Fren'), ('Hiu Hen')

Select   @Total = Count(1)
        ,@RowId = 1
From    #EN As en With (Nolock)

Select   @Total2 = Count(1)
        ,@RowId2 = 1
From    #TableNamesToCheck As et With (Nolock)

While (@RowId <= @Total)
Begin
    Select  @EmpName = en.employeename
    From    #EN As en With (Nolock)
    Where   en.RowId = @RowId

    While (@RowId2 <= @Total2)
    Begin
        Select  @TableName = et.dbName
        From    #TableNamesToCheck As et With (Nolock)
        Where   et.dbId = @RowId2

        Select  @Sql =  ' If Exists ' +
                        ' ( ' +
                            ' Select    1 ' +
                            ' From      ' + @TableName + ' As t With (Nolock) ' +
                            ' Where     t.employeename = ''' + @EmpName + ''' ' +
                            ' ) ' +
                        ' Begin ' +
                            ' Insert Into #EnTables(EmployeeName,dbName) ' +
                             ' Select    ''' + @EmpName + ''' ' +
                                        ' ,''' + @TableName + ''' ' +
                            ' End '
        ----Print(@Sql)
        Exec (@Sql)

        Select  @RowId2 = @RowId2 + 1
    End

    Select   @RowId = @RowId + 1
            ,@RowId2 = 1
            ,@EmpName = ''
            ,@TableName = ''
End

Select  *
From    #EnTables As et With (Nolock)