Wednesday, February 15, 2012

Empty a table before using DTS to append records?

In Access I have a macro that, each night, takes a table with a
primary key defined in it, and deletes all the rows. Then it
imports/appends records from a fixed width text file. In this way,
since the table is not deleted and recreated, the primary key is kept
intact.

What would be the equivalent SQL method for doing this in an automated
way? I've tried letting DTS import the table from Access, but the
primary key is lost. Is there some way to "empty" a table instead of
dropping it, and then append new records so that the table will end up
having the primary key I want it to have?

Thanks.

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown.""Larry Rekow" <larry@.netgeexdotcom> wrote in message
news:2gjej0hqagf196sp7ec3gn1uf96don3hgv@.4ax.com...
> In Access I have a macro that, each night, takes a table with a
> primary key defined in it, and deletes all the rows. Then it
> imports/appends records from a fixed width text file. In this way,
> since the table is not deleted and recreated, the primary key is kept
> intact.
> What would be the equivalent SQL method for doing this in an automated
> way? I've tried letting DTS import the table from Access, but the
> primary key is lost. Is there some way to "empty" a table instead of
> dropping it, and then append new records so that the table will end up
> having the primary key I want it to have?
> Thanks.
> Larry
> - - - - - - - - - - - - - - - - - -
> "Forget it, Jake. It's Chinatown."

I don't really understand what you mean, but I guess that your primary key
column is an IDENTITY column, and you want to remove all data in the table
without resetting the identity value back to 1 (or whatever your seed was)?
The simple way is just this:

delete from dbo.MyTable

This can be slow on large tables with many rows, which is why TRUNCATE TABLE
is often used instead, although it will reset the identity seed.

If this isn't helpful, or if I didn't understand correctly, then you should
post some more detailed information, in particular the CREATE TABLE
statement for your target table (including constraints), and what the data
looks like that you want to import. It would also be good to know if you
want MSSQL to generate new identity values for your primary key, or if you
already have values in the source data which you want to keep.

Simon|||On Thu, 2 Sep 2004 19:43:18 +0200, "Simon Hayes" <sql@.hayes.ch> wrote:

>If this isn't helpful, or if I didn't understand correctly, then you should
>post some more detailed information, in particular the CREATE TABLE
>statement for your target table (including constraints), and what the data
>looks like that you want to import. It would also be good to know if you
>want MSSQL to generate new identity values for your primary key, or if you
>already have values in the source data which you want to keep.
>Simon
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
Already have values in the source data, a unique field called
"FILE_NUM".

The default statement that DTS creates for me starts out with:

CREATE TABLE [hoyt].[dbo].[IMP7TEXT] (
[IMP_ID] nvarchar (12) NULL,
[CUST_ID] nvarchar (12) NULL,
[FILE_NUM] nvarchar (8) NULL,
etc.....

Because I'm using this table in an ASP.Net website, I want to relate
it to other tables that also have the FILE_NUM values.

Would it be as simple as editing the CREATE TABLE statement to specify
that FILE_NUM is the primary key? (yes, i'm very new at MS SQL).

Thanks,

Larry

- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."|||Larry Rekow (larry@.netgeexdotcom) writes:
> The default statement that DTS creates for me starts out with:
> CREATE TABLE [hoyt].[dbo].[IMP7TEXT] (
> [IMP_ID] nvarchar (12) NULL,
> [CUST_ID] nvarchar (12) NULL,
> [FILE_NUM] nvarchar (8) NULL,
> etc.....
> Because I'm using this table in an ASP.Net website, I want to relate
> it to other tables that also have the FILE_NUM values.
> Would it be as simple as editing the CREATE TABLE statement to specify
> that FILE_NUM is the primary key? (yes, i'm very new at MS SQL).

Not really. First you would have to make the column NOT NULL before you
can make it a primary key. Then you probably would have to set up foriegn-
key relations as well. Then again, that depends on what you mean with
relate. I don't know ASP .Net, but may you are really talking data tables
in ADO .Net?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||On Thu, 2 Sep 2004 22:00:18 +0000 (UTC), Erland Sommarskog
<esquel@.sommarskog.se> wrote:

>Larry Rekow (larry@.netgeexdotcom) writes:
>> The default statement that DTS creates for me starts out with:
>>
>> CREATE TABLE [hoyt].[dbo].[IMP7TEXT] (
>> [IMP_ID] nvarchar (12) NULL,
>> [CUST_ID] nvarchar (12) NULL,
>> [FILE_NUM] nvarchar (8) NULL,
>> etc.....
>>
>> Because I'm using this table in an ASP.Net website, I want to relate
>> it to other tables that also have the FILE_NUM values.
>>
>> Would it be as simple as editing the CREATE TABLE statement to specify
>> that FILE_NUM is the primary key? (yes, i'm very new at MS SQL).
>Not really. First you would have to make the column NOT NULL before you
>can make it a primary key. Then you probably would have to set up foriegn-
>key relations as well. Then again, that depends on what you mean with
>relate. I don't know ASP .Net, but may you are really talking data tables
>in ADO .Net?
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
Thanks to you and Simon for responses and hints. Yes, after more
googling, reading, discovering, I made FILE_NUM NOT NULL and was able
to make it the primary key by editing the MAKE TABLE query DTS put
together; now looking into defining the foreign keys, etc.

The penny is starting to drop :)

Larry
- - - - - - - - - - - - - - - - - -
"Forget it, Jake. It's Chinatown."

No comments:

Post a Comment