Home Forums SQL Server 7,2000 T-SQL How using SQL to generate random string? RE: How using SQL to generate random string?

  • Sharul Nizam

    Is your question about how to use that fantastic function developed by

    Lynn Pettis ? Assuming it is here is a start .

    If it is to insure the string is unique define your table with a unique index for example:

    CREATE TABLE [dbo].[tStrRmdm](

    [BoardCd] [varchar](6) NOT NULL

    ) ON [PRIMARY]

    END

    GO

    SET ANSI_PADDING OFF

    GO

    IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tStrRmdm]') AND name = N'IX_tStrRmdm')

    CREATE UNIQUE NONCLUSTERED INDEX [IX_tStrRmdm] ON [dbo].[tStrRmdm]

    (

    [BoardCd] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    Then you can execute Lynn Pettis function multiple times to insert the generated random string into the table. If the generated string is not unique an error message will be returned and the "duplicate" will not be inserted into the table.

    If you do not know what a Tally table is read Jeff Moden's article on SSC at:

    http://www.sqlservercentral.com/articles/T-SQL/62867/

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]