Results 1 to 3 of 3
  1. #1
    Join Date
    May 2003
    Location
    Valencia - Spain
    Posts
    7

    Unanswered: table name as a parameter

    I have to create a tabla with a different name each time. I have a stored procedure to do so like the following. Step 1 works fine but not step 2.
    The table name cannot include a parameter.
    How can I solve this?
    Any help on this will be appreciated.

    CREATE PROCEDURE NewTable
    @UserName as varchar
    AS

    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TABLE_' + @UserName + ']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)


    CREATE TABLE [dbo].[TABLE_' + @UserName + '] (......

  2. #2
    Join Date
    Dec 2004
    Posts
    5
    look at this:

    declare @stmt nvarchar(255)
    set @stmt = 'create table test (a integer not null)'
    exec sp_executesql @stmt

    this should solve your task

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd suggest you check out temp table opportunities. They should allow you to dodge this bullet altogether.

    -PatP

Posting Permissions

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