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. Table name a String

Table name a String

Scheduled Pinned Locked Moved Database
databasesharepoint
3 Posts 3 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.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    Hi All i have a sp which to querys a database, but each time the sp is run the name of one of the tables changes, so i want to pass the table name into the query as a string something like DECLARE @table_name varchar(100) set @table_name='mytable' exec ('select my_id from ' + @table_name+' ORDER BY my_ID') which works fine when im only using one table i wanted to do a join across several tables, and do something like set @sql_stmt = 'select callingnumber,seizedatetime,callednumber,new_action.telephone,new_action.action1,new_action.action2, new_call.student_id, datepart(dd,seizedatetime) as myday, null as sent_it from new_call,new_action,'+@table_name+' where new_call.call_ID=new_action.call_id' EXEC ( @sql_stmt ) but that doesnt work does ne1 have any ideas :~ :confused::confused:

    M G 2 Replies Last reply
    0
    • S si_69

      Hi All i have a sp which to querys a database, but each time the sp is run the name of one of the tables changes, so i want to pass the table name into the query as a string something like DECLARE @table_name varchar(100) set @table_name='mytable' exec ('select my_id from ' + @table_name+' ORDER BY my_ID') which works fine when im only using one table i wanted to do a join across several tables, and do something like set @sql_stmt = 'select callingnumber,seizedatetime,callednumber,new_action.telephone,new_action.action1,new_action.action2, new_call.student_id, datepart(dd,seizedatetime) as myday, null as sent_it from new_call,new_action,'+@table_name+' where new_call.call_ID=new_action.call_id' EXEC ( @sql_stmt ) but that doesnt work does ne1 have any ideas :~ :confused::confused:

      M Offline
      M Offline
      michanne1
      wrote on last edited by
      #2

      I don't see the error. What is the error msg? You might try printing the result instead of exec right off the bat. Paste the result into another qa window and see what errors you get. Hope that helps michanne

      1 Reply Last reply
      0
      • S si_69

        Hi All i have a sp which to querys a database, but each time the sp is run the name of one of the tables changes, so i want to pass the table name into the query as a string something like DECLARE @table_name varchar(100) set @table_name='mytable' exec ('select my_id from ' + @table_name+' ORDER BY my_ID') which works fine when im only using one table i wanted to do a join across several tables, and do something like set @sql_stmt = 'select callingnumber,seizedatetime,callednumber,new_action.telephone,new_action.action1,new_action.action2, new_call.student_id, datepart(dd,seizedatetime) as myday, null as sent_it from new_call,new_action,'+@table_name+' where new_call.call_ID=new_action.call_id' EXEC ( @sql_stmt ) but that doesnt work does ne1 have any ideas :~ :confused::confused:

        G Offline
        G Offline
        Grimolfr
        wrote on last edited by
        #3

        Looks to me like you forget to put a statement in the WHERE clause to join @table_name to either new_call or new_action. I can't be sure, but it also looks like you may have a whitespace problem. Are you actually getting an error message, or are just just not getting the results you expected?


        Grim

        (aka Toby)

        MCDBA, MCSD, MCP+SB

        Need a Second Life?[^]

        SELECT * FROM user WHERE clue IS NOT NULL GO

        (0 row(s) affected)

        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