Solved

Insert stored procedure

Posted on 2011-02-15
5
229 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
  • 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now