Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Query at database level

Query at database level

Scheduled Pinned Locked Moved Database
databasesysadminquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hello, I would like to write a select statement at the database server level, like trying to find all the existing indexes for all the databases in that server - something like this? But for all the databases in that given server and not individual database servers. This below piece works only for each databases. -- select t.name, i.name from sys.tables t, sys.indexes i where i.object_id = t.object_id -- thanks!

    H H S 3 Replies Last reply
    0
    • V vanikanc

      Hello, I would like to write a select statement at the database server level, like trying to find all the existing indexes for all the databases in that server - something like this? But for all the databases in that given server and not individual database servers. This below piece works only for each databases. -- select t.name, i.name from sys.tables t, sys.indexes i where i.object_id = t.object_id -- thanks!

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      AFAIK It's not implicitly possible with T-SQL as Particular index information is not stored globally, It's in corresponding DB. But there's some suggestion that you may apply to get. a) Get name of all the database in Server

      select name from master.dbo.sysdatabases

      b) Take database one by one from the above query result and make a custom query and execute it using EXEC statement. Like

      Use --Name database name one by one
      select t.name, i.name
      from sys.tables t, sys.indexes i
      where i.object_id = t.object_id

      c) Store each result in one temperory table with the required column. d) Finally SELECT * FROM #Temptable I Hope you are getting me.

      Regards, Hiren.

      My Recent Article: - Way to know which control have raised a postback
      My Recent Tip/Trick: - The ?? Operator.

      V 1 Reply Last reply
      0
      • H Hiren solanki

        AFAIK It's not implicitly possible with T-SQL as Particular index information is not stored globally, It's in corresponding DB. But there's some suggestion that you may apply to get. a) Get name of all the database in Server

        select name from master.dbo.sysdatabases

        b) Take database one by one from the above query result and make a custom query and execute it using EXEC statement. Like

        Use --Name database name one by one
        select t.name, i.name
        from sys.tables t, sys.indexes i
        where i.object_id = t.object_id

        c) Store each result in one temperory table with the required column. d) Finally SELECT * FROM #Temptable I Hope you are getting me.

        Regards, Hiren.

        My Recent Article: - Way to know which control have raised a postback
        My Recent Tip/Trick: - The ?? Operator.

        V Offline
        V Offline
        vanikanc
        wrote on last edited by
        #3

        Thanks for the reply, Hiren. I have tried to use the cursor to loop through all the databases, passing in one database at a time to th inner cursor at the master level database. But it is not getting the correct results. I believe you cannot use the reserved word USE in stored procs?

        H J T 4 Replies Last reply
        0
        • V vanikanc

          Thanks for the reply, Hiren. I have tried to use the cursor to loop through all the databases, passing in one database at a time to th inner cursor at the master level database. But it is not getting the correct results. I believe you cannot use the reserved word USE in stored procs?

          H Offline
          H Offline
          Hiren solanki
          wrote on last edited by
          #4

          vanikanc wrote:

          I believe you cannot use the reserved word USE in stored procs?

          I know that we can't use it plainly. IMO like following way.

          Declare @sql varchar(max)
          set @sql = 'Use AdventureWorksDW
          select * from dbo.ProspectiveBuyer';
          EXEC(@sql)

          Regards, Hiren.

          My Recent Article: - Way to know which control have raised a postback
          My Recent Tip/Trick: - The ?? Operator.

          1 Reply Last reply
          0
          • V vanikanc

            Hello, I would like to write a select statement at the database server level, like trying to find all the existing indexes for all the databases in that server - something like this? But for all the databases in that given server and not individual database servers. This below piece works only for each databases. -- select t.name, i.name from sys.tables t, sys.indexes i where i.object_id = t.object_id -- thanks!

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            I don't know a way to do what you require using SQL/T-SQL but it can definitely be done using SMO (Example of a C# utility using SMO to script indexes[^]) or a combination of SMO and PowerShell.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            1 Reply Last reply
            0
            • V vanikanc

              Thanks for the reply, Hiren. I have tried to use the cursor to loop through all the databases, passing in one database at a time to th inner cursor at the master level database. But it is not getting the correct results. I believe you cannot use the reserved word USE in stored procs?

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              vanikanc wrote:

              I believe you cannot use the reserved word USE in stored procs?

              A table name can be specified with the following form.

              [ server_name . database_name . schema_name .
              | database_name .[ schema_name ] .
              | schema_name .
              ]
              table_or_view_name

              That allows one to do a select from another database. Thus for example the 'master.dbo.sysdatabases' is using the form that starts with the database name.

              1 Reply Last reply
              0
              • V vanikanc

                Hello, I would like to write a select statement at the database server level, like trying to find all the existing indexes for all the databases in that server - something like this? But for all the databases in that given server and not individual database servers. This below piece works only for each databases. -- select t.name, i.name from sys.tables t, sys.indexes i where i.object_id = t.object_id -- thanks!

                S Offline
                S Offline
                senthil raja j
                wrote on last edited by
                #7

                Try Like this in Your SP If You Know all the Database Names.

                select t.name, i.name
                from DatabaseName1.sys.tables t, DatabaseName1.sys.indexes i
                where i.object_id = t.object_id
                union
                select t.name, i.name
                from DatabaseName2.sys.tables t, DatabaseName2.sys.indexes i
                where i.object_id = t.object_id

                Regards Senthil Raja.J

                modified on Thursday, December 30, 2010 4:34 AM

                H 1 Reply Last reply
                0
                • S senthil raja j

                  Try Like this in Your SP If You Know all the Database Names.

                  select t.name, i.name
                  from DatabaseName1.sys.tables t, DatabaseName1.sys.indexes i
                  where i.object_id = t.object_id
                  union
                  select t.name, i.name
                  from DatabaseName2.sys.tables t, DatabaseName2.sys.indexes i
                  where i.object_id = t.object_id

                  Regards Senthil Raja.J

                  modified on Thursday, December 30, 2010 4:34 AM

                  H Offline
                  H Offline
                  Hiren solanki
                  wrote on last edited by
                  #8

                  Please wrap the code in PRE tag.

                  Regards, Hiren.

                  My Recent Article: - Way to know which control have raised a postback
                  My Recent Tip/Trick: - The ?? Operator.

                  1 Reply Last reply
                  0
                  • V vanikanc

                    Thanks for the reply, Hiren. I have tried to use the cursor to loop through all the databases, passing in one database at a time to th inner cursor at the master level database. But it is not getting the correct results. I believe you cannot use the reserved word USE in stored procs?

                    H Offline
                    H Offline
                    Hiren solanki
                    wrote on last edited by
                    #9

                    Hi vanikanc, I got the solution regarding BUT I am going to publish a TIP/TRICK on that today so please wait and I will provide you a link to TIP/TRICK Soon.

                    Regards, Hiren.

                    My Recent Article: - Way to know which control have raised a postback
                    My Recent Tip/Trick: - The ?? Operator.

                    1 Reply Last reply
                    0
                    • V vanikanc

                      Thanks for the reply, Hiren. I have tried to use the cursor to loop through all the databases, passing in one database at a time to th inner cursor at the master level database. But it is not getting the correct results. I believe you cannot use the reserved word USE in stored procs?

                      T Offline
                      T Offline
                      Terry Reardon
                      wrote on last edited by
                      #10

                      Hi, try something like this and build on a sql string, I hope this helps.

                      DECLARE @sql VARCHAR(MAX), @db VARCHAR(255)

                      DECLARE c CURSOR FOR
                      SELECT [name] from master.dbo.sysdatabases
                      OPEN c
                      FETCH NEXT FROM c INTO @db
                      WHILE @@FETCH_STATUS = 0
                      BEGIN

                      SET @sql = 'Use ' + @db + SPACE(2)
                      SET @sql = @sql + ' SELECT t.name As TableName, i.name As IndexName FROM sys.tables t,
                      sys.indexes i
                      WHERE i.object_id = t.object_id
                      ORDER BY t.Name'

                      FETCH NEXT FROM c INTO @db END

                      CLOSE c
                      DEALLOCATE c

                      --PRINT @SQL

                      EXEC(@sql)

                      TJR We Came! We Saw! We Listened! We Eliminated Ambiguity and developed a system the user wanted, not what we thought they wanted. Enough Said!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups