Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2008, 01:48 AM
Authorized User
 
Join Date: Aug 2006
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default create a file name using auto number method, MS SQ

hello there. i need some idea. coz i duno how to start my searching.
i m using asp + ms sql and wan to do a function like:

i wan to generate a filename using tis formula:
the current date +underscore+get the last generate number from the DB+ any characters(autogenerate) ==> then insert into another column such as filename

09062008_123456abc.jpg?


* red color =date
* purple color=number from the last record in DB
* orange color= any characters(autogenerate)

can some one guide me how to do a searching? or how is the concept? thank you very much

 
Old June 9th, 2008, 01:42 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I think the best way to do this is in steps:

(1) Create your table with an IDENTITY column (that is, an autonumber column). So let's say you have these fields in your table:
    CREATE TABLE demo (
        id INT IDENTITY,
        filename VARCHAR(30) NULL,
        whatever INT,
        ... other fields ...
        )

(2) Insert the new record, *without* specifying the file name
    INSERT INTO demo ( whatever, ... ) VALUES( 777, ... )

(3) Then perform an UPDATE query to put the filename in place:
    UPDATE demo
    SET filename = CONVERT(VARCHAR(10),Year(getDate())*10000+Month(ge tDate())*100+Day(getDate())
                 + '_'
                 + CONVERT(VARCHAR(20),@@IDENTITY)
                 + '.jpg'

If you do this all in a Stored Procedure, then you can return the new filename. So:

Code:
CREATE PROCEDURE getFileNameForNewFile( 
     @something INT,
     ...any other parameters...
     )
AS
DECLARE @newid INT
DECLARE @daynumber INT
DECLARE @fiilename VARCHAR(50)

SET NOCOUNT ON -- ensure intermediate results not seen

-- add the record, without the filename
INSERT INTO demo ( whatever, ... ) VALUES( @something, ... )

-- get the id value just added
SELECT @newid = @@IDENTITY

-- see remarks below
SET @daynumber = Year(getDate())*10000+Month(getDate())*100+Day(getDate()
-- so use daynumber and newid to create the new filename
SET @filename = = CONVERT(VARCHAR(10),@daynumber)
              + '_' 
              + CONVERT(VARCHAR(20),@newid)
              + '.jpg'
-- and change the record in the db so the filename is what we just created
UPDATE demo SET filename = @filename WHERE id = @newid
-- and now we *WANT* the caller to see ...
SET NOCOUNT OFF
SELECT @filename ' ... what we return as the new filename
*UNTESTED*!!! Off the top of my head.

Remarks: I changed you 09062008 to 20080609. Reason: Now your filenames will "sort" correctly when you do
     ORDER BY filename
As your code was written,
     09062008
would come *before*
     03152009
By using YYYYMMDD pattern, the names will just naturally be correctly ordered!
 
Old June 9th, 2008, 02:53 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I missed your requirement for the "abc" characters in there.

Any *REAL* reason for them??? The name is guaranteed to be unique just because of the IDENTITY value being part of the name, so why do you need "abc"???

IN any case, I'd probably just pass them INTO the Stored Proc rather than messing with generating them in T-SQL code. Just pass them as, say, the first parameter to the SP.





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with Auto Number Brendan Bartley Access 6 August 14th, 2006 10:13 AM
auto generate an number utarian Access 2 March 28th, 2005 02:24 AM
[b]Auto Number[/b] vanjamier Classic ASP Databases 1 November 18th, 2004 03:44 AM
Auto number yami56 Access 1 August 20th, 2004 11:24 AM
Auto number Sheri Dreamweaver (all versions) 0 December 22nd, 2003 03:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.