Results 1 to 7 of 7

Thread: TSQL and Autogenerate **SOLVED**

  1. #1

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Resolved TSQL and Autogenerate **SOLVED**

    Hi all, I am pretty new to that TSQL thingy. Herers one thing I just cant seem to find an answer on the net!
    I use MS SQL Server 2000.
    This is what I do to create a new Table:

    Code:
    CREATE TABLE blah
    (
    blahId integer Primary Key Not Null,
    myText varchar(50)
    )
    Go
    This works fine. But I want to tell the SQLServer that the blahId will be autogenerated by the Database. What would I need to include in my script. I tried generate and autogenerate but both dont seem to work!

    Also how would my insert Statement look like.
    Usually you would like this:

    INSERT INTO blah VALUES (NULL,'someText')

    Will that still work?

    Thanks,

    Stephan
    Last edited by Sgt-Peppa; Jun 23rd, 2005 at 11:35 AM.
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  2. #2
    Addicted Member
    Join Date
    Jun 2005
    Posts
    243

    Re: TSQL and Autogenerate

    I don't know anything about the scripting deal, but I can tell you that the insert statement won't work.

    You set your blahID as the primary key, not null. And you are trying to set your PK as a null value, or at least a non-integer from what you typed. So you can't do that.

    The blahID has to have an integer value in it for the insert statement to work.

    -Side note:

    And I believe you would just need:
    insert into blah(mytext)
    values('some text value')

    If you get your PK field to autoincrement, because the blahID would automatically insert a new value upon insert of the mytext field.

  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: TSQL and Autogenerate

    Make your blahID field an Identity column:
    Code:
    CREATE TABLE blah
    (
    blahId integer Identity(1, 1) Primary Key Not Null,
    myText varchar(50)
    )
    Go
    Then you just use your other fields for insert:
    Code:
    Insert Into blah (myText) Values ('myText')
    blahId will automatically be assigned the next number in the sequence.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: TSQL and Autogenerate

    Just to expand a little regarding Identity(1, 1) .....
    The first 1 is the seed. This sets what the next number in the sequence will be. Typicaly this is 1, so that the first item added gets the number 1.
    The second one is the incrementor. This is added to the last number to get the next one in the sequence.

    So, Identity(1, 1) results in 1,2,3,4,5..... while Identity(10, 1) gives you 10,11,12,13,14..... and Identity(10, 10) gives you 10,20,30,40,50, and so on.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: TSQL and Autogenerate **SOLVED**

    Thanks to all of you! I am soooo bad at this SQl Server stuff!
    Saved my b....... tonight!

    Thanks again,

    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: TSQL and Autogenerate **SOLVED**

    I am so happy to see another person who likes to script an object...

    We do all our database work with scripts - store the scripts in source safe - and can easily create a new customer with these scripts...

    At any rate - here's a version of a script like you asked about...

    Code:
    Use AcctFiles
    GO
    DROP TABLE PayDist_T
    Go
    
    CREATE TABLE PayDist_T
    (PayEntry	int		not null identity
    ,PayStat	varchar(1)	null
    ,PayFiscalYr	varchar(5)	null
    ,PayDate	varchar(9)	null
    ,PayAcct	varchar(47)	null
    ,PayTDate	varchar(14)	null
    ,PayAmount	varchar(16)	null
    ,PayDesc	varchar(26)	null
    ,TDate		datetime
    ,constraint	    PKPayDist
    		    PRIMARY KEY (PayEntry)
    )
    GO
    We prefer to put our index constraints at the bottom of the CREATE TABLE - so we can name them specifically. That way when an error is returned by ADO we know what caused it - such as PKPayDist, or for a foreign key, FKPayDistFiscalYr. These names mean more to the client program then the odd names chosen by SQL...

    BTW - this table is used to store legacy data that is imported once a week - that's why the odd datatypes for things like dates - pay no attention to that!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: TSQL and Autogenerate **SOLVED**

    Quote Originally Posted by szlamany
    I am so happy to see another person who likes to script an object...

    We do all our database work with scripts - store the scripts in source safe - and can easily create a new customer with these scripts...

    [snip]...[/snip]

    We prefer to put our index constraints at the bottom of the CREATE TABLE - so we can name them specifically. That way when an error is returned by ADO we know what caused it - such as PKPayDist, or for a foreign key, FKPayDistFiscalYr. These names mean more to the client program then the odd names chosen by SQL...
    That's the way we do it too.... and quite frankly I (or the rest of us here) wouldn't have it any other way. It's just sooo much easier.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width