Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Deezy1017
Starting Member
4 Posts |
Posted - 2013-11-20 : 01:51:38
|
I'm trying to chech a see if a record(password) of individual have a number in it if not delete the recordCREATE PROCEDURE CHECK_INDIVIDUAL( @username VARCHAR(50))ASBEGIN DECLARE @PWD VARCHAR(50) SET @PWD=(SELECT Password_1 FROM Individual WHERE Password_1 =@username) IF(@PWD NOT LIKE '%[0-9]%') BEGIN SELECT'Passowrd Valid' END ELSE BEGIN SELECT'Password must Contain a number!' //ERROR LINE DELETE Password_1 FROM Individual WHERE Username=@username SELECT'Please re-enter your passwrod' END ENDS.Davis |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-20 : 02:04:03
|
As per your explanation it should be thisCREATE PROCEDURE CHECK_INDIVIDUAL(@username VARCHAR(50))ASBEGINDECLARE @PWD VARCHAR(50) SET @PWD=(SELECT Password_1 FROM Individual WHERE Password_1 =@username)IF(@PWD NOT LIKE '%[^0-9]%')BEGINSELECT'Passowrd Valid'ENDELSEBEGINSELECT'Password must Contain a number!'//ERROR LINE DELETE Password_1 FROM Individual WHERE Username=@usernameSELECT'Please re-enter your passwrod' ENDEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Deezy1017
Starting Member
4 Posts |
Posted - 2013-11-21 : 02:12:39
|
okS.Davis |
|
|
Kristen
Test
22859 Posts |
Posted - 2013-12-03 : 11:34:55
|
quote: Originally posted by visakh16
IF(@PWD NOT LIKE '%[^0-9]%')
Should be:IF(@PWD NOT LIKE '%[0-9]%') ... invalid ...ELSE ... valid ... ??SET @PWD=(SELECT Password_1 FROM Individual WHERE Password_1 =@username) should probably be:SET @PWD=(SELECT Password_1 FROM Individual WHERE Username = @username) personally I would code it as follows:IF EXISTS( SELECT * FROM Individual WHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%')BEGIN SELECT 'Password Valid'ENDELSEBEGIN SELECT 'Password must Contain a number!'//ERROR LINE DELETE Password_1 FROM Individual WHERE Username=@username SELECT 'Please re-enter your password' END not sure your DELETE code is what you want. Do you want to delete the ROW from INDIVIDUAL table? or just set their password to NULL?Taking that into account I would actually code it as:UPDATE USET Password_1 = NULLFROM Individual AS UWHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%'IF @@ROWCOUNT = 0BEGIN SELECT 'Password Valid'ENDELSEBEGIN SELECT 'Password must Contain a number!' SELECT 'Please re-enter your password' END If you actually want to delete the row from Individual table then replace the UPDATE with a DELETEDELETE UFROM Individual AS UWHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%'IF @@ROWCOUNT = 0... this code also takes care of the case that @username has more than one record, where one, or more, of the records does not contain a digit. Even if Username column is unique I prefer this style of programming as it is "defensive" - e.g. if the UNIQUE constraint ever got dropped by accident, or ... whatever!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-03 : 23:54:46
|
quote: Originally posted by Kristen
quote: Originally posted by visakh16
IF(@PWD NOT LIKE '%[^0-9]%')
Should be:IF(@PWD NOT LIKE '%[0-9]%') ... invalid ...ELSE ... valid ... ??SET @PWD=(SELECT Password_1 FROM Individual WHERE Password_1 =@username) should probably be:SET @PWD=(SELECT Password_1 FROM Individual WHERE Username = @username) personally I would code it as follows:IF EXISTS( SELECT * FROM Individual WHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%')BEGIN SELECT 'Password Valid'ENDELSEBEGIN SELECT 'Password must Contain a number!'//ERROR LINE DELETE Password_1 FROM Individual WHERE Username=@username SELECT 'Please re-enter your password' END not sure your DELETE code is what you want. Do you want to delete the ROW from INDIVIDUAL table? or just set their password to NULL?Taking that into account I would actually code it as:UPDATE USET Password_1 = NULLFROM Individual AS UWHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%'IF @@ROWCOUNT = 0BEGIN SELECT 'Password Valid'ENDELSEBEGIN SELECT 'Password must Contain a number!' SELECT 'Please re-enter your password' END If you actually want to delete the row from Individual table then replace the UPDATE with a DELETEDELETE UFROM Individual AS UWHERE Username = @username AND Password_1 NOT LIKE '%[0-9]%'IF @@ROWCOUNT = 0... this code also takes care of the case that @username has more than one record, where one, or more, of the records does not contain a digit. Even if Username column is unique I prefer this style of programming as it is "defensive" - e.g. if the UNIQUE constraint ever got dropped by accident, or ... whatever!!
sorry I dont think thats what OP is loking at as per thisI'm trying to chech a see if a record(password) of individual have a number in it if not delete the recordsee this illustrationdeclare @t table(val varchar(100))insert @tvalues ('1243214'),('76tr0980'),('76576wrr'),('786876@hhhj'),('768757'),('432432'),('9879ghg')SELECT *,CASE WHEN val NOT LIKE '%[0-9]%' THEN 'Invalid' ELSE 'Valid' END AS KristenLogic,CASE WHEN val NOT LIKE '%[^0-9]%' THEN 'valid' ELSE 'Invalid' END AS VisakhLogicFROM @t val KristenLogic VisakhLogic1243214 Valid valid76tr0980 Valid Invalid76576wrr Valid Invalid786876@hhhj Valid Invalid768757 Valid valid432432 Valid valid9879ghg Valid Invalid ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Kristen
Test
22859 Posts |
Posted - 2013-12-04 : 04:05:46
|
quote: Originally posted by visakh16 sorry I dont think thats what OP is looking at as per thisI'm trying to chech a see if a record(password) of individual have a number in it if not delete the record
I agree, that is how I read that too. However, this codeDELETE Password_1 FROM Individual WHERE Username=@username looks more like the O/P is wanting to UPDATE the table to remove the password, rather than delete the row. Plus, I don't know what is in the [Individual] table, but from the name it sounds like more attributes than just password, and deleting your personal record 'coz you entered a duff password sounds a bit severe! But I'm guessing what the O/P's requirements actually are.quote: see this illustration
Dunno what I was thinking of, but I probably misread the requirement. I was worrying that your code would not KEEP an all-alpha password, now re-reading the question its the other way round - only keep if it contains a number. But my guess is that the O/P's requirement is Some Numbers and Some Letters ... but there I go guessing again!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-04 : 05:21:23
|
quote: Originally posted by Kristen
quote: Originally posted by visakh16 sorry I dont think thats what OP is looking at as per thisI'm trying to chech a see if a record(password) of individual have a number in it if not delete the record
I agree, that is how I read that too. However, this codeDELETE Password_1 FROM Individual WHERE Username=@username looks more like the O/P is wanting to UPDATE the table to remove the password, rather than delete the row. Plus, I don't know what is in the [Individual] table, but from the name it sounds like more attributes than just password, and deleting your personal record 'coz you entered a duff password sounds a bit severe! But I'm guessing what the O/P's requirements actually are.quote: see this illustration
Dunno what I was thinking of, but I probably misread the requirement. I was worrying that your code would not KEEP an all-alpha password, now re-reading the question its the other way round - only keep if it contains a number. But my guess is that the O/P's requirement is Some Numbers and Some Letters ... but there I go guessing again!! Yep...just like me We can play guess games until OP provides us with clear explanation what the expected behavior is------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|