Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004
    Location
    austin
    Posts
    146

    Unanswered: Variable Table Name?

    How can i make a table name using a variable?


    Code:
    ALTER       PROCEDURE [P_Batch_Temp_Table] @User nvarchar(100), @Prog nvarchar(2), @Type nvarchar(4) as
    DECLARE @UserN nvarchar(100)
    SET @UserN = @User
    
    
    select * into ##Temp_Batch_  + @UserN 
    from tbl_batch WHERE PROD = @progN and TYPE = @TypeN and [User] = @UserN

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You can use dynamic SQL, but I have NEVER seen this as part of a good database design:

    declare @MyBadDynamicSQLString as varchar(500)
    set @MyBadDynamicSQLString = 'select * into ##Temp_Batch_' + @UserN + 'from tbl_batch WHERE PROD = @progN and TYPE = @TypeN and [User] = @UserN'
    exec @MyBadDynamicSQLString
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Jan 2004
    Location
    austin
    Posts
    146
    thanks for the reply.

    Using ASP.NET and SQL Server i'm trying to fill a temp table that is associated with a user. i'm open to other ideas??

  4. #4
    Join Date
    Jan 2004
    Location
    austin
    Posts
    146
    this worked...

    Code:
    ALTER        PROCEDURE [P_Batch_Temp_Table] @User nvarchar(100), @Prog nvarchar(2), @Type nvarchar(4) as
    
    declare @DSQLString as varchar(800)
    set @DSQLString = 'select * into ##Temp_Batch_' + @User + ' from tbl_batch WHERE [USER] = ' + "' @User '" + ' and Type = ' + "' @Type '" + ' and Prod = ' + "' @Prog '"
    exec (@DSQLString)

Posting Permissions

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