Fail to assigned to the output parameter in stored procedure - sql-server

ALTER PROCEDURE [dbo].[loginCheck]
#uid AS int ,
#pwd AS nchar(50) ,
#result AS INT OUTPUT
AS
BEGIN
IF EXISTS(SELECT * FROM seller WHERE S_id = #uid AND S_password = #pwd)
BEGIN
SET #result = 1
EXEC(#result)
END
ELSE
BEGIN
SET #result = 0
EXEC(#result)
END
END
This is the code of my stored procedure, I want to check out the id and password when login, so I check if there exists this data in my database.
declare #result int
exec loginCheck 1, '123456', #result output
select #result
I use these to test my stored procedure, but it informed there exist grammatical errors around "1", when i debug by each statement, I found #result has changed to 1, so why i got this error, and how to fix it? Thanks

Related

Get value from store or store and get if not exists

what I want to achieve is a way to get previously generated or generate, store and return data, in a way, which could be used as function - needed to join or APPLY in UPDATE scripts.
So, having data generate function:
ALTER FUNCTION generateData()
RETURNS NVARCHAR(20)
AS
BEGIN
RETURN 'asdyukyuk' --some rng related algorithm
END
And "get or generate, save and get" procedure:
ALTER PROCEDURE getOrGenerateAndGet (#orig NVARCHAR(20), #result NVARCHAR(20) OUTPUT)
AS
BEGIN
IF #orig IS NULL
BEGIN
SET #result = NULL
END
ELSE
BEGIN
DECLARE #hash VARBINARY(64) = dbo.getHash(#orig)
SELECT #result = GENERATED_DATA FROM STORED_DATA WHERE HASH = #hash
IF #result IS NULL
BEGIN
SET #result = dbo.generateData()
INSERT INTO STORED_DATA (HASH, GENERATED_DATA) VALUES (#hash, #result)
END
END
RETURN 1
END;
GO
And UPDATE script:
UPDATE FRAGILE_DATA SET FRAGILE_COLUMN = getOrGenerateAndGet(FRAGILE_COLUMN)
it's obviously not working, because getOrGenerateAndGet is not function.
However, with function way:
CREATE FUNCTION getOrGenerateAndGet (#orig NVARCHAR(20))
RETURNS NVARCHAR(20)
AS
BEGIN
DECLARE #result NVARCHAR(20)
IF #orig IS NULL
BEGIN
SET #result = NULL
END
ELSE
BEGIN
DECLARE #hash VARBINARY(64) = dbo.getHash(#orig)
SELECT #result = GENERATED_DATA FROM STORED_DATA WHERE HASH = #hash
IF #result IS NULL
BEGIN
SELECT #result = dbo.generateData()
EXEC sp_executesql N'INSERT INTO STORED_DATA (HASH, GENERATED_DATA) VALUES (#hash, #result)'
END
END
RETURN #result
END;
GO
it's still not working, because "Only functions and some extended stored procedures can be executed from within a function."
Is there any way to achieve this, without enabling sql command shell?

return ERROR_MESSAGE() as output variable in sql server

I want to return the ERROR_MESSAGE() in a variable, but this is not working because is like raising the error but I don't want it like that. I want to receive the return value (0 ok, 1 error), plus the id of the insert in the variable #Id and the error msg in this variable #ErrorMsg if there is an error
Here is the code:
CREATE PROCEDURE [cimpl].[TestInsert]
(
#TestId INT,
#Name NVARCHAR(50),
#Id INT OUTPUT,
#ErrorMsg NVARCHAR(4000) = NULL OUTPUT
)
AS
BEGIN
DECLARE #ReturnVal int
SET NOCOUNT ON;
SET #ReturnVal = 0;
SET #ErrorMsg = '';
BEGIN TRY
-- Insert statement
INSERT [dbo].[Test]
(
[TestId],
[Name]
)
VALUES (
#TestId,
#Name,
)
SET #Id = SCOPE_IDENTITY();
END TRY
BEGIN CATCH
SET #ReturnVal = 1;
SET #ErrorMsg = ERROR_MESSAGE();
END CATCH
RETURN #ReturnVal;
END
Here is the call:
DECLARE #IdIns INT
DECLARE #ErrorM NVARCHAR(4000)
DECLARE #ReturnValue INT = 0
EXEC #ReturnValue =
[cimpl].[TransactInsert]
#TestId= 'ee', --I'm forcing the error
#Name = 'A',
#Id = #IdIns OUTPUT,
#ErrorMsg = #ErrorM OUTPUT
SELECT #ErrorM
SELECT #ReturnValue
SELECT #IdIns
The code in your stored proc should work as expected. The problem is your test is throwing an error before the proc even runs since you are attempting to assign a char value to an integer.
In your test code:
#TestId= 'ee', --I'm forcing the error
This throws an error at parse-and-compile time since #TestId is defined as an INT, however ee is a CHAR(2) value that cannot be coalesced into a number.

T-SQL Stored Procedure returned value into variable

I have a simple stored procedure on MS SQL 2012 Express that returns a result doing something like :
DECLARE #value INT;
SELECT #value;
Meanwhile I am trying to call this stored procedure doing something like
DECLARE #result INT;
EXEC #result = SpFoo .....
However the value I get on #result from the SpFoois 0.
How can I get the real value returned from the Stored Procedure?
Thanks
First declare #Value as output parameter in your called sp
CREATE PROCEDURE spCallee
#Value INT Output
AS
BEGIN
set #Value = 1
END
GO
Get result like this in caller sp
CREATE PROCEDURE spCaller
AS
BEGIN
declare #Result as int = 0
EXEC spCallee #Value = #Result Output
Select #Result
END
GO
The only way I know is to put result to the table and then get it from it:
DECLARE #tmp TABLE (Result int)
DECLARE #res int
INSERT INTO #tmp
EXECUTE [Procedure]...
SELECT #res = Result
FROM #tmp

Retrieving a particular value in SQL Server

I have this stored procedure that has a value:
BEGIN TRY
BEGIN TRANSACTION;
-- Insert statements for procedure here
DECLARE #return_value int
DECLARE #media_id uniqueidentifier
'INSERT SQL STATEMENT HERE
SELECT [media_id] FROM [media] WHERE 1 = 1
-- One row affected
RETURN 1
END TRY
BEGIN CATCH
IF ##TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
-- Rollback, no row affected
RETURN 0
END CATCH;
I want to call the [media_id] value from another stored procedure. How do I get that value?
Table Definition
CREATE TABLE MY_EMPLOYEE
(EMPID INT, NAME VARCHAR(20),
LANGUAGEID INT , ID UNIQUEIDENTIFIER DEFAULT NEWID())
GO
Stored Procedure
ALTER PROCEDURE usp_ProcName
#Emp_ID INT = null,
#Name VARCHAR(20) = null,
#LanguageID int = null,
#NewID UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Insert statements for procedure here
INSERT INTO [Practice_DB].[dbo].[MY_EMPLOYEE](EMPID, NAME, LANGUAGEID)
VALUES (#Emp_ID, #Name, #LanguageID);
-- Populating the OUTPUT variable using the other variables that were passed
-- for INSERT statement.
SELECT #NewID = ID
FROM [Practice_DB].[dbo].[MY_EMPLOYEE]
WHERE EMPID = #Emp_ID
-- One row affected
COMMIT TRANSACTION
RETURN 1
END TRY
BEGIN CATCH
IF ##TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END
-- Rollback, no row affected
RETURN 0
END CATCH
END
GO
Calling Stored Procedure
DECLARE #value int, #ID VARCHAR(100)
EXECUTE #value = usp_ProcName
#Emp_ID = 50,
#Name = 'John',
#LanguageID = 50,
#NewID = #ID OUTPUT --<-- passing this variable with OUTPUT key word this will be
-- populated inside the Procedure and then you can SELECT it or
-- whatever you want to do with this value.
SELECT #ID
SELECT #value
This should help: Return Data from a Stored Procedure
I think this should work
BEGIN TRY
BEGIN TRANSACTION;
-- Insert statements for procedure here
DECLARE #return_value int
DECLARE #media_id uniqueidentifier
--get the value from the stored procedure here
Declare #SQL varchar(max)
set #SQL='INSERT SQL STATEMENT HERE
SELECT #media_id FROM [media] WHERE 1 = 1'
exec #SQL
-- One row affected
RETURN 1
END TRY

Stored procedure is not working

I have created a stored procedure to retrieve some details based on the certain values passed to a parameter. THis requires switching between the SQLs to be executed by stored procedure. Following is the code:
USE [DFS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[DAFS]
#EmailID Nvarchar(128),
#clientID int,
#userType Varchar(50),
#Success numeric output,
#msg varchar(100) output
AS
BEGIN
if #userType='Normal User'
IF EXISTS (SELECT 1 FROM dbo.Allcdn
WHERE EmailID = #EmailID AND ClientID = #clientID)
begin
set #Success=0
set #msg='Carry on ....'
end
else
begin
set #Success=6
set #msg='Not allowed ...'
END
end
else
Begin
IF EXISTS (SELECT 1 FROM dbo.Alcon
WHERE EmailID = #EmailID AND ClientID = #clientID)
BEGIN
set #Success=0
set #msg='Carry on...'
END
END
End
end
END
The entire processing is based on the variable #userType. Not sure why the stored procedure is not compiling.
Formatting is your friend, just with a quick glance, it appears you have too many ENDs -- See SQL Fiddle with working Demo:
CREATE PROCEDURE [dbo].[DAFS]
#EmailID Nvarchar(128),
#clientID int,
#userType Varchar(50),
#Success numeric output,
#msg varchar(100) output
AS
BEGIN
if #userType='Normal User'
IF EXISTS (SELECT 1 FROM dbo.Allcdn
WHERE EmailID = #EmailID AND ClientID = #clientID)
begin
set #Success=0
set #msg='Carry on ....'
end
else
begin
set #Success=6
set #msg='Not allowed ...'
END
else
Begin
IF EXISTS (SELECT 1 FROM dbo.Alcon
WHERE EmailID = #EmailID AND ClientID = #clientID)
BEGIN
set #Success=0
set #msg='Carry on...'
END
END
end

Resources