Adding a default unqiue value into a table

  • I'm tring to import data from one table to another.

    INSERT INTO Table2

       SELECT NULL AS Username,

          t1.Foreame,

          t1.Surname,

       FROM Table1 AS t1

    But I want a UDF or Trigger to create the default value for the new field (username). The rule for creating this default value is to be:

    First charcater of the forename + the surname (max 20 chars) and if the username is not unique then append a sequential number to the end. Example:

    jsmith

    jsmith1

    jsmith2

    Can anyone help?

     

    TIA

    David

  • This was removed by the editor as SPAM

  • Hi,

    May be You could try like this.

    DECLARE @defaultString varchar(25)

    DECLARE @countValue int

    -- you need to keep this inside a loop.

    -- I do not know how to keep it.

    SELECT @defaultString=left(forename+surname,20) from table1

    SELECT @countValue=Count(*) from table1 where defaultColumnValue=@defaultString

    if @countValue = 0 then

    insert into table1(default_columnname)

    values(@defaultString)

    else

    SET @countValue=@countValue+1

    insert into table1(default_columnname)

    values(@defaultString+@countValue)

    SET @countValue=0

    Thanks,

    Sridhar!!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply