Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Random String on Record Creation Expand / Collapse
Author
Message
Posted Saturday, April 4, 2009 5:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 16, 2011 8:25 AM
Points: 24, Visits: 54
I want to create a random string each time a new record is ceated in the database. I have the SQL code which will create the string but I am not sure how to make it execute and insert the value each time a new record is created.
Post #690526
Posted Saturday, April 4, 2009 5:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 12,917, Visits: 32,082
you'll need to make your code a function.
once it is a function, say udfRandomString()
you can create a table and set it's default value to the function.

you might run into problems if your script uses newid in it, since that's not allowed in a directly function;
you can get around that by using a view though
can we see your function?

here is a complete example, i'm using a view and really nice function(it uses a Tally table) that assumes 20 characters, you can change the values to what you need.
here's typical results:
tmpid	password               morestuff
1 BSLPYRZPOAH8LDCW19MJ one
2 T2OD2LLJC9IPWLNEOIJE two
3 C3U4KN5NHSXEIGOZ6BGH three

and the code:

Create View OneRandomString20
AS

with
a1 as (select 1 as N union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1),
a2 as (select
1 as N
from
a1 as a
cross join a1 as b),
a3 as (select
1 as N
from
a2 as a
cross join a2 as b),
a4 as (select
1 as N
from
a3 as a
cross join a2 as b),
Tally as (select
row_number() over (order by N) as N
from
a4)
, cteRandomString (
RandomString
) as (
select top (20)
substring(x,(abs(checksum(newid()))%36)+1,1)
from
Tally cross join (select x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') a
)
select
replace((select
',' + RandomString
from
cteRandomString
for xml path ('')),',','') AS Results;

GO
CREATE FUNCTION fn_RandomString()
returns varchar(20)
AS
BEGIN
Declare @results varchar(20)
SELECT top 1 @results = Results from OneRandomString20
return @results
END
GO
select dbo.fn_RandomString()

CREATE TABLE tmp(tmpid int identity(1,1) not null primary key,password varchar(20) default(dbo.fn_RandomString()),morestuff varchar(30) )
insert into tmp(morestuff)
select 'one' union all select 'two' union all select 'three'
select * from tmp



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #690527
Posted Saturday, April 4, 2009 6:47 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 16, 2011 8:25 AM
Points: 24, Visits: 54
Very nice sample. So now I have a function called 'fn_RandomString' and in the table field I set the 'Default Value for Binding' to 'fn_RandomString()'. But when records are inserted in the table the field is populated with the string 'fn_RandomString()' and not the random string. Where you think I went wrong?
Post #690528
Posted Saturday, April 4, 2009 6:55 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 16, 2011 8:25 AM
Points: 24, Visits: 54
I think I got it. I entered 'dbo.fn_RandomString()' and it worked.
Post #690529
Posted Saturday, April 4, 2009 7:25 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 12,917, Visits: 32,082
sorry about the naming thing...i wrote my comments, then added the code, change the name along the way in there....
glad it's working for you.

did you change it to be a different length than 20 characters? what are you using it for? ( i assumed a random password?)


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #690532
Posted Saturday, April 4, 2009 7:34 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 16, 2011 8:25 AM
Points: 24, Visits: 54
I changed the 20 to 64 and I am using this random string as a handshake token between two applications. My next task is to replicate this random string on the other application LDAP directory DB.
Post #690533
Posted Saturday, April 4, 2009 10:00 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:48 PM
Points: 23,397, Visits: 32,244
First, I was slightly confused, as the code Lowell posted uses a CTE and this is in a SQL Server 7, 2000 forum.

But, since it worked, I thought I'd throw something else out for you all. This code looks familiar, but I made a slight change.

First, I created a view that returns a NEWID as NewIdValue. I then use this view in my function where Lowell had the NEWID() function in his view. The difference, a single function that takes as a paramter the length of the random string to be returned.

create view dbo.MyNewID as
select newid() as NewIDValue;
go
create function dbo.ufn_RandomString(
@pStringLength int = 20
) returns varchar(max)
as begin
declare @RandomString varchar(max);
with
a1 as (select 1 as N union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1 union all
select 1),
a2 as (select
1 as N
from
a1 as a
cross join a1 as b),
a3 as (select
1 as N
from
a2 as a
cross join a2 as b),
a4 as (select
1 as N
from
a3 as a
cross join a2 as b),
Tally as (select
row_number() over (order by N) as N
from
a4)
, cteRandomString (
RandomString
) as (
select top (@pStringLength)
substring(x,(abs(checksum((select NewIDValue from MyNewID)))%36)+1,1)
from
Tally cross join (select x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') a
)
select @RandomString =
replace((select
',' + RandomString
from
cteRandomString
for xml path ('')),',','');
return (@RandomString);
end
GO




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #690545
Posted Monday, April 6, 2009 5:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 16, 2011 8:25 AM
Points: 24, Visits: 54
Lynn Pettis (4/4/2009)
First, I was slightly confused, as the code Lowell posted uses a CTE and this is in a SQL Server 7, 2000 forum.


My bad. I am using SQL 2005 and I posted the question in the wrong forum. The code provided by Lowell worked fine. Lynn, I will try your suggestion later today. Thank you both.
Post #690911
Posted Monday, April 6, 2009 6:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:27 PM
Points: 12,917, Visits: 32,082
I like Lynn's a lot more; having a view with just a newid in it is more useful, since i could use the same view for other purposes.

my code was a verbatim copy of one of her examples anyway, i just made it a function.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #690919
Posted Monday, April 6, 2009 6:23 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:48 PM
Points: 23,397, Visits: 32,244
Lowell (4/6/2009)
I like Lynn's a lot more; having a view with just a newid in it is more useful, since i could use the same view for other purposes.

my code was a verbatim copy of one of her examples anyway, i just made it a function.


Two things, Lowell. One, I thought the code looked familiar, and two, I'm a he. Don't worry, others here have mad the same mistake, and after many years I have learned to shrug it off.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #690937
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse