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. Select * from @TableName

Select * from @TableName

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
11 Posts 7 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.
  • A AndieDu

    Declare @idOrder as nvarchar(10) set @idOrder = '144' Declare @sInvoice as nvarchar(50) set @sInvoice = 'UK_B_IN000051' Declare @DateRange as nvarchar(100) set @DateRange = '28Jul2010' Declare @Country as nvarchar(10) set @Country = substring(@sInvoice, 1, 2) Declare @TableName as nvarchar(500) set @TableName = 'splashdb..Comersus_' + @Country + '_Orders_' + @DateRange what i want is: select * from @TableName, but sql server returns me error. If someone in here can shed me a light would be really appreciated, or there is an alternative way to achieve the same goal?

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #2

    You have to construct a query by combining required variables and pass that to EXEC[^] TSQL function.

    Best wishes, Navaneeth

    A 1 Reply Last reply
    0
    • N N a v a n e e t h

      You have to construct a query by combining required variables and pass that to EXEC[^] TSQL function.

      Best wishes, Navaneeth

      A Offline
      A Offline
      AndieDu
      wrote on last edited by
      #3

      Thank very much, i knew the one that you mentioned, but this way is not what i want, coz it is not feasible for me since i have a very long select statement.

      1 Reply Last reply
      0
      • A AndieDu

        Declare @idOrder as nvarchar(10) set @idOrder = '144' Declare @sInvoice as nvarchar(50) set @sInvoice = 'UK_B_IN000051' Declare @DateRange as nvarchar(100) set @DateRange = '28Jul2010' Declare @Country as nvarchar(10) set @Country = substring(@sInvoice, 1, 2) Declare @TableName as nvarchar(500) set @TableName = 'splashdb..Comersus_' + @Country + '_Orders_' + @DateRange what i want is: select * from @TableName, but sql server returns me error. If someone in here can shed me a light would be really appreciated, or there is an alternative way to achieve the same goal?

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #4

        It look to me that your database is a total mess. Why don't you have separate columns for region and date in one and the same table. Then you could make the query as simple as

        Select * from Comersus where sInvoice = 'B_IN000051' and Country = 'UK' and Date = '28Jul2010'

        where Date naturally shouldn't be a string but an actual Date

        "When did ignorance become a point of view" - Dilbert

        A 1 Reply Last reply
        0
        • J Jorgen Andersson

          It look to me that your database is a total mess. Why don't you have separate columns for region and date in one and the same table. Then you could make the query as simple as

          Select * from Comersus where sInvoice = 'B_IN000051' and Country = 'UK' and Date = '28Jul2010'

          where Date naturally shouldn't be a string but an actual Date

          "When did ignorance become a point of view" - Dilbert

          A Offline
          A Offline
          AndieDu
          wrote on last edited by
          #5

          Mate, first of all, thanks for your reply, but if i can go the way that you mentioned, i wouldn't jump on internet, coz be realistic, a perfect database is a db that has no table. Hope you got my drift.

          J 1 Reply Last reply
          0
          • A AndieDu

            Declare @idOrder as nvarchar(10) set @idOrder = '144' Declare @sInvoice as nvarchar(50) set @sInvoice = 'UK_B_IN000051' Declare @DateRange as nvarchar(100) set @DateRange = '28Jul2010' Declare @Country as nvarchar(10) set @Country = substring(@sInvoice, 1, 2) Declare @TableName as nvarchar(500) set @TableName = 'splashdb..Comersus_' + @Country + '_Orders_' + @DateRange what i want is: select * from @TableName, but sql server returns me error. If someone in here can shed me a light would be really appreciated, or there is an alternative way to achieve the same goal?

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #6

            No matter how you work it you are not going to get what you want.

            Select * from @TableName

            The sames as this will not work

            Select @ColumnName from Table

            You are going to have to construct a string and execute it, it is called dynamic SQL and this is one of the few reason it is a valid solution. BTW I agree that your db looks ugly (I presume you are aware of that). Have you looked into partitioning, as I assume you have split the orders by country/date for performance reasons.

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • A AndieDu

              Mate, first of all, thanks for your reply, but if i can go the way that you mentioned, i wouldn't jump on internet, coz be realistic, a perfect database is a db that has no table. Hope you got my drift.

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #7

              Well, I am curious why you need to have your database modeled that way. The best reason I can think of at the moment is: "It's always been like that and now it's to much work to change"

              "When did ignorance become a point of view" - Dilbert

              1 Reply Last reply
              0
              • A AndieDu

                Declare @idOrder as nvarchar(10) set @idOrder = '144' Declare @sInvoice as nvarchar(50) set @sInvoice = 'UK_B_IN000051' Declare @DateRange as nvarchar(100) set @DateRange = '28Jul2010' Declare @Country as nvarchar(10) set @Country = substring(@sInvoice, 1, 2) Declare @TableName as nvarchar(500) set @TableName = 'splashdb..Comersus_' + @Country + '_Orders_' + @DateRange what i want is: select * from @TableName, but sql server returns me error. If someone in here can shed me a light would be really appreciated, or there is an alternative way to achieve the same goal?

                A Offline
                A Offline
                AndieDu
                wrote on last edited by
                #8

                Thanks All for your help, and i will find an alternative way to do it then.

                S S 2 Replies Last reply
                0
                • A AndieDu

                  Thanks All for your help, and i will find an alternative way to do it then.

                  S Offline
                  S Offline
                  small_programmer
                  wrote on last edited by
                  #9

                  hello you can use the exec function. like this: exec('select * from ' + @TableName) that execute a query and return the result set good luck

                  1 Reply Last reply
                  0
                  • A AndieDu

                    Thanks All for your help, and i will find an alternative way to do it then.

                    S Offline
                    S Offline
                    Simon_Whale
                    wrote on last edited by
                    #10

                    your alternatives are limited 1. create seperate SQL queries for all possibilities 2. Create an external application to overcome it 3. use dynamic SQL, Erlands Dynamic SQL article others might chip in other alternatives for you

                    As barmey as a sack of badgers

                    1 Reply Last reply
                    0
                    • A AndieDu

                      Declare @idOrder as nvarchar(10) set @idOrder = '144' Declare @sInvoice as nvarchar(50) set @sInvoice = 'UK_B_IN000051' Declare @DateRange as nvarchar(100) set @DateRange = '28Jul2010' Declare @Country as nvarchar(10) set @Country = substring(@sInvoice, 1, 2) Declare @TableName as nvarchar(500) set @TableName = 'splashdb..Comersus_' + @Country + '_Orders_' + @DateRange what i want is: select * from @TableName, but sql server returns me error. If someone in here can shed me a light would be really appreciated, or there is an alternative way to achieve the same goal?

                      D Offline
                      D Offline
                      dan sh
                      wrote on last edited by
                      #11

                      You will have to use dynamic queries or a series of if/else blocks to do this. Dynamic queries are not a good way to do things. If/else, will look ugly if you have even 10 of them. So, either live with dynamic queries or redesign things.

                      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