Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:26:14+00:00 2026-05-22T22:26:14+00:00

What I am trying to do with the following code is have it grab

  • 0

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 where 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:26:15+00:00Added an answer on May 22, 2026 at 10:26 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have the following code, I'm trying to get a table with 4 columns
I am trying to use the following code, which I have not been able
I'm trying to drop a SQL Server database using the following code: SqlCommand command
I have the following bit of code. I'm trying to dynamically add drop-down options
I've got the following code in my Services project, which is trying to grab
I am trying the following code: <?php $link = mysql_connect('localhost', 'root', 'geheim'); if (!$link)
I'm trying to use the following code but it's returning the wrong day of
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
I am trying to use the following code to write data into an excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.