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!
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!
Code:
CREATE PROCEDURE CustomerInsert ( @Company nvarchar(50)= NULL, @CustomerName nvarchar(50), @Phone nchar(12)= NULL, @Address1 nvarchar(50), @Address2 nvarchar(50)= NULL, @City nvarchar(50), @State nchar(2)= NULL, @Zipcode nchar(10)= NULL ) AS /*SET NOCOUNT ON*/ DECLARE @State_ID int INSERT INTO State (City, State, Zipcode) VALUES (@City, @State, @Zipcode) SELECT @State_ID=@@IDENTITY INSERT INTO Customers (Company, CustomerName, Phone, Address1, Address2, State_ID) VALUES (@Company, @CustomerName, @Phone, @Address1, @Address2, @State_ID) RETURN
Comment