Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert into a table when new record


Insert into a table when new record

Author
Message
johnnyrmtl
johnnyrmtl
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 429
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
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8566 Visits: 18143
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24255 Visits: 37978
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.

Cool
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)
johnnyrmtl
johnnyrmtl
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 429
I apologize for the vagueness Smile

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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16632 Visits: 17024
johnnyrmtl (4/5/2013)
I apologize for the vagueness Smile

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)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8566 Visits: 18143
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24255 Visits: 37978
johnnyrmtl (4/5/2013)
I apologize for the vagueness Smile

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.

Cool
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)
johnnyrmtl
johnnyrmtl
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 429
Maybe 3 times is a charm , let me try again Smile

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 ;-)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24255 Visits: 37978
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?

Cool
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)
johnnyrmtl
johnnyrmtl
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 429
Lynn... thx for your patience Smile

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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search