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. Simple Stuff...but I don't know it.

Simple Stuff...but I don't know it.

Scheduled Pinned Locked Moved Database
databasequestion
11 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.
  • I Offline
    I Offline
    imnotso
    wrote on last edited by
    #1

    Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.

    M J S 3 Replies Last reply
    0
    • I imnotso

      Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      Where exactly are you defining a temporary table in this example?

      I 1 Reply Last reply
      0
      • M Marek Grzenkowicz

        Where exactly are you defining a temporary table in this example?

        I Offline
        I Offline
        imnotso
        wrote on last edited by
        #3

        Hi, I'm not. Just want to use a variable to access an exisiting table. Select * from @parameter is this not possible? Thanks for the interest

        M 1 Reply Last reply
        0
        • I imnotso

          Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.

          J Offline
          J Offline
          John ph
          wrote on last edited by
          #4

          have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try... @SqlQuery = 'Select * from ' + @MyParam EXEC sp_executesql @SqlQuery

          Regards
           - J O H N -


          C I 2 Replies Last reply
          0
          • I imnotso

            Hi, I'm not. Just want to use a variable to access an exisiting table. Select * from @parameter is this not possible? Thanks for the interest

            M Offline
            M Offline
            Marek Grzenkowicz
            wrote on last edited by
            #5

            ASAIK, you can only create a table[^] variable, fill it with data and then use in SELECT statements.

            imnotso# wrote:

            Just want to use a variable to access an exisiting table.

            BTW, why do you want to do something like this?

            1 Reply Last reply
            0
            • J John ph

              have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try... @SqlQuery = 'Select * from ' + @MyParam EXEC sp_executesql @SqlQuery

              Regards
               - J O H N -


              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              You should always enclosed the table name in side square brackets just in case it contains any odd characters and the table name should be checked to see if the table exists first. These are very basic security precautions against invalid or maliceous data from causing problems with the database.


              Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

              1 Reply Last reply
              0
              • J John ph

                have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try... @SqlQuery = 'Select * from ' + @MyParam EXEC sp_executesql @SqlQuery

                Regards
                 - J O H N -


                I Offline
                I Offline
                imnotso
                wrote on last edited by
                #7

                Thanks John, but declare @SqlQuery varchar set @SqlQuery = 'Select * from raiser wHERE raiser.raiserid=3' EXEC @SqlQuery Server: Msg 2812, Level 16, State 62, Line 5 Could not find stored procedure 'S'. doesn't work what am I doing wrong?

                J 1 Reply Last reply
                0
                • I imnotso

                  Thanks John, but declare @SqlQuery varchar set @SqlQuery = 'Select * from raiser wHERE raiser.raiserid=3' EXEC @SqlQuery Server: Msg 2812, Level 16, State 62, Line 5 Could not find stored procedure 'S'. doesn't work what am I doing wrong?

                  J Offline
                  J Offline
                  John ph
                  wrote on last edited by
                  #8

                  Here is a link to an article which will help you to write and execute Dynamic SQL in a Stored Procedure.[^].

                  Regards
                   - J O H N -


                  1 Reply Last reply
                  0
                  • I imnotso

                    Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.

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

                    hi there, in this example you didn't create any table. You creating a variable and it is not possible to select from variable, -- Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' you can simple select like this: SELECT @MyParam -- it will give this result 'MyTableName' if you want to create table, use this Create Table #MyTableName ( here you define the attributes ex. atributename varchar(20) ) and then you can use SELECT * FROM #MyTableName the result --- atributename respect

                    spaps

                    I 1 Reply Last reply
                    0
                    • S Shpendh

                      hi there, in this example you didn't create any table. You creating a variable and it is not possible to select from variable, -- Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' you can simple select like this: SELECT @MyParam -- it will give this result 'MyTableName' if you want to create table, use this Create Table #MyTableName ( here you define the attributes ex. atributename varchar(20) ) and then you can use SELECT * FROM #MyTableName the result --- atributename respect

                      spaps

                      I Offline
                      I Offline
                      imnotso
                      wrote on last edited by
                      #10

                      Cheers guys, job done via Dynamic SQL. Why I can't lookup a variable table name, who knows? Seems stupid to not be able to. Thanks again.

                      P 1 Reply Last reply
                      0
                      • I imnotso

                        Cheers guys, job done via Dynamic SQL. Why I can't lookup a variable table name, who knows? Seems stupid to not be able to. Thanks again.

                        P Offline
                        P Offline
                        pmarfleet
                        wrote on last edited by
                        #11

                        imnotso# wrote:

                        Why I can't lookup a variable table name, who knows?

                        Because it's not part of the language specification.

                        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                        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