How to return the output of stored procedure into a string variable in SQL Server - sql

I want to execute a stored procedure in SQL Server and assign the output to a string.
CREATE PROCEDURE sp_Check_User_Password
#name NVARCHAR(30),
#email NVARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #pass NVARCHAR(50)
IF EXISTS(SELECT dbo.Mitarbeiter.Name, dbo.Mitarbeiter.Email
FROM dbo.Mitarbeiter
WHERE dbo.Mitarbeiter.Name = #name
AND dbo.Mitarbeiter.Email = #email
BEGIN
SET #pass = dbo.Mitarbeiter.Password
END
ELSE
BEGIN
SET #pass = null
END
RETURN #pass
END
I have changed what I wrote wrong

Have a look on below sample, just add your stuff and it will work
CREATE PROCEDURE return_string
(#param1 NVARCHAR(10),
#param2 NVARCHAR(10),
#output NVARCHAR(10) OUT)
AS
BEGIN
IF (1=1)
SET #output = 'here'
else
SET #output = null
END
to test
declare #out nvarchar(10)
exec return_string 'firdt param', 'second param', #out out
select #out
I hope this will help you achive what you need.
So your SP would look like:
CREATE PROCEDURE sp_Check_User_Password
(
#name nvarchar(30),
#email nvarchar(50),
#pass nvarchar(50) OUT
)
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT 1
FROM dbo.Mitarbeiter
WHERE dbo.Mitarbeiter.Name = #name
and dbo.Mitarbeiter.Email = #email)
BEGIN
SET #pass = (SELECT dbo.Mitarbeiter.Name
FROM dbo.Mitarbeiter
WHERE dbo.Mitarbeiter.Name = #name
and dbo.Mitarbeiter.Email = #email)
END
ELSE
BEGIN
SET #pass = null
END
END

I'll take a stab at it. Is this what you're trying to do?
CREATE PROCEDURE sp_Check_User_Password
#name nvarchar(30),
#email nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #pass nvarchar(50) = null
IF EXISTS(SELECT dbo.Mitarbeiter.Email FROM dbo.Mitarbeiter
WHERE dbo.Mitarbeiter.Name = #name and dbo.Mitarbeiter.Email = #email)
BEGIN
SELECT #pass = dbo.Mitarbeiter.Email FROM dbo.Mitarbeiter
WHERE dbo.Mitarbeiter.Name = #name and dbo.Mitarbeiter.Email = #email
END
PRINT #pass
END

Related

How to get stored procedure internal Declare Variable value in SQL Server

This is my stored procedure:
CREATE PROCEDURE MYSP
(#PARAM INT,
#PARAM2 NVARCHAR(34),
#OUTPARAM3 NVARCHAR(34) OUTPUT)
AS
BEGIN
DECLARE #RParam INT
DECLARE #ErrParam2 NVARCHAR(34)
IF(#PARAM = 1 AND #PARAM2 = 'abc' )
BEGIN
SET #OUTPARAM3 = 'EQUAL'
SET #RPARAM = 0
END
ELSE
BEGIN
SET #ERRPARAM2 = 'SOME ERROR..MSG'
SET #RPARAM = -1
END
RETURN #RPARAM
END
And I am trying to get the value of the internally declare variable value.
DECLARE #RParam INT
DECLARE #ErrParam2 NVARCHAR(34)
#RParam is returned but how can I return #ErrParam2 value?
Declare #RP int
Declare #ErrMsg Nvarchar(40)
Exec #RP = MYSP 1,'abc'
You need to specify the output parameter both when the stored procedure is defined and when it is called:
Declare #RP int;
Declare #ErrMsg Nvarchar(40);
Exec #RP = MYSP 1, 'abc' , #ErrMsg output;

Stored procedure for Login Application

I have a table with the user details like
id
fname
lname
role
branch
email
password
I have a stored procedure for the login validation which takes the values of email and password, validates and send out the success message.
The validation is working now. I need to fetch the values of fname, lname, role and branch too.
Here is the code:
USE [Project]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[usp_loginuser]
(#Email varchar(100),
#password varchar(100)
)
AS
BEGIN
DECLARE #msg nvarchar(2048)
SET NOCOUNT ON
--select * from dbo.userdetails where email=#userEmail and pwd=#password
BEGIN TRY
Declare #flag bit
SET #flag = 0
IF EXISTS(Select * from userdetails
where ltrim(rtrim(email)) = ltrim(rtrim(#Email)) AND ltrim(rtrim(pwd)) = ltrim(rtrim(#password))
AND isactive = 1)
BEGIN
SET #flag =1;
END
ELSE
BEGIN
SET #flag =0;
END
SELECT #flag [IsSuccess]
END TRY
BEGIN CATCH
SET #msg = error_message()
RAISERROR (#msg, 16, 1)
END CATCH
SET NOCOUNT OFF
END
Here is the code
ALTER Procedure [dbo].[usp_loginuser]
(#Email varchar(100),
#password varchar(100),
#fname varchar(100) output,
#lname varchar(100) output
)
AS
BEGIN
DECLARE #msg nvarchar(2048)
SET NOCOUNT ON
BEGIN TRY
Declare #flag bit
SET #flag = 0
IF EXISTS(Select #fname = fname, #lname = lname from userdetails
where ltrim(rtrim(email)) = ltrim(rtrim(#Email)) AND ltrim(rtrim(pwd)) = ltrim(rtrim(#password))
AND isactive = 1)
BEGIN
SET #flag =1;
END
ELSE
BEGIN
SET #flag =0;
END
SELECT #flag [IsSuccess], #fname, #lname
END TRY
BEGIN CATCH
SET #msg = error_message()
RAISERROR (#msg, 16, 1)
END CATCH
SET NOCOUNT OFF
END
BEGIN TRY
Declare #flag bit
SET #flag = 0
IF EXISTS(Select * from userdetails
where ltrim(rtrim(email)) = ltrim(rtrim(#Email)) AND ltrim(rtrim(pwd)) = ltrim(rtrim(#password))
AND isactive = 1)
BEGIN
SET #flag =1;
END
ELSE
BEGIN
SET #flag =0;
END
SELECT fname, lname, role, branch from userdetails
where ltrim(rtrim(email)) = ltrim(rtrim(#Email)) AND ltrim(rtrim(pwd)) = ltrim(rtrim(#password))
END TRY
If you get an empty data set, its a failure else success

Getting Request Timeout sometimes from only one SP?

I have a lot of stored procedures. But I am only getting Request Timeout sometimes only for this SP ?
ALTER PROCEDURE [dbo].[Insertorupdatedevicecatalog]
(#OS NVARCHAR(50)
,#UniqueID VARCHAR(500)
,#Longitude FLOAT
,#Latitude FLOAT
,#Culture VARCHAR(10)
,#Other NVARCHAR(200)
,#IPAddress VARCHAR(50)
,#NativeDeviceID VARCHAR(50))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #TranCount INT;
SET #TranCount = ##TRANCOUNT;
DECLARE #OldUniqueID VARCHAR(500) = ''-1'';
SELECT #OldUniqueID = [UniqueID] FROM DeviceCatalog WHERE (#NativeDeviceID != '''' AND [NativeDeviceID] = #NativeDeviceID);
BEGIN TRY
IF #TranCount = 0
BEGIN TRANSACTION
ELSE
SAVE TRANSACTION Insertorupdatedevicecatalog;
DECLARE #Geo GEOGRAPHY = geography::STGeomFromText(''POINT('' + CONVERT(VARCHAR(100), #Longitude) + '' '' + CONVERT(VARCHAR(100), #Latitude) + '')'', 4326);
IF EXISTS(SELECT 1 FROM DeviceCatalog WHERE [UniqueID] = #UniqueID)
BEGIN
DECLARE #OldGeo GEOGRAPHY
,#OldCity NVARCHAR(100)
,#OldCountry NVARCHAR(100)
,#OldAddress NVARCHAR(100);
SELECT #OldGeo = [LastUpdatedLocationFromJob]
,#OldCity = [City]
,#OldCountry = [Country]
,#OldAddress = [Address]
FROM DeviceCatalog
WHERE [UniqueID] = #UniqueID;
UPDATE DeviceCatalog
SET [OS] = #OS
,[Location] = #Geo
,[Culture] = #Culture
,[Other] = #Other
,[IPAddress] = #IPAddress
WHERE [UniqueID] = #UniqueID;
IF (#OldGeo IS NULL OR #OldAddress IS NULL OR #OldCity IS NULL OR #OldCountry IS NULL OR ISNULL(#Geo.STDistance(#OldGeo) / 1000,0) > 50)
BEGIN
UPDATE DeviceCatalog
SET [Lastmodifieddate] = Getdate()
WHERE [UniqueID] = #UniqueID;
END
END
ELSE
BEGIN
INSERT INTO DeviceCatalog
([OS]
,[UniqueID]
,[Location]
,[Culture]
,[Other]
,[IPAddress]
,[NativeDeviceID])
VALUES (#OS
,#UniqueID
,#Geo
,#Culture
,#Other
,#IPAddress
,#NativeDeviceID);
IF(#OldUniqueID != ''-1'' AND #OldUniqueID != #UniqueID)
BEGIN
EXEC DeleteOldAndroidDeviceID #OldUniqueID, #UniqueID;
END
END
LBEXIT:
IF #TranCount = 0
COMMIT;
END TRY
BEGIN CATCH
DECLARE #Error INT, #Message VARCHAR(4000), #XState INT;
SELECT #Error = ERROR_NUMBER() ,#Message = ERROR_MESSAGE() ,#XState = XACT_STATE();
IF #XState = -1
ROLLBACK;
IF #XState = 1 AND #TranCount = 0
rollback
IF #XState = 1 AND #TranCount > 0
ROLLBACK TRANSACTION Insertorupdatedevicecatalog;
RAISERROR (''Insertorupdatedevicecatalog: %d: %s'', 16, 1, #error, #message) ;
END CATCH
END
The timeout occurs due to two updates to same table inside same transaction. You could avoid it with a case statement. Also whole IF ELSE can be replaced with a merge.
MERGE INTO DeviceCatalog DC
USING (SELECT #UniqueID AS UniqueID) T ON (DC.UniqueID = T.UniqueID)
WHEN MATCHED THEN
UPDATE SET [OS] = #OS
,[Location] = #Geo
,[Culture] = #Culture
,[Other] = #Other
,[IPAddress] = #IPAddress
,[Lastmodifieddate] = (CASE
WHEN (LastUpdatedLocationFromJob IS NULL OR [Address] IS NULL OR [City] IS NULL OR [Country] IS NULL OR ISNULL(#Geo.STDistance(LastUpdatedLocationFromJob) / 1000,0) > 50)
THEN Getdate()
ELSE [Lastmodifieddate]
END)
WHEN NOT MATCHED THEN
INSERT INTO DeviceCatalog
([OS]
,[UniqueID]
,[Location]
,[Culture]
,[Other]
,[IPAddress]
,[NativeDeviceID])
VALUES (#OS
,#UniqueID
,#Geo
,#Culture
,#Other
,#IPAddress
,#NativeDeviceID)
WHEN NOT MATCHED BY SOURCE AND #OldUniqueID != ''-1'' AND #OldUniqueID != #UniqueID THEN
DELETE;
Try it and check whether this is what you expected.
Already discussed here
You can achieve it using sp_getapplock in TSQL.
But you need a wrapper storedproc or batch for this. Check the following example it will help you to desing your wrapper sp/batch statement.
Sample Code Snippet
Create table MyTable
(
RowId int identity(1,1),
HitStartedAt datetime,
HitTimestamp datetime,
UserName varchar(100)
)
Go
Create proc LegacyProc (#user varchar(100), #CalledTime datetime)
as
Begin
Insert Into MyTable
Values(#CalledTime, getdate(), #user);
--To wait for 10 sec : not required for your procedures, producing the latency to check the concurrent users action
WAITFOR DELAY '000:00:10'
End
Go
Create Proc MyProc
(
#user varchar(100)
)
as
Begin
Declare #PorcName as NVarchar(1000), #CalledTime datetime
Begin Tran
--To get the Current SP Name, it should be unique for each SP / each batch
SET #PorcName = object_name(##ProcID)
SET #CalledTime = Getdate()
--Lock the Current Proc
Exec sp_getapplock #Resource = #PorcName, #LockMode = 'Exclusive'
--Execute Your Legacy Procedures
Exec LegacyProc #user, #CalledTime
--Release the lock
Exec sp_releaseapplock #Resource = #PorcName
Commit Tran
End
You are doing two seperate updates on the DeviceCatalog table where [UniqueID] = #UniqueID in the same transaction.
I bet your locking/request timeout issue is happening when:
IF (#OldGeo IS NULL OR #OldAddress IS NULL OR #OldCity IS NULL OR #OldCountry IS NULL OR ISNULL(#Geo.STDistance(#OldGeo) / 1000,0) > 50) is true.
Try something like this in place of the two updates.
Obviously test in dev first.
In the else clause, you want to have it insert something if the when is false. Here I am just inserting the current before update field contents.
UPDATE DeviceCatalog
SET [OS] = #OS
,[Location] = #Geo
,[Culture] = #Culture
,[Other] = #Other
,[IPAddress] = #IPAddress
,[Lastmodifieddate] =
case when (
#OldGeo is NULL
OR
#OldAddress is NULL
OR
#OldCity is NULL
OR
#OldCountry is NULL
OR
ISNULL(#Geo.STDistance(#OldGeo) / 1000,0) > 50
) then Getdate()
else [Lastmodifieddate]
end
WHERE [UniqueID] = #UniqueID

Dont want Select variable displaying values

It might be simple but I been having trouble figuring out.
I have a piece of code(Similar to Below Code) where I am assigning bunch of values to bunch of variables via select, it does the job but when I am running it I am getting result set which is causing my SSMS to crash is there way to avoid this....
DECLARE #Name VARCHAR(100)
DECLARE #LastName VARCHAR(100)
DECLARE #Address VARCHAR(100)
SELECT TOP 1
#Name = Name
#LastName = LastName
#Address = Address
From Person.Address
Where Name = 'Name'
Order By ID
I am using the above code in a loop where I am processing around 3-400K rows and pass this variables to a stored procedure, each top 1 select statement throws a result set causing my SSMS to crash I dont really need the select top 1 values to be displayed, any idea how to get rid of this?....
Any help would be much appreciated.
---As requested below is the code, I have hashed few things but this is what it is and I am running it from Query Analayzer as this is only 1 time process so we dont need SP to be created.
DECLARE #retstat INT
DECLARE #Name VARCHAR(255)
DECLARE #Lang TINYINT
DECLARE #Address CHAR(10)
DECLARE #ID INT
DECLARE #BranchSeqNo INT
DECLARE #AddressCode VARCHAR(10)
DECLARE #Address1 VARCHAR(50)
DECLARE #City VARCHAR(30)
DECLARE #State VARCHAR(3)
DECLARE #PostalCode VARCHAR(15)
DECLARE #Country VARCHAR(30)
SET #ID = NULL
UPDATE RAWClaimTable Set Processed = 0 where Processed is null
UPDATE RAWClaimTable SET ErrorCode = NULL ,ErrorMessage = NULL ,Processed = 0
WHERE ErrorMessage IS NOT NULL AND CLAIMID is null
WHILE EXISTS ( SELECT *
FROM RAWClaimTable
WHERE Processed = 0 )
BEGIN
-----Initialize Default Variables
SET #Lang = 0
SET #Address = 'Import'
SET #SaveMode = 0
SET #ID = Null
SELECT TOP 1
#LossDate = LossDate ,
#ClaimDate = ClaimDate ,
#OpenDate = OpenDate ,
#Receivedate = ReceiVeDate ,
#Name = Name ,
#Address = Address ,
#Address1 = Address1 ,
#City = City ,
#State = State ,
#PostalCode = PostalCode ,
#Country = Country
FROM RAWClaimTable
WHERE Processed = 0
ORDER BY ClaimID
BEGIN TRY
EXEC #RetStat = Usp_ProcessRawData #Lang, #Address, #SaveMode, #ID OUT,
#BranchSeqNo, #OriginalBranchSeqNo, #IncidentID,
#AssignmentTypeCode, #PartnershipID, #AccountID,
END TRY
BEGIN CATCH
SELECT #RetStat = ##Error
if ##Trancount > 0 rollback tran
IF #RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE() where ClaiMKey = #Name
END
END CATCH
IF #ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = #ID ,
Processed = 1
WHERE ClaiMKey = #Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = #Name
END
END
Use a CURSOR to loop thru your rows!
SET #Lang = 0
SET #Address = 'Import'
SET #SaveMode = 0
SET #ID = Null
DECLARE my_cursor CURSOR FOR
SELECT LossDate, ClaimDate, OpenDate, ReceiVeDate, Name, Address,
Address1, City, State, PostalCode, Country
FROM RAWClaimTable
WHERE Processed = 0
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO #LossDate, #ClaimDate, #OpenDate, #Receivedate, #Name, #Address,
#Address1, #City, #State, #PostalCode, #Country
WHILE ##FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC #RetStat = Usp_ProcessRawData #Lang, #Address, #SaveMode, #ID OUT,
#BranchSeqNo, #OriginalBranchSeqNo, #IncidentID,
#AssignmentTypeCode, #PartnershipID, #AccountID,
END TRY
BEGIN CATCH
SELECT #RetStat = ##Error
if ##Trancount > 0 rollback tran
IF #RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE()
where ClaiMKey = #Name
END
END CATCH
IF #ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = #ID ,
Processed = 1
WHERE ClaiMKey = #Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = #Name
END
FETCH NEXT FROM my_cursor
INTO #LossDate, #ClaimDate, #OpenDate, #Receivedate, #Name, #Address,
#Address1, #City, #State, #PostalCode, #Country
END

Not able to get INSERT of SP to work

I am not able to get the INSERT portion of the below procedure to work. Any help would be appreciated....
#currTable varchar(100),
#ID int,
#short_Text varchar(250),
#brief_Descrip varchar(250) = Null,
#needsTranslation varchar(10) = Null,
#prev_LangString varchar(250) = Null,
#lang_String varchar(250) = Null,
#original_lang_String varchar(250) = Null,
#StringID_from_Master int,
#GUID varchar(250) = Null
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME;
SET #submitDate1 = GETDATE()
SET #prev_LangString = #original_lang_String
DECLARE #sql NVARCHAR(MAX);
SELECT #sql = N' UPDATE ' + QUOTENAME(#currTable) + ' SET [lang_String] = ''' + REPLACE(#lang_String,'''','''''') + ''', [date_Changed] = ''' + convert(varchar(20), #submitDate1) + ''', [prev_LangString] = ''' + #prev_LangString + ''', [needsTranslation] = ''' + #needsTranslation + ''' WHERE ID = ' + RTRIM(#ID) + '; ';
EXEC sp_executesql #sql;
INSERT tblPendingDBUpdates
( stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID
)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
I tried this code block only and no luck it did not INSERT to tblPendingDBUpdates.... Noticed something weird about GUID as field name so changed it in table also...
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME;
SET #submitDate1 = GETDATE()
-- SET #prev_LangString = #original_lang_String
DECLARE #sql NVARCHAR(MAX);
DECLARE #currTable varchar(100);
SET #currTable = 'tblLangenUS'
DECLARE #ID INT;
SET #ID = 2
DECLARE #short_Text varchar(250);
SET #short_Text = 'testing99'
DECLARE #StringID_from_Master INT;
SET #StringID_from_Master = 2
DECLARE #lang_String varchar(250);
SET #lang_String = 'testing9999'
DECLARE #GUID1 varchar(250);
SET #GUID1 = 'Null'
INSERT tblPendingDBUpdates
( stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1
)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
I don't really know what you need but here is a stored proc with your declarations
CREATE PROCEDURE TestProc
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME
SET #submitDate1 = GETDATE()
DECLARE #sql NVARCHAR(MAX)
DECLARE #StringID_from_Master INT
SET #StringID_from_Master = 2
DECLARE #short_Text varchar(250)
SET #short_Text = 'testing99'
DECLARE #lang_String varchar(250)
SET #lang_String = 'testing9999'
DECLARE #GUID1 varchar(250)
SET #GUID1 = 'Null'
BEGIN
EXEC sp_executesql #sql
END
BEGIN
INSERT INTO tblPendingDBUpdates
(
stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1
)
SELECT
#StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master
END
END
GO
Well as far I understand it ... change the SP code to below and try again ... most probably will work fine
CREATE PROCEDURE <SOME NAME>
AS
BEGIN
<ALL YOUR DECLARATION HERE>
BEGIN
EXEC sp_executesql #sql;
END
GO
BEGIN
INSERT INTO tblPendingDBUpdates
(stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
END
GO
If your not getting an error message and inserting zero rows, than you select statement must not be returning anything.
what does this return?
select *
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master
If you're first stored proc it didn't look like #StringID_from_Master ever got set. That could be your problem.