Insert into a table when new record

  • Hello,

    I would need help from a programming front...

    I guess this is probably done via trigger but not sure how to start.

    When table1 gets a new record added , only certain fields from table1 records would need to be inserted into table2

    Many thx

  • You could do it via trigger FOR INSERT or as part of the procedure used to insert the row using the OUTPUT clause.

    However, if you're just copying all the rows (with selected columns) into a new table, why do you need it at all? Couldn't you use a view or the same table?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • It would help to see what you are working with not just this vague decription.

    Please read the first article I reference below in my signature block regarding asking for help. It will walk you through the what you need to post and how to do it to get the best answers possible.

  • I apologize for the vagueness 🙂

    What I have is table1 which has been imported externally with 4 fields including an (AccountDescription) field

    srvnameAccountAccountDescriptionDATE_CREATION

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

    I need to be able to copy only 3 fields from another similar table whenever a new record is added as less the (AccountDescription) ,the data in this field is manually entered by me

    srvnameAccount DATE_CREATION

    I hope it's clearer

  • johnnyrmtl (4/5/2013)


    I apologize for the vagueness 🙂

    What I have is table1 which has been imported externally with 4 fields including an (AccountDescription) field

    srvnameAccountAccountDescriptionDATE_CREATION

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

    I need to be able to copy only 3 fields from another similar table whenever a new record is added as less the (AccountDescription) ,the data in this field is manually entered by me

    srvnameAccount DATE_CREATION

    I hope it's clearer

    I will go with no. At least to me it is much less clear than it was previously. ddl for the two tables and an explanation would go a long way here.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I got more confused.

    Could you give an example of what you have and what you need to get? Avoid using "these table", "another table", instead of that, you should name them and define them with proper DDL as shown in the article mentioned by Lynn. You don't have to use the actual names and you can change the information to mantain it confidential.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • johnnyrmtl (4/5/2013)


    I apologize for the vagueness 🙂

    What I have is table1 which has been imported externally with 4 fields including an (AccountDescription) field

    srvnameAccountAccountDescriptionDATE_CREATION

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

    I need to be able to copy only 3 fields from another similar table whenever a new record is added as less the (AccountDescription) ,the data in this field is manually entered by me

    srvnameAccount DATE_CREATION

    I hope it's clearer

    Nope, still clear as mud.

    Again, please read the first article I reference below in my signature block regarding asking for help. It will walk you through what you need to post and how to do it in order to get the best possible answer(s) in return.

  • Maybe 3 times is a charm , let me try again 🙂

    I have a server accounts table called ALLDB_syslogins_Sql2008 whereas I gather information hourly from other servers though a agent job. This particular server accounts table does not have a account description field.

    CREATE TABLE [dbo].[ALLDB_syslogins_Sql2008](

    [srvname] [varchar](128) NOT NULL,

    [name] [varchar](128) NOT NULL,

    [DATE_CREATION] [datetime] NOT NULL

    ) ON [PRIMARY]

    srvname name DATE_CREATION

    server\PRODabc_prod 2012-12-17 13:57:38.057

    server\PRODtest1 2013-01-04 10:31:31.420

    server\PRODtest2 2013-01-15 11:00:08.270

    .....

    My question is that I have created another table identical to this one and imported it externally from an excel csv file with almost the same corresponding information along with an extra field.

    CREATE TABLE [dbo].[ALLDB_syslogins_Sql2008_with desc](

    [srvname] [varchar](128) NOT NULL,

    [name] [varchar](128) NOT NULL,

    [account description] ](128) NOT NULL,

    [DATE_CREATION] [datetime] NOT NULL

    ) ON [PRIMARY]

    srvname name account description DATE_CREATION

    server\PRODabc_prod play 2012-12-17 13:57:38.057

    server\PRODtest1 work 2013-01-04 10:31:31.420

    server\PRODtest2 home 2013-01-15 11:00:08.270

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

    I know what i'm asking is a bit strange but since I cannot get an account description from my original table ALLDB_syslogins_Sql2008 . Therefore, whenever a new record is created in ALLDB_syslogins_Sql2008 , i need to copy it over to ALLDB_syslogins_Sql2008_with_desc and I will manually add the data in the account_description field.

    So basically all I need to do is bring over all new records into this new table

    ALLDB_syslogins_Sql2008_with_desc

    I need a glass of water 😉

  • Dumb question here, why not just add the description column to the existing table (as a nullable column) and update it there as you get the description(s)? Why maintain duplicate data?

  • Lynn... thx for your patience 🙂

    Basically because a new table was already loaded and created with most account descriptions already

    I can probably do that but would have to match things up...but the powers that be here want a separate table.

Viewing 10 posts - 1 through 9 (of 9 total)

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