473,800 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored Proc To Copy Unnormalized to Normalized Table

I have a "source" table that is being populated by a DTS bulk import
of a text file. I need to scrub the source table after the import
step by running appropriate stored proc(s) to copy the source data to
2 normalized tables. The problem is that table "Companies" needs to
be populated first in order to generate the Identity ID and then use
that as the foreign key in the other table.

Here is the DDL:

CREATE TABLE [dbo].[OriginalList] (
[FirstName] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[LastName] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[Company] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[Addr1] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[City] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[State] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[Zip] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[Phone] [varchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Companies] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[CompanyLocation s] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CompanyID] [int] NOT NULL ,
[Addr1] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[City] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[State] [varchar] (2) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[Zip] [varchar] (10) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[Phone] [varchar] (14) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
) ON [PRIMARY]
GO
This is the stored proc I have at this time that does NOT work. It
uses the last Company insert for all the CompanyLocation s which is not
correct.

CREATE PROCEDURE DataScrubSP AS
Begin Transaction
insert Companies (Name) select Company from OriginalList
IF @@Error <> 0
GOTO ErrorHandler

declare @COID int
select @COID=@@identit y

insert CompanyLocation s (CompanyID, Addr1, City, State, Zip) select
@COID, Addr1, City, State, Zip from OriginalList
IF @@Error <> 0
GOTO ErrorHandler

COMMIT TRANSACTION

ErrorHandler:
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
RETURN
GO

Thanks for any help.

Alex.
Jul 20 '05 #1
3 3261
On 9 Nov 2004 08:57:44 -0800, Alex wrote:
I have a "source" table that is being populated by a DTS bulk import
of a text file. I need to scrub the source table after the import
step by running appropriate stored proc(s) to copy the source data to
2 normalized tables. The problem is that table "Companies" needs to
be populated first in order to generate the Identity ID and then use
that as the foreign key in the other table.
(snip DDL)
This is the stored proc I have at this time that does NOT work. It
uses the last Company insert for all the CompanyLocation s which is not
correct.


(snip code)

Hi Alex,

First some comments on the DDL. OriginalList is obviously some staging
table so I can understand the funny datatypes, but are you really sure
that all these columns can be NULL? And that no column (or even
combination of columns) can be used in a PRIMARY KEY or UNIQUE constraint?

In the Companies table, you forgot to include a UNIQUE constraint on the
Name column. Your company names are unique, aren't they? If not, you
should of course not add the UNIQUE constraint on that column only, but
add more columns, to ensure that you'll ahve a natural, verifiable key in
addition to the convenient but meaningless identity column. Remember:
without verifiable key, you'll never be able to know if the below example
data is correct or a result of udplicated data entry:
ID | Name
----+------------
17 | Microsoft
36 | Microsoft

In the CompanyLocation s table, you'll gain a few bytes, a little bit of
speed and improved self-documenting if you change State to char(2). Also,
I'm surprised that you allow all columns to be NULL - most forms I have to
fill out require me to supply at least address, city, state (if US) or
country (if not US) and ZIP. Only phone is often optional.
Now, on to your question. If you have a real strong urge to use @@IDENTITY
or SCOPE_IDENTITY (), then you'll have to use a cursor or another way to
loop through the rows in OriginalList and process them one by one. But
that is not needed at all - as long as you have a good natural key (and
you always should have!), there are other ways to find the IDENTITY value
of a row: by using the natural key.

Try this snippet of (untested) code. My assumption is that Companies.Name
is indeed unique - if it isn't, you'll have to adapt the code (and your
tables).

CREATE PROCEDURE DataScrubSP AS
BEGIN TRANSACTION
-- Insert all companies from OriginalList
INSERT Companies (Name)
SELECT Company
FROM OriginalList
IF @@Error <> 0
GOTO ErrorHandler

-- Now, insert all companylocation s.
-- Use companyname to find the assigned ID value.
INSERT CompanyLocation s (CompanyID, Addr1, City, State, Zip)
SELECT c.ID, o.Addr1, o.City, o.State, o.Zip
FROM OriginalList AS o
INNER JOIN Companies AS c
ON c.Name = o.Company
IF @@Error <> 0
GOTO ErrorHandler

COMMIT TRANSACTION

ErrorHandler:
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
RETURN
GO
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
Alex (hf*****@yahoo. com) writes:
I have a "source" table that is being populated by a DTS bulk import
of a text file. I need to scrub the source table after the import
step by running appropriate stored proc(s) to copy the source data to
2 normalized tables. The problem is that table "Companies" needs to
be populated first in order to generate the Identity ID and then use
that as the foreign key in the other table.
...
This is the stored proc I have at this time that does NOT work. It
uses the last Company insert for all the CompanyLocation s which is not
correct.


Remove the IDENTITY property on Companies, and roll your own:

CREATE PROCEDURE DataScrubSP AS

CREATE TABLE #temp (ident int IDENTITY,
name varchar(50) NOT NULL)

insert #temp (name) select Company from OriginalList
IF @@Error <> 0
GOTO ErrorHandler

Begin Transaction

declare @first_id int
select @first_id = colaesce(MAX(ID ), 0) FROM Companies (UPDLOCK)

insert Companies(ID, name)
SELECT @first_id + ident, name FROM #temp
IF @@Error <> 0
GOTO ErrorHandler

insert CompanyLocation s (CompanyID, Addr1, City, State, Zip)
select c.ID, o.Addr1, o.City, o.State, o.Zip
from OriginalList o
Join Companies C ON o.Company = C.name
IF @@Error <> 0
GOTO ErrorHandler

Actually, this example you don't even need that temp table or
@first_id. Or even to drop the IDENTITY property on Companies.
All you need the join in the INSERT statement. But I suspect
that the due to the shrubbing there is some more complexity, so
I included that technique.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Thanks Hugo. I implemented the suggestion and it worked.

Alex.
"Hugo Kornelis" <hugo@pe_NO_rFa ct.in_SPAM_fo> wrote in message
news:rk******** *************** *********@4ax.c om...
On 9 Nov 2004 08:57:44 -0800, Alex wrote:
I have a "source" table that is being populated by a DTS bulk import
of a text file. I need to scrub the source table after the import
step by running appropriate stored proc(s) to copy the source data to
2 normalized tables. The problem is that table "Companies" needs to
be populated first in order to generate the Identity ID and then use
that as the foreign key in the other table.


(snip DDL)
This is the stored proc I have at this time that does NOT work. It
uses the last Company insert for all the CompanyLocation s which is not
correct.


(snip code)

Hi Alex,

First some comments on the DDL. OriginalList is obviously some staging
table so I can understand the funny datatypes, but are you really sure
that all these columns can be NULL? And that no column (or even
combination of columns) can be used in a PRIMARY KEY or UNIQUE constraint?

In the Companies table, you forgot to include a UNIQUE constraint on the
Name column. Your company names are unique, aren't they? If not, you
should of course not add the UNIQUE constraint on that column only, but
add more columns, to ensure that you'll ahve a natural, verifiable key in
addition to the convenient but meaningless identity column. Remember:
without verifiable key, you'll never be able to know if the below example
data is correct or a result of udplicated data entry:
ID | Name
----+------------
17 | Microsoft
36 | Microsoft

In the CompanyLocation s table, you'll gain a few bytes, a little bit of
speed and improved self-documenting if you change State to char(2). Also,
I'm surprised that you allow all columns to be NULL - most forms I have to
fill out require me to supply at least address, city, state (if US) or
country (if not US) and ZIP. Only phone is often optional.
Now, on to your question. If you have a real strong urge to use @@IDENTITY
or SCOPE_IDENTITY (), then you'll have to use a cursor or another way to
loop through the rows in OriginalList and process them one by one. But
that is not needed at all - as long as you have a good natural key (and
you always should have!), there are other ways to find the IDENTITY value
of a row: by using the natural key.

Try this snippet of (untested) code. My assumption is that Companies.Name
is indeed unique - if it isn't, you'll have to adapt the code (and your
tables).

CREATE PROCEDURE DataScrubSP AS
BEGIN TRANSACTION
-- Insert all companies from OriginalList
INSERT Companies (Name)
SELECT Company
FROM OriginalList
IF @@Error <> 0
GOTO ErrorHandler

-- Now, insert all companylocation s.
-- Use companyname to find the assigned ID value.
INSERT CompanyLocation s (CompanyID, Addr1, City, State, Zip)
SELECT c.ID, o.Addr1, o.City, o.State, o.Zip
FROM OriginalList AS o
INNER JOIN Companies AS c
ON c.Name = o.Company
IF @@Error <> 0
GOTO ErrorHandler

COMMIT TRANSACTION

ErrorHandler:
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
RETURN
GO
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
28069
by: June Moore | last post by:
Hi all, I have a stored procedure that return a resultset e.g. stored proc: get_employee_details select emp_id, emp_name, emp_salary, emp_position from empoloyee I would like to write another stored procedure that executes the above stored procedure - returning the same number of records but it will only show 2 columns
2
5127
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
0
7150
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE TRIGGER statement.) I've also defined a SQL stored proc, and the trigger is set to call this SP. I've posted the simplified source below. I can manually call the stored proc, and the external trigger is created without any errors. However, when I do...
45
3419
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
3
15811
by: mandible | last post by:
I'm trying to call one stored procedure inside another. I was wondering if this is possible Some ideas I was toying with is putting the first stored procedure inside of a temp table but haven't been able to get this idea to work.
3
6873
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these procs Repro --*********************************** use pubs go CREATE PROCEDURE Test @percentage int
7
8564
by: ashtek | last post by:
Hi, I have a generic function that executes a stored procedure & returns a data table. Code: === public static DataTable ExecuteStoredProcedure(string strProc,SqlParameter paramArray) { SqlCommand command = new SqlCommand(strProc, connection); command.CommandType = CommandType.StoredProcedure;
4
1869
by: davinski | last post by:
Hello, it's been a while since my last post, hope everyone is fine :P I'm stuck with what seems to be a simple task, but I'm getting confused on how to complete this. Basically, I have been given a stored procedure which nests itself within itself and uses a temporary table to store the data while writing. The nested stored procedure is used so that it can output the data rows in a db table into an organized tree like Example 1 1.0...
0
1990
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the contents of which comprise the call to a stored proc
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.