This stored procedure doesn't work. It just returns 0 rows. I've checked the SQL and it returns the correct value when parsed directly to the DB. What could be wrong?
ALTER PROCEDURE dbo.GetSaltOfUser
(
#eMail nvarchar
)
AS
DECLARE #result nvarchar
/* SET NOCOUNT ON */
BEGIN
SELECT #result = salt
FROM UserSet
WHERE eMail = #eMail
RETURN #result
END
#eMail nvarchar
Will truncate the passed in email to one character. You need to put in a length. e.g.
#eMail nvarchar(50)
This should match the datatype of the relevant column in UserSet
Also do you really want to be using the return code for this or do you want to use an output parameter perhaps - or just a scalar select?
ALTER PROCEDURE dbo.GetSaltOfUser
(
#eMail nvarchar (50), /*Should match datatype of UserSet.eMail*/
#salt nvarchar (50) OUTPUT /*Should match datatype of UserSet.salt*/
)
AS
BEGIN
SET NOCOUNT ON
SELECT #result = salt
FROM UserSet
WHERE eMail = #eMail
END
And to call it
DECLARE #salt nvarchar(50)
EXECUTE dbo.GetSaltOfUser N'abc#example.com', #salt OUTPUT
SELECT #salt
You do not need to assign the salt to a variable.
CREATE PROCEDURE dbo.GetSaltOfUser
(
#eMail nvarchar
)
AS
BEGIN
SELECT salt
FROM UserSet
WHERE eMail = #eMail
END
Two Cents from my end
Good you are using SET NOCOUNT ON
Always use RETURN to return status code, Example 0- success, 1 - failure
Use SELECT to return the ROWS
Use Try Catch to handle Error Conditions
Related
There is a stored procedure that can return top 1 result as
USE [DB]
GO
.....
CREATE PROCEDURE [dbo].[GET]
(#in VARCHAR(10), #Out VARCHAR(10) OUTPUT)
AS
SELECT top 1 #Out = tab.Col
FROM table tab
RETURN
GO
When I call it in main query
DECLARE #output VARCHAR(10)
DECLARE #in VARCHAR(10)
DECLARE #Out VARCHAR(10)
EXECUTE dbo.GET #in = 'table', #Out = #output
It prints #output as 0;
but if I do
EXECUTE dbo.GET #in = 'table', #Out = #Out
And print #out, I get the correct value.
Why could this happen?
I did pass output #Out to pre-defined variable #output
Assuming SQLS due to presence of 'dbo' and sqlserver tag
Your query in the procedure doesn't assign a value to the out parameter (called #out) it assigns to some other variable called #outpk. Resolve the naming mismatch and make them the same
Sqlserver does not support LIMIT. To limit result set size use SELECT TOP 1 *. Using TOP (or any similar result set restrictor) without ORDER BY is illogical. Specify an ORDER BY
In sqlserver, output parameters must be passed with the OUTPUT keyword when calling the procedure:
EXEC sprocname #inputparameter ='value', #outputparameter = #variableToSet OUTPUT;
Use semicolons; omitting them is deprecated
Example
USE [DB]
GO
CREATE PROCEDURE [dbo].[GET]
(#in VARCHAR(10), #OutPk VARCHAR(10) OUTPUT)
AS
SELECT #OutPK = tab.Col
FROM table tab
ORDER BY tab.Col;
GO
DECLARE #output VARCHAR(10);
EXECUTE dbo.GET #in = 'table', #OutPK = #output OUTPUT
SELECT #output;
If its MySql (Limit is in mySql), you can simply call:
Call dbo.GET('table', #out);
No need to have separate variable #output.
i would like to hash a varchar and store it in db but i 've got a problem
when checking its value ,i thought i just had to hash it again and compare to db value
ALTER PROCEDURE [dbo].[AddLecteur]
#userName NVARCHAR(50),
#PasswordHash NVARCHAR(50),
#biblioPrincip int
AS
BEGIN
SET NOCOUNT ON
declare #ErrorMessage NVARCHAR(250)
declare #salt varbinary(4)=crypt_gen_random(4)
BEGIN TRY
INSERT INTO lecteur(nom, motPassword, biblioPrincipal)
VALUES(#userName,HASHBYTES('SHA2_256',cast( #PasswordHash as varbinary(max))+#salt), #biblioPrincip)
SET #ErrorMessage='Success'
END TRY
BEGIN CATCH
SET #ErrorMessage=ERROR_MESSAGE()
END CATCH
END
the value inserted is ‰¥_#碿K¤IFÕšxHà6œûäÜô4îõ„R¨Ó
am i not supposed to get the same value when checking the user if i use a salt inserted at the creation of the user and hash the user input the same way?
the second trigger hashing a proposal to compare to the one above generated when creating the user
ALTER PROCEDURE [dbo].[CheckngUser]
#userName varchar(50),
#password nvarchar(50),
#libelle varchar(50)
AS
BEGIN
declare #salt varbinary(4)
set #salt=(select lecteur.salt from lecteur where lecteur.nom=#userName)
select HASHBYTES('SHA2_256',cast( #password as varbinary(max))+#salt),
le.id,le.nom,le.[motPassword],bi.libellé from lecteur as le
inner join biblio as bi on le.biblioPrincipal=bi.id
where le.nom=#userName and le.motPassword=HASHBYTES('SHA2_256',cast(
#password as varbinary(max))+#salt)
END
why do i have this value here 0x7774FB52EB1FB5D3DD731A8B64B4BA1A73F4893F8A3C9084248D774D83C3E326
I am trying to run a simple script in SQL Server Management Studio.
Declare #total varchar (max)
set #total = 1
Declare #total2 varchar (max)
set #total2 = '1,4'
if #total in (#total2)
print 'Success'
For some reason I cannot get this simple script to work. It will work when I do this though "if 1 in ('1,2')" it will print "Success" Any help[ would be greatly appreciated.
You need to use Dynamic SQL because your #total contains varchar not numbers
Declare #total varchar (max)
set #total = 1
Declare #total2 varchar (max)
set #total2 = '1,4'
exec ('if '+#total+' in ('+#total2+') print ''Success'' ' )
IN works against sets, not strings. You could either declare a table variable and perform INSERT statements:
Declare #total varchar (max)
set #total = 1
Declare #total2 TABLE varchar (max)
(
total int
)
INSERT INTO #total2 VALUES (1)
INSERT INTO #total2 VALUES (4)
if #total in (SELECT total FROM #total2)
print 'Success'
or use string functions like CHARINDEX, but then you have to consider partial matches, etc.
You could convert a comma-delimited string to a table, but you'd still need to insert the values one-by one using a loop.
How can i execute a stored procedure in another stored procedure in SQL server?
How will I pass the parameters of the second procedure.?
If you only want to perform some specific operations by your second SP and do not require values back from the SP then simply do:
Exec secondSPName #anyparams
Else, if you need values returned by your second SP inside your first one, then create a temporary table variable with equal numbers of columns and with same definition of column return by second SP. Then you can get these values in first SP as:
Insert into #tep_table
Exec secondSPName #anyparams
Update:
To pass parameter to second sp, do this:
Declare #id ID_Column_datatype
Set #id=(Select id from table_1 Where yourconditions)
Exec secondSPName #id
Update 2:
Suppose your second sp returns Id and Name where type of id is int and name is of varchar(64) type.
now, if you want to select these values in first sp then create a temporary table variable and insert values into it:
Declare #tep_table table
(
Id int,
Name varchar(64)
)
Insert into #tep_table
Exec secondSP
Select * From #tep_table
This will return you the values returned by second SP.
Hope, this clear all your doubts.
Suppose you have one stored procedure like this
First stored procedure:
Create PROCEDURE LoginId
#UserName nvarchar(200),
#Password nvarchar(200)
AS
BEGIN
DECLARE #loginID int
SELECT #loginID = LoginId
FROM UserLogin
WHERE UserName = #UserName AND Password = #Password
return #loginID
END
Now you want to call this procedure from another stored procedure like as below
Second stored procedure
Create PROCEDURE Emprecord
#UserName nvarchar(200),
#Password nvarchar(200),
#Email nvarchar(200),
#IsAdmin bit,
#EmpName nvarchar(200),
#EmpLastName nvarchar(200),
#EmpAddress nvarchar(200),
#EmpContactNo nvarchar(150),
#EmpCompanyName nvarchar(200)
AS
BEGIN
INSERT INTO UserLogin VALUES(#UserName,#Password,#Email,#IsAdmin)
DECLARE #EmpLoginid int
**exec #EmpLoginid= LoginId #UserName,#Password**
INSERT INTO tblEmployee VALUES(#EmpName,#EmpLastName,#EmpAddress,#EmpContactNo,#EmpCompanyName,#EmpLoginid)
END
As you seen above, we can call one stored procedure from another
Yes, you can do that like this:
BEGIN
DECLARE #Results TABLE (Tid INT PRIMARY KEY);
INSERT #Results
EXEC Procedure2 [parameters];
SET #total 1;
END
SELECT #total
Your sp_test: Return fullname
USE [MY_DB]
GO
IF (OBJECT_ID('[dbo].[sp_test]', 'P') IS NOT NULL)
DROP PROCEDURE [dbo].sp_test;
GO
CREATE PROCEDURE [dbo].sp_test
#name VARCHAR(20),
#last_name VARCHAR(30),
#full_name VARCHAR(50) OUTPUT
AS
SET #full_name = #name + #last_name;
GO
In your sp_main
...
DECLARE #my_name VARCHAR(20);
DECLARE #my_last_name VARCHAR(30);
DECLARE #my_full_name VARCHAR(50);
...
EXEC sp_test #my_name, #my_last_name, #my_full_name OUTPUT;
...
You can call User-defined Functions in a stored procedure alternately
this may solve your problem to call stored procedure
Yes ,
Its easy to way we call the function inside the store procedure.
for e.g. create user define Age function and use in select query.
select dbo.GetRegAge(R.DateOfBirth, r.RegistrationDate) as Age,R.DateOfBirth,r.RegistrationDate from T_Registration R
Procedure example:
Create PROCEDURE SP_Name
#UserName nvarchar(200),
#Password nvarchar(200)
AS
BEGIN
DECLARE #loginID int
--Statements for this Store Proc
--
--
--
--execute second store procedure
--below line calling sencond Store Procedure Exec is used for execute Store Procedure.
**Exec SP_Name_2 #params** (if any)
END
How to user prefix 'N' for unicode with nvarchar variable in SQL Server? For example:
Given this variable:
declare #Query1 nvarchar(max)
I can assign to it like this:
set #Query1 = N'لاحظات'
But what if I want to use N#Query1 somewhere?
You only need to use N'xyz' when you have literal text. Once the nvarchar data is in a variable or result set column of nvarchar data type you no longer need to use the N'...' notation, the system knows the data type at that point.
try it out:
DECLARE #A nvarchar(100)
,#B nvarchar(100)
SET #A = N'anything here!!'
SET #B=#A --would work the same if coming from a query result set as well
SELECT #B --will show special unicode characters
declare #nv1 nvarchar(50) = N'لاحظات', #nv2 nvarchar(50) = 'لاحظات', #v varchar(50) = N'لاحظات'
declare #nv3 nvarchar(50) = #nv1 + ' hallo', #nv4 nvarchar(50) = #nv1 + N' hallo'
select #nv1, #nv2, #nv3, #nv4, #v
It is used with string literals to indicate the text should be treated as unicode. e.g.
DECLARE #something NVARCHAR(100)
SET #something = N'sometext'
Declare #var nvarchar(255)
Set #var = N'Hello World'
print #var
create table #tmp( columnA nvarchar(255) not null)
insert into #tmp(columnA) Values (N'Test')
Select #var = columnA from #tmp
print #var
drop table #tmp
Thanks to marc_s for his answer, that solved my problem.
I highlight it here as an answer for those who can't find it.
"
if #Query1 IS a NVARCHAR variable, then it IS a NVARCHAR variable and you don't need to prefix it with another 'N' to make it NVARCHAR.... just use it
"