Solved

Insert stored procedure

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

22 Experts available now in Live!

Get 1:1 Help Now