开发者

How do you use a T-SQL variable within an if OBJECT_ID to check if a table exists?

开发者 https://www.devze.com 2023-03-10 02:48 出处:网络
What I am trying to do with the following code is have it grab all the database names then loop through those databases check to see if the table tblAdminLogin exists and if it does update the passwor

What I am trying to do with the following code is have it grab all the database names then loop through those databases check to see if the table tblAdminLogin exists and if it does update the password for username 'foo'

I have been using a select statement instead of an update as of yet until it works properly.

declare @databases table
(
    PK      int IDENTITY(1,1), 
    dbid    int,
    name    varchar(50)
)

insert into @databases(dbid, name) select dbid, name from master.dbo.sysdatabases

Declare @maxPK int; select @maxPK = MAX(PK) from @databases
Declare @pk int; Set @pk = 1
Declare @name varchar(50)

While @pk <= @maxPK
Begin
    Select @name = name from @databases where PK=@pk
    if OBJECT_ID(''+@name+'.dbo.tblAdminLogin') IS NOT NULL
    Begin
        Select password from @name.dbo.tblAdminLogin where username = 'foo'
        --Update @name.dbo.tblAdminLogin Set password='bar' where username = 'foo' 
    End
    Set @pk = @pk + 1
End

The main code in question is

    Select @name = name from @databases where PK=@pk
    if OBJECT_ID(''+@name+'.dbo.tblAdminLogin') IS NOT NULL
    Begin
        Select password from @name.dbo.tblAdminLogin wh开发者_高级运维ere username = 'foo'
        --Update @name.dbo.tblAdminLogin Set password='bar' where username = 'foo' 
    End

Edit: Added T-SQL to title since I am using SQL Server

Edit: Fixed the Typo .dbl. to the correct .dbo.


Dynamic SQL.

Untested

declare @SQL varchar(max) -- varchar(8000) if on SQL Server 2000 or earlier
While @pk <= @maxPK  
Begin      
    Select @name = name from @databases where PK=@pk
    if OBJECT_ID(''+@name+'.dbo.tblAdminLogin') IS NOT NULL      
    Begin          
        set @SQL = 'update ' + quotename(@name) + '.dbo.tblAdminLogin Set     password=''bar'' where username = ''foo'''
        exec (@SQL)
    End
    set @PK = @PK + 1
End

References:

  • QUOTENAME
  • EXECUTE

Edit: general note on types, not asked for by OP:

The database name is of type SYSNAME which, last I checked, is equivelent to a NVARCHAR(128). Storing that value in a VARCHAR type has the chance of losing information. Small chance at some site because of the names they would choose, but a chance none the less.

declare @databases table
(
    PK      int IDENTITY(1,1), 
    dbid    int,
    name    sysname
)

insert into @databases(dbid, name) select dbid, name from master.dbo.sysdatabases

Declare @maxPK int; select @maxPK = MAX(PK) from @databases
Declare @pk int; Set @pk = 1
Declare @name sysname -- so that 

declare @SQL nvarchar(4000) 
While @pk <= @maxPK  

Begin      
    Select @name = name from @databases where PK=@pk
    if OBJECT_ID(@name+N'.dbo.tblAdminLogin') IS NOT NULL      
    Begin          
        set @SQL = N'update ' + quotename(@name) + N'.dbo.tblAdminLogin Set     password=''bar'' where username = ''foo'''
        exec (@SQL)
    End
    set @PK = @PK + 1
End


Alternative:

EXEC sp_MSForEachDB 
  'Use [?]; if object_id(''tblAdminLogin'') is not null Select password from tblAdminLogin where username = ''foo'''


I might do something like this:

exec sp_msforeachdb '
if (object_id(''[?].[dbo].[tblAdminLogin]'', ''U'')) is not null
begin
  update table [?].[dbo].[tblAdminLogin] Set password=''bar'' where username = ''foo''
  select ''?'', username, password from [?].[dbo].[tblAdminLogin] where username = ''foo''
end
'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号