SQL Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Limno
    New Member
    • Apr 2008
    • 92

    SQL Query

    Anyone suggest me how to store data in SQL Server 2005 as

    Book - ID
    -------------
    Book - 1
    Book - 2
    .......
    Book - n

    Book - (1,2,3...) must be in auto increment value. Is there any query to insert this data, or any procedure. Suggest me the query or procedure to code, through SQL 2005. Please explain this carefully.....

    Thanks in Advance.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Yes.

    Create an identity column to handle your auto-increment column. Add a computed column to store your BOOK ID.


    --- CK

    Comment

    • Limno
      New Member
      • Apr 2008
      • 92

      #3
      Thank U,

      Auto-increment column, increase the value by 1,2, 3.... and Computed column->formula, Showing some error and i dont know what to give in formula column. Please explain me more with some example.
      I need to increment by Book-1,Book-2...Book-n, not 1,2,3... n.

      Comment

      • Limno
        New Member
        • Apr 2008
        • 92

        #4
        Originally posted by ck9663
        Yes.

        Create an identity column to handle your auto-increment column. Add a computed column to store your BOOK ID.


        --- CK
        Thank U,

        Auto-increment column, increase the value by 1,2, 3.... and Computed column->formula, Showing some error and i dont know what to give in formula column. Please explain me more with some example.
        I need to increment by Book-1,Book-2...Book-n, not 1,2,3... n.

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          Limno
          Why do you need to store it as
          Book-1
          Book-2
          ...

          It seems that having "Book-" in every row is a waste of space.

          If you have the table with an auto incrementing field as suggested by CK
          achieves what you are asking for.

          [code=sql]
          Table tblMyBooks
          ID TITLE ETC........
          1 ghfgteuj
          2 bjhja
          3 dsw
          [/code]

          If you really want it to be displayed as you describe then you do that in a query and base your front end off the query
          [code=sql]
          SELECT 'Book-' + convert(varchar (10),ID) as ID,Title,Etc... .
          FROM tblMyBooks
          [/code]
          The above query would return my sample data as
          [code=sql]
          Book-1 ghfgteuj
          Book-2 bjhja
          Book-3 dsw
          [/code]

          Alternatively, you could add the "Book-" to the ID in the front end application itself.

          Comment

          Working...