I am trying to write a stored procedure which inserts the username and password into the database. And there is an Identity column which auto-increments itself. But I am not able to get the syntax correct !
Here is the code snippet:
CREATE PROCEDURE dbo.SPRegisterUser
#Username_V nvarchar(100),
#Email_V nvarchar(100),
#Password_V nvarchar(100)
AS
BEGIN
Declare #count int;
Declare #ReturnCode int;
Select #count = COUNT(Username)
from Register
where Username = #Username_V
If #count > 0
Begin
Set ReturnCode = -1
End
Else
Begin
Set ReturnCode = 1
Insert into Register
values(#Username_V, #Email_V, #Password_V)
END
RETURN
The error generated is
Incorrect Syntax near'='
Incorrect syntax near RETURN
First of all, the following line
RETURN
needs to be replaced by this
RETURN #ReturnCode
Also you're missing the final END
Also, variables need to start with a #, so change the two lines
SET ReturnCode ...
to
SET #ReturnCode ...
It must be:
Set #ReturnCode=1
It is a variable declared further up.
Related
I am trying to get a returncode from my SP and then based on its value (0 or 1) it will go to next task.
Create Procedure sp_test
AS
DECLARE #cmdline VARCHAR(500),
#ReturnCode INT
SELECT #cmdline = 'dir z:'
CREATE TABLE #temp (SomeCol VARCHAR(500))
INSERT #temp
EXEC #ReturnCode = master.dbo.xp_cmdshell #cmdline
IF #ReturnCode <> 0
BEGIN
SELECT SomeCol
FROM #temp
WHERE SomeCol IS NOT NULL
END
DROP TABLE #temp
RETURN #ReturnCode
GO
Now inside my Execute SQL task of SSIS, Here is what I type and specify ResultSet = Single Row
DECLARE #return_value int
EXEC #return_value = [dbo].[sp_test]
SELECT 'ReturnValue' = #return_value
Here is the error I am getting,
[Execute SQL Task] Error: An error occurred while assigning a value to variable "Variable": "Unable to find column ReturnValue in the result set.".
I need that ReturnCode Value so that I can use it for my next task.
Assuming OLE DB provider, the Result Name should be 0 as it's the zeroeth element of the result set. Also, ensure your Result Set type on the General tab is ... Single Row or whatever the label is.
Also, no need for a Parameter Mapping as your stored procedure does not accept parameters (input or output)
You have to try below logic...
Put the below code into SQL Statement .
DECLARE #return_value int
SET #return_value=?
EXEC ? = [dbo].[sp_test]
SELECT 'ReturnValue' = ?
And set the result set as in snapshot..
CREATE PROCEDURE dbo.test2
AS
BEGIN
DECLARE #status as int
DECLARE #error as int
SET #status = 1
SET #Error = ##ERROR
UPDATE dbo.BView
SET bview='dar'
WHERE pt='foo'
IF #Error > 0
print 'ERROR'
else
SELECT #status as 'status'
END;
Why does this query always return the default value of the local variable #status regardless of what was assigned to it?
status
------
0
Server version: MS SQL Server 2014 Express
In sql server 2005 : Run this in your sql server.
You cannot assign a default value at declaration.
and post the result in command.
DECLARE #status int
SET #status = 1
SELECT #status as 'status'
Suggestions. Although can't find some thing amiss, why not do it the normal way.
Always Declare a variable first before your update,insert or any action.
CREATE PROCEDURE dbo.sp_test2
AS
BEGIN
DECLARE #status as int
SET #status = 1
UPDATE dbo.BView
SET bview='dar'
WHERE pt='foo'
SELECT #status as 'status'
END
Second Always use the keyword AS then variable type , the followed by the size. If you dont follow by the variable size, it mostly return 0, note nvarchar types/varchar types mostly. Example
DECLARE #status as nvarchar
Often return 0 when use against a string of length 20 . Where as ,
Declare #status as nvacrhar(20)
Works fine.
There might exist an error in your update/Insert etc processes and i can see u put the declaration and the select process in the same node(i.e begin and end). You can check for an error like.
CREATE PROCEDURE dbo.sp_test2
AS
BEGIN
DECLARE #status as int
DECLARE #error as int
SET #status = 1
SET #Error = ##ERROR
--Some statement, update or insert, let's say:
UPDATE dbo.BView
SET bview='dar'
WHERE pt='foo'
IF #Error > 0
print 'ERROR'
else
SELECT #status as 'status'
END
These are suggestions though, I often do this way and it never fails me. FOr better understanding of variable in SQL Here and . Raising Errors in MSSql here and many more. Hope it helps.
So I have a stored proc and I am trying to set an output parameter to an input parameter but I can't get it to work.
Essentially here is my proc...
...
#InID DECIMAL(9),
#ID DECIMAL(9) OUTPUT,
...
if #InID is not null
begin
#ID = #InID
end
...
But this shows as having a syntax error. How do I fix this?
create procedure [dbo].[StackOverflow]
(
#InID DECIMAL(9),
#ID DECIMAL(9) OUTPUT
)
as
begin
if #InID is not null
begin
set #ID = #InID
end
end
What is the syntax error you got? The next works fine:
declare #out as decimal
exec StackOverflow 1, #out
Running into problems with a SQL Server 2008 stored procedure: I keep getting the following error.
Conversion failed when converting from a character string to
uniqueidentifier.
Here's the stored procedure - I've chopped a lot of it out for testing
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE RateReview
#ProfileKey INT
,#ReviewKey NVARCHAR(36)
,#Rating BIT
,#Result NVARCHAR(16) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET #ReviewKey = 'NotFound'
DECLARE #ReviewID INT = 0
DECLARE #VisitorProfileID INT = 0
DECLARE #ReviewRatingID INT = 0
DECLARE #VotedUp BIT = 0
DECLARE #Temp UNIQUEIDENTIFIER
SET #Temp = CONVERT(UNIQUEIDENTIFIER, #ReviewKey)
-- Commented code here
END
GO
I try to call this with the standard Management Studio "Execute" menu option, which given me this:
DECLARE #return_value int,
#Result nvarchar(16)
EXEC #return_value = [dbo].[maxi_content_RateReview]
#ProfileKey = 1985118925,
#ReviewKey = N'4D051C99-1D59-4BB0-BFB9-D26786B5C809',
#Rating = 1,
#Result = #Result OUTPUT
SELECT #Result as N'#Result'
SELECT 'Return Value' = #return_value
GO
I've checked that the GUID is correct and tried with both CAST and CONVERT - always the same problem. Any ideas? It's driving me nuts!
Your code has this line
SET #ReviewKey = 'NotFound'
Then you try and cast that string to uniqueidentifier. If you delete that line it works!
You
SET #ReviewKey = 'NotFound'
Then try to
SET #Temp = CONVERT(UNIQUEIDENTIFIER, #ReviewKey)
Which wont work as 'NotFound' isn't a GUID
Unless my eyeballs are deceiving me, your problem is on this line:
SET #ReviewKey = 'NotFound'
The string 'NotFound' cannot be converted to a GUID. It looks like your proc is overwriting whatever value is passed in with 'NotFound'.
Can we return null value from stored procedure. i dont want to use collase or isnull. I want to capture NULL at the frontend. Is it possible ?
Edit:
I am using Sql Server 2005
eg. where i want to use
CREATE PROCEDURE [Authentication].[spOnlineTest_CheckLogin]
#UserName NVARCHAR(50)
AS
BEGIN TRY
BEGIN TRAN
COMMIT TRAN
RETURN NULL
END TRY
Error
The 'spOnlineTest_CheckLogin' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
No, the return type of a stored procedure is INT and it cannot be null.
use an output parameter, example
CREATE PROCEDURE Test
#UserName NVARCHAR(50), #Status int output
AS
BEGIN TRY
BEGIN TRAN
COMMIT TRAN
set #Status = null
END TRY
begin catch
end catch
go
then call it like this
declare #s int
set #s =5
exec Test'bla',#s output
select #s --will be null
You can think of a proc like follows. Let me first set the context. We might have a table Table1(id int, name varchar(2), Address varchar(2)) and want to get the id and if not found, it will be null. So we might write a proc like the following:
CREATE PROCEDURE GetId
#Name VARCHAR(50), #Status int output
AS
BEGIN TRY
set #Status = null
select #Status = id from Table1 where name=#name
This will work for you.