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. Create Table from Trigger

Create Table from Trigger

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
4 Posts 2 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.
  • G Offline
    G Offline
    Guinness4Strength
    wrote on last edited by
    #1

    I'm attempting to create a new table from and INSERT trigger on MSSQL Server. I've tried the following with no luck...any help out there /

    ALTER TRIGGER [TRG_INS_CLIENTS] ON [dbo].[Clients]
    FOR INSERT
    AS
    DECLARE @ClientName VARCHAR(50)
    SELECT @ClientName = ClientName+'_ParseTable' FROM inserted

    CREATE TABLE @ClientName (
    [PrimaryKey] [decimal](18, 0) IDENTITY (1, 1) NOT NULL PRIMARY KEY CLUSTERED,
    [FieldName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Source] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Target] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [Steps] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
    )

    E 1 Reply Last reply
    0
    • G Guinness4Strength

      I'm attempting to create a new table from and INSERT trigger on MSSQL Server. I've tried the following with no luck...any help out there /

      ALTER TRIGGER [TRG_INS_CLIENTS] ON [dbo].[Clients]
      FOR INSERT
      AS
      DECLARE @ClientName VARCHAR(50)
      SELECT @ClientName = ClientName+'_ParseTable' FROM inserted

      CREATE TABLE @ClientName (
      [PrimaryKey] [decimal](18, 0) IDENTITY (1, 1) NOT NULL PRIMARY KEY CLUSTERED,
      [FieldName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Source] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Target] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Steps] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
      )

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      One possible solution:

      CREATE TRIGGER TRG_INS_CLIENTS ON [dbo].[Clients]
      FOR INSERT
      AS
      DECLARE @ClientName VARCHAR(50)
      SELECT @ClientName = ClientName FROM inserted

      EXEC('CREATE TABLE ' + @ClientName + '(
      [PrimaryKey] [decimal](18, 0) IDENTITY (1, 1) NOT NULL PRIMARY KEY CLUSTERED,
      [FieldName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Source] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Target] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [Steps] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL )')

      I'm assuming that your field name is ClientName. I'm not sure what +'_ParseTable' is. If it is the field name, then just put ClientName_ParseTable in the select. Be careful with EXEC() - it opens a door to malicious code. ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

      G 1 Reply Last reply
      0
      • E Eric Dahlvang

        One possible solution:

        CREATE TRIGGER TRG_INS_CLIENTS ON [dbo].[Clients]
        FOR INSERT
        AS
        DECLARE @ClientName VARCHAR(50)
        SELECT @ClientName = ClientName FROM inserted

        EXEC('CREATE TABLE ' + @ClientName + '(
        [PrimaryKey] [decimal](18, 0) IDENTITY (1, 1) NOT NULL PRIMARY KEY CLUSTERED,
        [FieldName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
        [Source] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
        [Target] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
        [Steps] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL )')

        I'm assuming that your field name is ClientName. I'm not sure what +'_ParseTable' is. If it is the field name, then just put ClientName_ParseTable in the select. Be careful with EXEC() - it opens a door to malicious code. ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

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

        Thanks ! I found another solution using sp_sqlexecute() but this one is more elligant.

        E 1 Reply Last reply
        0
        • G Guinness4Strength

          Thanks ! I found another solution using sp_sqlexecute() but this one is more elligant.

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          Glad it helped. Here is some info on sp_sqlexec. SQL Books Online SQL Server 6.x sp_sqlexec provided a convenient way for SQL Server database clients and servers to send a language statement of any format to an Open Data Services server application. SQL Server 2000 Removed; no longer available. Remove all references to sp_sqlexec. ---------- There go my people. I must find out where they are going so I can lead them. - Alexander Ledru-Rollin

          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