help-CREATE TABLE-how can i do this

  • help how can i do this

    and use variable "@tb"

    ---------------------------------

    DECLARE @tb char (20)

    SET @tb ='ilan'

    CREATE TABLE @tb (

    [id] [int] NOT NULL ,

    [Fname] [nvarchar] (50) COLLATE Hebrew_CI_AS NULL ,

    [mhlka] [int] NOT NULL ,

    [ilan] [int] NOT NULL DEFAULT (1) ,

    [fld1] [smalldatetime] NOT NULL ,

    [fld2] [int] NOT NULL ,

    [fld3] [nvarchar] (200) COLLATE Hebrew_CI_AS NULL ,

    [fld4] [smalldatetime] NOT NULL ,

    [fld5] [int] NULL ,

    [fld6] [char] (10) COLLATE Hebrew_CI_AS NULL ,

    [na] [int] IDENTITY (1, 1) NOT NULL

    ) ON [PRIMARY]

    INSERT INTO [@tb]([id], [Fname], [mhlka], [fld1], [fld2], [fld3], [fld4], [fld5])

    SELECT id int, [Fname][nvarchar(50)], [mhlka_id]int, [fld1]smalldatetime, [fld2]int, [fld3][nvarchar(200)], [fld4]smalldatetime, [fld5]int

    FROM [sidor]

    SELECT * FROM @tb

    DROP TABLE @tb

    -------------------------------------

  • Try:

     
    
    DECLARE @tb char (20)
    SET @tb ='ilan'

    EXECUTE ('
    CREATE TABLE ' + @tb + '(
    [id] [int] NOT NULL ,
    [Fname] [nvarchar] (50) COLLATE Hebrew_CI_AS NULL ,
    [mhlka] [int] NOT NULL ,
    [ilan] [int] NOT NULL DEFAULT (1) ,
    [fld1] [smalldatetime] NOT NULL ,
    [fld2] [int] NOT NULL ,
    [fld3] [nvarchar] (200) COLLATE Hebrew_CI_AS NULL ,
    [fld4] [smalldatetime] NOT NULL ,
    [fld5] [int] NULL ,
    [fld6] [char] (10) COLLATE Hebrew_CI_AS NULL ,
    [na] [int] IDENTITY (1, 1) NOT NULL
    ) ON [PRIMARY]

    INSERT INTO [' + @tb + ']([id], [Fname], [mhlka], [fld1], [fld2], [fld3], [fld4], [fld5])
    SELECT 1 , ''x'', 1, ''00:00'', 1, ''x'', ''00:00'', 1


    SELECT * FROM ' + @tb + '

    DROP TABLE ' + @tb + '
    ')

    Cheers,

    - Mark


    Cheers,
    - Mark

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply