Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Insert into a table when new record Expand / Collapse
Author
Message
Posted Friday, April 5, 2013 12:34 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 8:11 AM
Points: 75, Visits: 340
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
Post #1439422
Posted Friday, April 5, 2013 12:39 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:22 PM
Points: 3,325, Visits: 7,174
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1439426
Posted Friday, April 5, 2013 12:45 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 23,000, Visits: 31,482
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1439427
Posted Friday, April 5, 2013 12:59 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 8:11 AM
Points: 75, Visits: 340
I apologize for the vagueness :)

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

srvname Account AccountDescription DATE_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

srvname Account DATE_CREATION



I hope it's clearer
Post #1439434
Posted Friday, April 5, 2013 1:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 13,067, Visits: 11,903
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

srvname Account AccountDescription DATE_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

srvname Account 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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1439444
Posted Friday, April 5, 2013 1:15 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:22 PM
Points: 3,325, Visits: 7,174
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1439445
Posted Friday, April 5, 2013 1:17 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 23,000, Visits: 31,482
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

srvname Account AccountDescription DATE_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

srvname Account 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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1439446
Posted Friday, April 5, 2013 1:49 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 8:11 AM
Points: 75, Visits: 340
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\PROD abc_prod 2012-12-17 13:57:38.057
server\PROD test1 2013-01-04 10:31:31.420
server\PROD test2 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\PROD abc_prod play 2012-12-17 13:57:38.057
server\PROD test1 work 2013-01-04 10:31:31.420
server\PROD test2 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




Post #1439457
Posted Friday, April 5, 2013 1:53 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 23,000, Visits: 31,482
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1439458
Posted Monday, April 8, 2013 9:06 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 8:11 AM
Points: 75, Visits: 340
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.
Post #1439893
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse