Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Importing to current table from Excel

Author  Topic 

bhman79
Starting Member

10 Posts

Posted - 2011-06-10 : 14:16:05
I'm still new to SQL and I'm trying to import data to 3 different tables. I'm using the import wizard in DTS. I'm ignoring my recordid and rowguid fields. To get the import to work I have to change the design of the tables by allowing the identity increment, import the data, then change the design back to continue allowing people to use the system. Otherwise I get errors about null in the recordid and rowguid fields. Is there a better way to do this???

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-10 : 14:37:04
Importing the data into a staging table and then moving it into the production table via a t-sql script would be a better alternative.
Go to Top of Page

bhman79
Starting Member

10 Posts

Posted - 2011-06-10 : 15:35:00
If my staging table was partsstaging and parts was the production table, what would the code be for my script?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-10 : 16:08:11
Here is an example. Before you use it, test it in a dev environment to see if it is doing what you need, and change as required.
-- Create tables
-- Parts table. It has an identity column and GUID column.
CREATE TABLE Parts(col1 INT, col2 INT, idCol INT NOT NULL IDENTITY(1,1), guidCol uniqueidentifier);

-- Staging table. It does not have the identity column and the GUID column.
--The structure of this table should match the data file.
CREATE TABLE PartsStaging(col1 INT, col2 INT);

-- Insert some data into the staging table.
INSERT INTO PartsStaging SELECT 1,10 UNION SELECT 2,11 UNION SELECT 3,12;

-- Script to insert the data from the staging table into the Parts table.
INSERT INTO
Parts (col1, col2, guidCol)
SELECT
col1,
col2,
NEWID()
FROM
PartsStaging;

-- cleanup staging table (or you will need to truncate it it before you do the next import).
TRUNCATE TABLE PartsStaging;

-- see what you have in the Parts table.
SELECT * FROM Parts
Go to Top of Page

bhman79
Starting Member

10 Posts

Posted - 2011-06-10 : 16:45:11
Awesome...Thanks ALOT!!!!!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-10 : 18:07:01
you are very welcome!
Go to Top of Page

bhman79
Starting Member

10 Posts

Posted - 2011-06-13 : 16:35:00
I've run into a problem. In order for this to work, I must change the recordid design so that identity of a field is yes. However it needs to be changed back to no in order for our front end program to function correctly. It also slows down the system to do this during the day. What script would change this to yes and then back to no at the end of my code?

Thanks
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-13 : 20:35:35
If your production table is not an identity column, then there is no need to change it to identity column to insert the data. You can add a column in the Staging table that is an identity column which would populate it. Then insert the results into the production table. What I mean is something like shown below. If this does not work for you, can you post the DDL for your production table?

-- Create tables
-- Parts table. It has an identity column and GUID column.
CREATE TABLE Parts(col1 INT, col2 INT, idCol INT NOT NULL , guidCol uniqueidentifier);

-- Staging table. It does not have the identity column and the GUID column.
--The structure of this table should match the data file.
CREATE TABLE PartsStaging(col1 INT, col2 INT);

-- Insert some data into the staging table.
INSERT INTO PartsStaging SELECT 1,10 UNION SELECT 2,11 UNION SELECT 3,12;

-- add the identity column in the staging table and set the starting id to
-- whatever you need it to be, for example, 1477
alter table PartsStaging add idCol int not null identity (1477,1);

-- Script to insert the data from the staging table into the Parts table.
INSERT INTO
Parts (col1, col2, idCol, guidCol)
SELECT
col1,
col2,
idCol,
NEWID()
FROM
PartsStaging;

-- cleanup staging table (or you will need to truncate it it before you do the next import).
DROP TABLE PartsStaging;

-- see what you have in the Parts table.
SELECT * FROM Parts
Go to Top of Page
   

- Advertisement -