Empty a table before using DTS to append records?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Larry Rekow

    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."
  • Simon Hayes

    #2
    Re: Empty a table before using DTS to append records?


    "Larry Rekow" <larry@netgeexd otcom> wrote in message
    news:2gjej0hqag f196sp7ec3gn1uf 96don3hgv@4ax.c om...[color=blue]
    > 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."[/color]

    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


    Comment

    • Larry Rekow

      #3
      Re: Empty a table before using DTS to append records?

      On Thu, 2 Sep 2004 19:43:18 +0200, "Simon Hayes" <sql@hayes.ch > wrote:
      [color=blue]
      >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[/color]
      +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
      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."

      Comment

      • Erland Sommarskog

        #4
        Re: Empty a table before using DTS to append records?

        Larry Rekow (larry@netgeexd otcom) writes:[color=blue]
        > 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).[/color]

        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@sommarsk og.se

        Books Online for SQL Server SP3 at
        SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

        Comment

        • Larry Rekow

          #5
          Re: Empty a table before using DTS to append records?

          On Thu, 2 Sep 2004 22:00:18 +0000 (UTC), Erland Sommarskog
          <esquel@sommars kog.se> wrote:
          [color=blue]
          >Larry Rekow (larry@netgeexd otcom) writes:[color=green]
          >> 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).[/color]
          >
          >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?[/color]
          +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++
          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."

          Comment

          Working...