| Author |
Topic |
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-08 : 18:09:40
|
I have a code snippet for generating random password:declare @password varchar(8)set @password=''select @password=@password+char(n) from( select top 8 number as n from master..spt_values where type='p' and number between 48 and 122 order by newid()) as tselect @password as 'Password' Now, I have a column name: st_password in table: student_details in MS SQL 2000.I want to keep this random password generate code in my database as procedure or function and that should be directly linked with st_password column.How should I do this?I am very much new to MS SQL, so please help me!Daipayan |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-08 : 21:12:13
|
i think its better to make it a function CREATE FUNCTION GenerateRandomPassword()RETURNS varchar(8) ASdeclare @password varchar(8)set @password=''select @password=@password+char(n) from(select top 8 number as n from master..spt_values where type='p' and number between 48 and 122order by newid()) as treturn @passwordGO then make the column as calculated column likeCREATE TABLE student_details(....,st_password AS dbo.GenerateRandomPassword())or use an updateUPDATE student_detailsSET st_password =dbo.GenerateRandomPassword() |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2009-02-08 : 22:51:50
|
quote: Originally posted by visakh16 i think its better to make it a function...
You can't use newid within a function.CODO ERGO SUM |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-09 : 02:14:52
|
quote: Originally posted by visakh16 i think its better to make it a function CREATE FUNCTION GenerateRandomPassword()RETURNS varchar(8) ASdeclare @password varchar(8)set @password=''select @password=@password+char(n) from(select top 8 number as n from master..spt_values where type='p' and number between 48 and 122order by newid()) as treturn @passwordGO then make the column as calculated column likeCREATE TABLE student_details(....,st_password AS dbo.GenerateRandomPassword())or use an updateUPDATE student_detailsSET st_password =dbo.GenerateRandomPassword()
While creating the function, am getting following errors:Server: Msg 156, Level 15, State 1, Procedure GenerateRandomPassword, Line 5Incorrect syntax near the keyword 'declare'.Server: Msg 443, Level 16, State 1, Procedure GenerateRandomPassword, Line 11Invalid use of 'newid' within a function. Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 04:25:25
|
quote: Originally posted by Michael Valentine Jones
quote: Originally posted by visakh16 i think its better to make it a function...
You can't use newid within a function.CODO ERGO SUM
sorry...i didnt notice that newid()yup...its not possible to use it within function |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-09 : 05:10:38
|
quote: sorry...i didnt notice that newid()yup...its not possible to use it within function
Is there any other possibilities??Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 05:26:01
|
| the other possibility is what tara suggested. where you make it procedure and call it to update each record in table. othewise, include the logic inline with an update statement |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-09 : 10:10:59
|
quote: Originally posted by tkizer CREATE PROC SomeProcName(@password varchar(8) OUTPUT)set @password=''select @password=@password+char(n) from( select top 8 number as n from master..spt_values where type='p' and number between 48 and 122 order by newid()) as tGOTara Kizer
Am getting this error......while executing the ProcedureServer: Msg 156, Level 15, State 1, Procedure SomeProcName, Line 4Incorrect syntax near the keyword 'set'. Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 10:14:25
|
| [code]CREATE PROC SomeProcName(@password varchar(8) OUTPUT)ASset @password=''select @password=@password+char(n) from(select top 8 number as n from master..spt_values where type='p' and number between 48 and 122order by newid()) as tGO[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-09 : 10:15:42
|
| CREATE PROC SomeProcName(@password varchar(8) OUTPUT)ASset @password=''select @password=@password+char(n) from(select top 8 number as n from master..spt_values where type='p' and number between 48 and 122order by newid()) as tGOMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-09 : 10:16:08
|
MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 10:16:18
|
quote: Originally posted by madhivanan CREATE PROC SomeProcName(@password varchar(8) OUTPUT)ASset @password=''select @password=@password+char(n) from(select top 8 number as n from master..spt_values where type='p' and number between 48 and 122order by newid()) as tGOMadhivananFailing to plan is Planning to fail
|
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-09 : 11:03:01
|
| I stored the procedure in the name of RandomProcedure. Now, how should I link with st_password column in student_details table.I know, am asking very silly question, but I am very new to MS SQL 2000, just started learning this DBMS, so please guide me.@visakh16Also, Sir, u given me a solution, how to generate Auto ID year wise in following link:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119422The ID is generateing as per my needs, but I cannot make this ID column linked with others columns in another table, please help me in this part also.Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-09 : 11:15:32
|
just use something like:-DECLARE @pwd varchar(8),@ID intSELECT @ID=MIN(PK)FROM student_detailsWHILE @ID ISNOT NULLBEGINEXEC SomeProcName @pwd OUTPUTUPDATE student_detailsset st_password = @pwdWHERE PK=@IDSELECT @ID=MIN(PK)FROM student_detailsWHERE PK >@IDEND PK is primary key of student_details table |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-02-09 : 11:38:15
|
You may not be able to use newID in the function but you can do this1) Make a view withCREATE VIEW my_Rand AS SELECT newID() AS new_Id 2) You can then reference the view from the function and get your newId that way.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2009-02-09 : 11:55:58
|
| You cannot directly link a stored procedure to a table column, so you will have to generate the passwords with a stored procedure call with each insert.This thread has several more ideas for generating passwords:Generate Password Procedurehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=78859CODO ERGO SUM |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-02-10 : 09:07:29
|
| Thanks to all the peoples who help me in this query, thankz a lot!You people just ROCK!HATS OFF TO ALL OF YOU GUYS!Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 09:21:36
|
| you're welcome |
 |
|
|
|