Select from in multi-tables and multi-database











up vote
0
down vote

favorite












I need to do something like:



SELECT    data, hour, user, 
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...


We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.



Any one have an advice or idea how to do it?










share|improve this question









New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
    – Himanshu Ahuja
    2 days ago










  • What you mean by DDBB??
    – Sami
    2 days ago












  • Hi, DDBB means DataBase,
    – Bernat Moragas
    17 hours ago










  • Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
    – a_horse_with_no_name
    14 hours ago















up vote
0
down vote

favorite












I need to do something like:



SELECT    data, hour, user, 
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...


We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.



Any one have an advice or idea how to do it?










share|improve this question









New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
    – Himanshu Ahuja
    2 days ago










  • What you mean by DDBB??
    – Sami
    2 days ago












  • Hi, DDBB means DataBase,
    – Bernat Moragas
    17 hours ago










  • Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
    – a_horse_with_no_name
    14 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to do something like:



SELECT    data, hour, user, 
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...


We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.



Any one have an advice or idea how to do it?










share|improve this question









New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to do something like:



SELECT    data, hour, user, 
FROM [dbx].dbo._datamodel AS db0
UNION
SELECT data, hour, user,
FROM [dby].dbo._datamodel AS db1
UNION
SELECT data, hour, user,
FROM [dbz].dbo._datamodel AS db02
UNION
...


We have to do it for a lot of DDBB and we are looking the way to do it automatically for each DB with a condition.
We don't want to add the UNION and select for each DB every time.



Any one have an advice or idea how to do it?







sql tsql






share|improve this question









New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 14 hours ago









a_horse_with_no_name

286k45429526




286k45429526






New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Bernat Moragas

11




11




New contributor




Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
    – Himanshu Ahuja
    2 days ago










  • What you mean by DDBB??
    – Sami
    2 days ago












  • Hi, DDBB means DataBase,
    – Bernat Moragas
    17 hours ago










  • Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
    – a_horse_with_no_name
    14 hours ago


















  • create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
    – Himanshu Ahuja
    2 days ago










  • What you mean by DDBB??
    – Sami
    2 days ago












  • Hi, DDBB means DataBase,
    – Bernat Moragas
    17 hours ago










  • Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
    – a_horse_with_no_name
    14 hours ago
















create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago




create a procedure with a parameter db_name run it as many times as you want with giving the db name you need
– Himanshu Ahuja
2 days ago












What you mean by DDBB??
– Sami
2 days ago






What you mean by DDBB??
– Sami
2 days ago














Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago




Hi, DDBB means DataBase,
– Bernat Moragas
17 hours ago












Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
– a_horse_with_no_name
14 hours ago




Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...
– a_horse_with_no_name
14 hours ago












3 Answers
3






active

oldest

votes

















up vote
0
down vote













I think you are looking for DynamicSQL



DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';

SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
FROM master.dbo.sysdatabases
WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
SELECT @SQL;
--EXECUTE sp_executesql @SQL;





share|improve this answer






























    up vote
    0
    down vote













    I would simply create a view in a standard database that has the union:



    create view v_datamodel as
    select . . .
    union
    . . .;


    (Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)



    If your set of databases is fixed, then you can create the view once -- set it and forget it.



    If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.



    If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.






    share|improve this answer




























      up vote
      0
      down vote













      I was trying and still don't working.
      I've been trying with :



      Declare @sql AS VARCHAR(8000)
      SET @sql = ' IF ''?'' like ''dat%''
      EXECUTE ('' USE [?]
      Select process, processVers
      from dbo._datamodel
      '')'
      EXEC sp_MSforeachdb @sql



      It works for the SQL query, but i doesn't for the view. :(
      Any advice?






      share|improve this answer










      New contributor




      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















      • Please don't add additional information as comments. EDIT your question instead
        – a_horse_with_no_name
        14 hours ago











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350006%2fselect-from-in-multi-tables-and-multi-database%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I think you are looking for DynamicSQL



      DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';

      SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
      FROM master.dbo.sysdatabases
      WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
      SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
      SELECT @SQL;
      --EXECUTE sp_executesql @SQL;





      share|improve this answer



























        up vote
        0
        down vote













        I think you are looking for DynamicSQL



        DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';

        SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
        FROM master.dbo.sysdatabases
        WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
        SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
        SELECT @SQL;
        --EXECUTE sp_executesql @SQL;





        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          I think you are looking for DynamicSQL



          DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';

          SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
          FROM master.dbo.sysdatabases
          WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
          SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
          SELECT @SQL;
          --EXECUTE sp_executesql @SQL;





          share|improve this answer














          I think you are looking for DynamicSQL



          DECLARE @SQL NVARCHAR(MAX) = N'SELECT data, hour, user FROM ';

          SELECT @SQL = @SQL + [name] + '.dbo._datamodel UNION SELECT * FROM '
          FROM master.dbo.sysdatabases
          WHERE dbid > 4 ; --ReportServer and ReportServerTempDB with dbid 5 and 6
          SET @SQL = LEFT(@SQL, LEN(@SQL) - 21);
          SELECT @SQL;
          --EXECUTE sp_executesql @SQL;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Sami

          6,24031038




          6,24031038
























              up vote
              0
              down vote













              I would simply create a view in a standard database that has the union:



              create view v_datamodel as
              select . . .
              union
              . . .;


              (Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)



              If your set of databases is fixed, then you can create the view once -- set it and forget it.



              If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.



              If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.






              share|improve this answer

























                up vote
                0
                down vote













                I would simply create a view in a standard database that has the union:



                create view v_datamodel as
                select . . .
                union
                . . .;


                (Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)



                If your set of databases is fixed, then you can create the view once -- set it and forget it.



                If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.



                If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I would simply create a view in a standard database that has the union:



                  create view v_datamodel as
                  select . . .
                  union
                  . . .;


                  (Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)



                  If your set of databases is fixed, then you can create the view once -- set it and forget it.



                  If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.



                  If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.






                  share|improve this answer












                  I would simply create a view in a standard database that has the union:



                  create view v_datamodel as
                  select . . .
                  union
                  . . .;


                  (Side note: If you really just want union all, then use it. union incurs lots of extra overhead.)



                  If your set of databases is fixed, then you can create the view once -- set it and forget it.



                  If databases are being added and removed, then you have a process for doing so. This process can update the view, perhaps by using dynamic SQL as suggested in another answer. Or, you might have the list of appropriate databases somewhere other than sysdatabases.



                  If the database are created "ad hoc", then you could use DDL triggers. I'm not sure if you can define these on create database, but within each database, you could check for the creation of the "data model" table.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Gordon Linoff

                  744k32285390




                  744k32285390






















                      up vote
                      0
                      down vote













                      I was trying and still don't working.
                      I've been trying with :



                      Declare @sql AS VARCHAR(8000)
                      SET @sql = ' IF ''?'' like ''dat%''
                      EXECUTE ('' USE [?]
                      Select process, processVers
                      from dbo._datamodel
                      '')'
                      EXEC sp_MSforeachdb @sql



                      It works for the SQL query, but i doesn't for the view. :(
                      Any advice?






                      share|improve this answer










                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • Please don't add additional information as comments. EDIT your question instead
                        – a_horse_with_no_name
                        14 hours ago















                      up vote
                      0
                      down vote













                      I was trying and still don't working.
                      I've been trying with :



                      Declare @sql AS VARCHAR(8000)
                      SET @sql = ' IF ''?'' like ''dat%''
                      EXECUTE ('' USE [?]
                      Select process, processVers
                      from dbo._datamodel
                      '')'
                      EXEC sp_MSforeachdb @sql



                      It works for the SQL query, but i doesn't for the view. :(
                      Any advice?






                      share|improve this answer










                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • Please don't add additional information as comments. EDIT your question instead
                        – a_horse_with_no_name
                        14 hours ago













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      I was trying and still don't working.
                      I've been trying with :



                      Declare @sql AS VARCHAR(8000)
                      SET @sql = ' IF ''?'' like ''dat%''
                      EXECUTE ('' USE [?]
                      Select process, processVers
                      from dbo._datamodel
                      '')'
                      EXEC sp_MSforeachdb @sql



                      It works for the SQL query, but i doesn't for the view. :(
                      Any advice?






                      share|improve this answer










                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      I was trying and still don't working.
                      I've been trying with :



                      Declare @sql AS VARCHAR(8000)
                      SET @sql = ' IF ''?'' like ''dat%''
                      EXECUTE ('' USE [?]
                      Select process, processVers
                      from dbo._datamodel
                      '')'
                      EXEC sp_MSforeachdb @sql



                      It works for the SQL query, but i doesn't for the view. :(
                      Any advice?







                      share|improve this answer










                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer








                      edited 14 hours ago





















                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered 14 hours ago









                      Bernat Moragas

                      11




                      11




                      New contributor




                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Bernat Moragas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.












                      • Please don't add additional information as comments. EDIT your question instead
                        – a_horse_with_no_name
                        14 hours ago


















                      • Please don't add additional information as comments. EDIT your question instead
                        – a_horse_with_no_name
                        14 hours ago
















                      Please don't add additional information as comments. EDIT your question instead
                      – a_horse_with_no_name
                      14 hours ago




                      Please don't add additional information as comments. EDIT your question instead
                      – a_horse_with_no_name
                      14 hours ago










                      Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.













                      Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.












                      Bernat Moragas is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350006%2fselect-from-in-multi-tables-and-multi-database%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno