473,419 Members | 4,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

How do I insert data into two related tables in SQL Server 2008 database

Hi,
I would like to insert data into two related tables in a SQL Server 2008 database. I created the following stored procedure but it has a problem. When I add a record, the State table gets updated for every entry. I would like it to be normalized properly. My question is how do I check the State table to see if the record exist and add it only if it is needed. Any assistance will be greatly appreciated!

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE CustomerInsert
  2.  
  3.     (
  4.     @Company nvarchar(50)= NULL,
  5.     @CustomerName nvarchar(50),
  6.     @Phone nchar(12)= NULL,
  7.     @Address1 nvarchar(50),
  8.     @Address2 nvarchar(50)= NULL,
  9.     @City nvarchar(50),
  10.     @State nchar(2)= NULL,
  11.     @Zipcode nchar(10)= NULL
  12.     )
  13.  
  14. AS
  15.     /*SET NOCOUNT ON*/
  16.  
  17.     DECLARE @State_ID int
  18.  
  19.     INSERT INTO State (City, State, Zipcode)
  20.     VALUES (@City, @State, @Zipcode)
  21.  
  22.     SELECT @State_ID=@@IDENTITY
  23.  
  24.     INSERT INTO Customers (Company, CustomerName, Phone, Address1, Address2, State_ID)
  25.     VALUES (@Company, @CustomerName, @Phone, @Address1, @Address2, @State_ID) 
  26. RETURN
  27.  
May 19 '10 #1
3 5273
debasisdas
8,127 Expert 4TB
For your case the two table in questions must be related.
One must be the parent and the other child.
As per the rule to create a record in child table there must be a parent record in the parent table to maintain REFERENTIAL INTEGRITY.
So before inserting a new record to the child table check if it has a parent record.
If it exists insert into child table
else insert a record into the parent table first and then into the child table.
May 19 '10 #2
Hi,

The two tables are related. The State table is the parent with a primary key on the ID column (not shown in the example) and the Customer table has a foreign key Customer.State_ID. Both tables are set to Cascading update and delete.
May 19 '10 #3
ck9663
2,878 Expert 2GB
Why do you need to insert table in State table for every insert in Customer Table? Shouldn't your state table be a fix dimension table? You can get that in the net.

Happy Coding!!!

~~ CK
May 20 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Santo Santis | last post by:
How can I make an .exe file that can insert data automatically in a SQLServer database, I can do it in C, but how can I connect to SQLServer and execute a query. All data that I have to insert...
2
by: Ruslan Shlain | last post by:
I have looked high and low and i cant get anything to work. I have a dataset that i fill with data from text file. I need to populate my SQL Server 2000 database with data in that dataset. Dataset...
3
by: c676228 | last post by:
Hi all, I think I have data type problem when I tried to insert data into sql server 200. The erroe message is like this: system.invalidcastexception data type DFlage char(1) in sql 2000, DMI...
0
by: avi | last post by:
how to insert data from textboxes to sql database
2
slapshock
by: slapshock | last post by:
hi everyone..... can u help me with my problem... i have a project now regarding on how to import the data from database to excel sheets....please give me the codes on how to do it in java... ...
1
by: dillipdhal | last post by:
Hi This is Dillip from HSBC Software,Hyd. Now I m working in a financial domain. Please help me in "How can I insert data from a datagridview to database in vb .net and also retrive the data "
5
by: narendrareddy | last post by:
Hi , assume a flat file name emp.txt contains the following data 10000100001 narendranathreddy tallapireddy 01-01-2007 male 10000100002 ravindranathreddy tallapireddy 01-01-2008 male...
6
by: AAaron123 | last post by:
I need to move a database from sql server 2008 to sql express 2008. Will backing up the sql server 2008 database to a file and then using that file to restore to sql express 2008 work? I don't...
1
by: likun12 | last post by:
i develope a barcode program from that barcode is generated.but i just dont know how to insert data into sqlserver from the barcode reader.
0
by: LitaOsiris | last post by:
Hi everyone, I'm doing a website in JSP. I'm using Tomcat 6 and have gotten the site talking to the backend file. However, when I go to add in a database connection, I have problems. This is my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.