Author |
Topic |
smithm55
Starting Member
9 Posts |
Posted - 2012-08-01 : 16:09:35
|
I'm trying to enhance a query used to build a nightly file of new student accounts in our school district. I have a table of 6-letter words, called password_list. I want my query to build a CSV file each night of new students that contains (A) basic demographic information collected from various tables, and (B) a password. I'd like to build a password for each student as follows:(1) Pick a random number, and use the password from that row in table password_list(2) Append to that a random 2-digit numberSo, some examples of passwords would be Rabbit24, Orange19, etc.Could this be done within a select query?I've tried to incorporate subqueries with some ideas to randomize things that I've found on other sites, such as:select top 1 password from password_list order by newid() andselect password from password_list where pwno=ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 1000 + 1 but I can't quite seal the deal.Any advice would be greatly appreciated!!Matt SmithInformation ServicesDeSoto County (FL) School District |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-08-01 : 16:21:28
|
What version of SQL are you using? maybe this will help?SELECT <Column List>, (select top 1 password from password_list order by newid()) + LEFT('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 100 + 1 AS VARCHAR(2)), 2) AS PasswordFROM <Student Info>... If not:http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2012-08-02 : 12:03:37
|
Lamprey, thanks much for the reply! I'm using SQL Server 2008 R2. Your code is really close, but apparently the "order by newid()" only seeds (if that's the right term) once at the beginning of the query. The code I used is as follows:SELECT first_name, (select top 1 password from password_list order by newid()) + right('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 99 + 1 AS VARCHAR(2)), 2) AS PasswordFROM student_info_core and the results I get are as follows:ABIGAIL quacks88BRANDON quacks21CAMERON quacks83CAPONE quacks48CHEYENNE quacks20CHLOE quacks84DAMARION quacks56DANIELLE quacks25DONTAVIAN quacks49 Every time I re-run the query, I get a different word, but it's always the same word for every record. We're close, but that's where I'm stuck! Anyone know how to make sure we get a new word each time? |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-08-02 : 12:46:01
|
Humm, I'm not seeing the same behavior:DECLARE @Student TABLE (Name VARCHAR(50))INSERT @Student (Name)VALUES('ABIGAIL'), ('BRANDON'), ('CAMERON'), ('CAPONE'), ('CHEYENNE'), ('CHLOE'), ('DAMARION'), ('DANIELLE'), ('DONTAVIAN')DECLARE @Password_list TABLE (Password VARCHAR(50))INSERT @Password_list (Password)VALUES('Orange'),('Rabbit'),('Quacks'),('Fudges'),('Happy') SELECT Name, (select top 1 password from @password_list order by newid()) + right('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 99 + 1 AS VARCHAR(2)), 2) AS PasswordFROM @student ResultsName PasswordABIGAIL Fudges46BRANDON Quacks35CAMERON Fudges50CAPONE Rabbit67CHEYENNE Quacks33CHLOE Happy84DAMARION Happy65DANIELLE Happy34DONTAVIAN Orange37 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-08-02 : 12:47:46
|
May try an OUTER APPLY?SELECT Name, A.PasswordFROM @student AS sOUTER APPLY( select top 1 password + right('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 99 + 1 AS VARCHAR(2)), 2) AS Password from @password_list order by newid() ) AS A |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2012-08-02 : 13:51:48
|
Lamprey, what version of SQL are you using? I'm getting syntax errors when I try to execute your code. Specifically, the table values from your first post, and the OUTER APPLY from the second post. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-08-02 : 13:56:06
|
I tried it on several versions of SQL: 2008, 2008 R2 and 2012. Is your Server perhaps running in a backwards compatable mode for an older version of SQL? I don't have a running version of 2005, but my first query should work on that too. |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2012-08-02 : 14:46:17
|
Strange. No weird back-compatible modes that I'm aware of. I'll go back to the drawing board for just a bit. Thanks for staying with this! |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2012-08-02 : 15:06:40
|
Very puzzling. I've tried on my 2005 and 2008 R2 servers.This statement:DECLARE @Password_list TABLE (Password VARCHAR(50))INSERT @Password_list (Password)VALUES('Orange') returns this:(1 row(s) affected) But as soon as I add a second record:DECLARE @Password_list TABLE (Password VARCHAR(50))INSERT @Password_list (Password)VALUES('Orange'),('Puppet') I get this:Server: Msg 170, Level 15, State 1, Line 4Line 4: Incorrect syntax near ','. |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2012-08-02 : 15:48:16
|
The version is indeed the culprit. I was working on different SQL Servers, but still connecting back to my database that's (gulp) SQL 2000 SP4. I guess I'd have to upgrade this server in order to get this code to work. |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2013-06-17 : 11:07:26
|
Lamprey, it looks like you're still around, so I wanted to resurrect this topic, if I may. I've upgraded my old server above to 2008. Let me ask a new question. If I copy and paste your code above, beginning with the DECLARE @Student TABLE..., everything works just as you described. I get a new word and number for every account, as I'd hoped.However, as soon as I convert these into physical tables, things break down for me again, and I get the same word on every line.For example, I'm curious if you would be willing to execute this code, and see if you get similar results:==========CREATE TABLE Student (Name VARCHAR(50))INSERT StudentVALUES('ABIGAIL'), ('BRANDON'), ('CAMERON'), ('CAPONE'), ('CHEYENNE'), ('CHLOE'), ('DAMARION'), ('DANIELLE'), ('DONTAVIAN')CREATE TABLE PasswordList (Password VARCHAR(50))INSERT PasswordList VALUES('Orange'),('Rabbit'),('Quacks'),('Fudges'),('Happy') SELECT Name, (select top 1 password from passwordlist order by newid()) + right('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 99 + 1 AS VARCHAR(2)), 2) AS PasswordFROM student===========Results:Name PasswordABIGAIL Happy33BRANDON Happy36CAMERON Happy20CAPONE Happy91CHEYENNE Happy72CHLOE Happy43DAMARION Happy76DANIELLE Happy16DONTAVIAN Happy41Thank you for any advice!! |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
smithm55
Starting Member
9 Posts |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-06-17 : 13:28:13
|
You might try something like an OUTTER APPLY. Although you need to corrilate it in some way, even falsely, in order to get the resutls you want. Here is one way to do it:SELECT Name, A.PasswordFROM student AS sOUTER APPLY( select top 1 password + right('0' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 99 + 1 AS VARCHAR(2)), 2) AS Password from passwordlist WHERE s.Name <> '' -- Something that wil never be true order by newid() ) AS A |
|
|
smithm55
Starting Member
9 Posts |
Posted - 2013-06-17 : 15:58:58
|
I just did a little preliminary testing, but I *think* this is going to work. THANKS Lamprey!! |
|
|
|