Solved

Insert stored procedure

Posted on 2011-02-15
5
233 Views
Last Modified: 2012-05-11
I have two tables.
First table has the ff. structure

tID   tcode >>First table

Second table has

tID Name address>>Second table

What Id like to do is write an INSERT statement that would insert the value 38 in the tcode field of the First table under the following condition.

1. first if the 2 tables link based on the tID field.
2. If link exits,  check in the first table if there is a value of 38 in the tCode. If not exist, INSERT 28 in the tCode otherwise,ingnore.

Essentliall the first table can have many codes under 1 tID like eg.

tID     tcode
222    20
222    21
222    38
222   29


Thanks

I included vb.net because, it may need a code to loop to each record in the table of the database.


0
Comment
Question by:zachvaldez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:PSSUser
ID: 34899026
If I've understood your question correctly I believe the below should do what you want:

INSERT INTO First_Table
SELECT Second_Table.tid, 38
FROM Second_Table
WHERE NOT EXISTS (SELECT T1.Tid FROM First_Table as T1 WHERE T1.Tid=Second_Table.Tid AND T1.TCode=38);
GO

Kind Regards
Chris
0
 

Author Comment

by:zachvaldez
ID: 34899436
I forgot to mention that there is datefield  value in 2nd table that I need to insert to 1st table tdatefield at the same time.
0
 
LVL 10

Expert Comment

by:PSSUser
ID: 34899504
Hi,

you should just be able to add the date field to the SELECT statement. I did miss the field list in the insert line and also, if the TID can occur more than once in Second_Table you may want a DISTINCT.

INSERT INTO First_Table (Tid, TCode, DateField)
SELECT DISTINCT Second_Table.tid, 38, Second_Table.DateField
FROM Second_Table
WHERE NOT EXISTS (SELECT T1.Tid FROM First_Table as T1 WHERE T1.Tid=Second_Table.Tid AND T1.TCode=38);
GO

If there are multiple entries for a TID in the second table, you can add conditions to select which record's date field is used or use a calculation such as MAX(DateField).

Kind Regards
Chris
0
 

Author Comment

by:zachvaldez
ID: 34913301
Question on insert procedure

Actually, the table structure for First table  are:
Comp
Emp
TID
TCode
DateField
NCode
DCode
CCode

Second_table structure

TID
TCode
DateCode

Im actually inserting 3  values from Second_table
So for the rest of the columns in First_table that are not in Second_table
Id like to insert the hard code  Values
Comp = 1
Emp = 30
NCode = 'LK'
DCode = 7
CCode = 78

How would I write the sql statement for the INSERT?
0
 
LVL 10

Accepted Solution

by:
PSSUser earned 400 total points
ID: 34914109
Hi,

It would depend whether the check/link on Tid between the 2 is enough or if it also needs to include the date field. I've assumed DateCode is just a normal date field the same as DateField. If one is a string you will want to add data type conversion.

Assuming I just need to check if any entry in the first table for the Tid has a TCode of 38:
INSERT INTO First_Table (Comp, Emp, Tid, TCode, DateField, NCode, DCode, CCode)
SELECT DISTINCT 1, 30, Second_Table.tid, 38, Second_Table.DateCode, 'LK', 7, 78
FROM Second_Table
WHERE NOT EXISTS (SELECT T1.Tid FROM First_Table as T1 WHERE T1.Tid=Second_Table.Tid AND T1.TCode=38);
GO

If you need to check if any entry in the first table for the Tid AND Date has a TCode of 38:
INSERT INTO First_Table (Comp, Emp, Tid, TCode, DateField, NCode, DCode, CCode)
SELECT DISTINCT 1, 30, Second_Table.tid, 38, Second_Table.DateCode, 'LK', 7, 78
FROM Second_Table
WHERE NOT EXISTS (SELECT T1.Tid FROM First_Table as T1 WHERE T1.Tid=Second_Table.Tid AND T1.TCode=38 AND T1.DateField=Second_Table.DateCode);
GO

Kind Regards,
Chris
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question