Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2005
    Posts
    1

    Unanswered: Table names and variables

    I have a cursor which populates a variable with the name of each user table within my DB.
    I'm trying to copy the tables one at a time by using a command like this:
    Code:
    SELECT * INTO @NewTable FROM @OrigTable
    Query analyzer tells me that there's incorrect syntax near the keyword 'INTO'.
    This seems fair enough to me as I assume it's trying to put the data into the variable rather than the table name which is held within the variable. Does any know how I can get around this?
    Are there any alternative ways of copying the table structure (preferable without the data)?

    BTW both variables are of type char(100)

  2. #2
    Join Date
    Jun 2003
    Posts
    269

    Thumbs up Re

    eg:

    declare @NewTable sysname
    declare @OrigTable sysname
    declare @query varchar(4000)

    set @NewTable='new'
    set @OrigTable='Old'

    set @query='SELECT * INTO ' +@NewTable+ ' FROM ' +@OrigTable+ ' where 1=0'
    exec(@query)
    Last edited by mallier; 11-14-05 at 12:13.
    I love cursor,though I never use it.Cos' I dont want to misuse it.
    ----------------------------------------------

    ----cheers
    ----http://mallier.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •