Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar situation but was generating the string in the code then sending it to the DB. Of course this is used on a very small table so I was not to worried about speed. Thought it might help, though it assumes you are using c#.</p> <pre><code> /// Generates a string and checks for existance /// &lt;returns&gt;Non-existant string as ID&lt;/returns&gt; public static string GetRandomNumbers(int numChars, string Type) { string result = string.Empty; bool isUnique = false; while (!isUnique) { //Build the string result = MakeID(numChars); //Check if unsued isUnique = GetValueExists(result, Type); } return result; } /// Builds the string public static string MakeID(int numChars) { string random = string.Empty; string[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; Random rnd = new Random(); for (int i = 0; i &lt; numChars; i++) { random += chars[rnd.Next(0, 35)]; } return random; } /// Checks database tables based on type for existance, if exists then retry /// &lt;returns&gt;true or false&lt;/returns&gt; private static bool GetValueExists(string value, string Type) { bool result = false; string sql = ""; if (Type == "URL") { sql = string.Format(@"IF EXISTS (SELECT COUNT(1) FROM myTable WHERE uniqueString = '{0}') BEGIN SELECT 1 END ELSE BEGIN SELECT 0 END ", value); } //query the DB to see if it's in use result = //ExecuteSQL return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload