int is incompatible with uniqueidentifier when no int usage - sql

I am getting this error when there is absolutely no usage of int anywhere.
I have this stored procedure
ALTER procedure [dbo].[usp_GetFileGuid] #fileType varchar(25)
as
select [id] from fileTypes where dirId = #fileType
Here id is a uniqueidentifier in fileTypes table
When I execute the following
declare #fileGuid uniqueidentifier
exec #fileGuid = usp_GetFileGuid 'accounts'
print #fileGuid
I get the following error
(1 row(s) affected)
Msg 206, Level 16, State 2, Procedure usp_GetFileGuid, Line 0
Operand type clash: int is incompatible with uniqueidentifier
Is there anything wrong with the syntax of assigning output of stored procedure to the local variable? Thank you.

You are using EXEC #fileGuid = procedure syntax which is used for retrieving return values, not resultsets. Return values are restricted to INT and should only be used to return status / error codes, not data.
What you want to do is use an OUTPUT parameter:
ALTER procedure [dbo].[usp_GetFileGuid]
#fileType varchar(25),
#id UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT #id = [id] from dbo.fileTypes where dirId = #fileType;
-- if the procedure *also* needs to return this as a resultset:
SELECT [id] = #id;
END
GO
Then for usage:
declare #fileGuid uniqueidentifier;
exec dbo.usp_GetFileGuid #fileType = 'accounts', #id = #fileGuid OUTPUT;
print #fileGuid;

create procedure [dbo].[usp_GetFileGuid] #fileType varchar(25),#uuid uniqueidentifier output
as
select #uuid=[id] from fileTypes where dirId = #fileType
declare #fileGuid uniqueidentifier
exec usp_GetFileGuid 'accounts',#fileGuid output
print #fileGuid

The value returned is an int as it is the status of the execution
From CREATE PROCEDURE (Transact-SQL)
Return a status value to a calling procedure or batch to indicate
success or failure (and the reason for failure).
You are looking for an output parameter.
OUT | OUTPUT
Indicates that the parameter is an output parameter. Use
OUTPUT parameters to return values to the caller of the procedure.
text, ntext, and image parameters cannot be used as OUTPUT parameters,
unless the procedure is a CLR procedure. An output parameter can be a
cursor placeholder, unless the procedure is a CLR procedure. A
table-value data type cannot be specified as an OUTPUT parameter of a
procedure.

Related

SQL Server : XML value returns Scalar and seems I need to convert it twice

I have this line of SQL:
DECLARE #temp_Id UNIQUEIDENTIFIER = #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
So, #data is a parameter to my stored procedure. #data XML
I want to pass a value out of the XML into another stored procedure, as you can see it's the Id field in the xml.
#data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
This pulls out and converts the Id value to a Uniqueidentifier however when I assign this to a variable and pass it to the stored procedure:
EXEC my_sproc #Id = #temp_Id
I get an error
Msg 137, Level 15, State 2, Procedure Some_Sproc, Line __
Must declare the scalar variable "#tempId".
Looking around, people are suggesting to wrap the #data.value(... in a CONVERT( DATA_TYPE, VALUE ) which seems slightly absurd.
-- EDIT --
I reckon there could also be a chance of my SQL Server Management Studio intellisense being out of sync....
-- CODE --
CREATE PROCEDURE [dbo].[Sproc_CB8PutNotification]
#message_type NVARCHAR(250),
#utc_timestamp DATETIME2,
#data XML
AS
BEGIN TRY
DECLARE #temp_Id UNIQUEIDENTIFIER = #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
EXEC dbo.SaveNotification #Id = #tempId;
END TRY
BEGIN CATCH
-- some catch code
END CATCH
It should work:
CREATE PROCEDURE my_proc
#id UNIQUEIDENTIFIER
AS
SELECT #id;
GO
DECLARE #data XML =
'<metaData><item key="Id" value="903e9859-f8fd-4163-9303-b43f89fe977f"/>
</metaData>';
DECLARE #temp_Id UNIQUEIDENTIFIER
= #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier');
EXEC my_proc #id = #temp_id;
LiveDemo
I suspect you have GO betweens calls and variable is not visible between batches like:
DECLARE #data XML =
'<metaData>
<item key="Id" value="903e9859-f8fd-4163-9303-b43f89fe977f"/>
</metaData>';
DECLARE #temp_Id UNIQUEIDENTIFIER
= #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier');
GO
EXEC my_proc #id = #temp_id;
Error(s), warning(s):
Must declare the scalar variable "#temp_id".
LiveDemo2
Or you have nested calls and variable is out of scope.
EDIT:
Typo #temp_id <> #tempId:
DECLARE #temp_Id UNIQUEIDENTIFIER =
#data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
EXEC dbo.SaveNotification #Id = #tempId;
Working code
You've mixed two naming conventions. The key point is to be consistent. It can save a lot of headaches.

unqiueidenfitier is not compatible with type int SQL Server Procedure

I have the following procedure for inserting into a user table:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andy Armstrong
-- Create date:
-- Description:
-- =============================================
CREATE PROCEDURE db_SignupAddLogin
-- Add the parameters for the stored procedure here
#LoginName VARCHAR(15),
#LoginPassword VARCHAR(15)
AS
BEGIN
DECLARE #GUID UNIQUEIDENTIFIER
SET #GUID = NEWID();
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO tblMemberLogin
(
UserID,
LoginName,
LoginPassword
)
VALUES
(
#GUID,
#LoginName,
#LoginPassword
)
RETURN #GUID
END
GO
However when I execute it I get the following error:
Msg 206, Level 16, State 2, Procedure db_SignupAddLogin, Line 34
Operand type clash: uniqueidentifier is incompatible with int
I cannot quite workout why as i am not referencing an int anywhere.
My Schema for tblMemberLogin looks like this:
UserID(PK,uniqueidentifier,notnull)
LoginName(nchar(15),not null)
LoginPassword(nchar(15),not null)
Please help!
RETURN can only be used with an int. You can simply use a SELECT query to retrieve the value of variable #GUID.
Reference: http://technet.microsoft.com/en-us/library/ms174998(v=sql.110).aspx
get rid of RETURN #GUID and you should be good to go.
In SQL Server, stored procedures may only return integer values. SQL Server RETURN
If you want to return data from a stored procedure other than an integer, you can use an output parameter: Returning Data from Stored Procedures
You declare the output parameter along with your input parameters:
CREATE PROCEDURE CREATE PROCEDURE db_SignupAddLogin
-- Add the parameters for the stored procedure here
#LoginName VARCHAR(15),
#LoginPassword VARCHAR(15),
#NewGuid UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET #NewGuid = NEWID();
-- rest of procedure
END
And then use the output parameter:
DECLARE #NewLoginGuidFromSP UNIQUEIDENTIFIER
EXECUTE db_SignupAddLogin 'Username', 'password', #NewGuid = #NewLoginGuidFromSP OUTPUT;

output parameter in stored procedure not working

I am new with stored procedures.
I have simple stored procedure for addition of two numbers as follows:
alter proc simpleProc
(
#Tax int ,
#TotalAmount int,
#sum int output
)
as
BEGIN
set #sum=(#Tax+#TotalAmount)
print #sum
END
As we can see in this #sum is output parameter.
But when I execute it as follows:
exec simpleProc 908,82
It gives me the following error:
Msg 201, Level 16, State 4, Procedure simpleProc, Line 0
Procedure or Function 'simpleProc' expects parameter '#sum', which was not supplied.
I have mentioned #sum as output parameter, but then also its demanding me to input #sum parameter.
What can be the mistake?
Yes it you haven't provided output parameter.
Try this
Declare #op int
exec simpleProc 908,82,#op output
//use op variable
You should give the procedure a variable where the output can be stored
declare #sum int
exec simpleProc 908, 82, #sum output

SQL Server Stored Procedure Update and Return Single Value

I need to call a stored procedure, give it a report id (int) and have it update a report table with a confirmation number (varchar) (confirmation # generated in the stored procedure) and return the confirmation number (i will need it to return it to a web service/website).
My stored procedure code:
DECLARE PROCEDURE [dbo].[spUpdateConfirmation]
#ReportID int
AS
BEGIN
Declare #Confirmation varchar(30) = replace(replace(replace(convert(varchar(16),CURRENT_TIMESTAMP,120),'-',''),' ',''),':','')+convert(varchar(24),#ReportID)
PRINT #Confirmation
UPDATE Report
SET Confirmation = #Confirmation
WHERE ReportID = #ReportID;
RETURN #Confirmation
END
My call to the stored procedure:
execute [spUpdateConfirmation] 2
I confirmed in the table the value was inserted but I get this error message:
2013050219072
(1 row(s) affected)
Msg 248, Level 16, State 1, Procedure spUpdateConfirmation, Line 12
The conversion of the varchar value '2013050219072' overflowed an int column.
The 'spUpdateConfirmation' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
Question: What did I do wrong?
I understand what overflow is, the value is too large for an int, but I used the convert to varchar, inserted to table column type varchar(30)
I also tested this statement in SQL and it works fine:
print replace(replace(replace(convert(varchar(16),CURRENT_TIMESTAMP,120),'-',''),' ',''),':','')+convert(varchar(24),2)
It returns: 2013050219162
RETURN from a stored procedure only allows integer values. Specifically, the documentation states:
RETURN [ integer_expression ]
If you want to return a varchar value, you can use an output parameter
CREATE PROCEDURE [dbo].[spUpdateConfirmation]
#ReportID int, #Confirmation varchar(30) output
AS
--BEGIN
SET #Confirmation = replace(replace(replace(convert(varchar(16),CURRENT_TIMESTAMP,120),'-',''),' ',''),':','')+convert(varchar(24),#ReportID)
--PRINT #Confirmation
UPDATE Report
SET Confirmation = #Confirmation
WHERE ReportID = #ReportID;
--RETURN #Confirmation
--END
GO
To be called like this
declare #reportID int; -- set #reportID
declare #confirmation varchar(30);
exec [dbo].[spUpdateConfirmation] #reportID, #confirmation output;
-- #confirmation now contains the value set from the SP call above
A simpler option if you are calling this from C# is to SELECT the output as a single-row, single-column result and use SqlCommand.ExecuteScalar, e.g.
CREATE PROCEDURE [dbo].[spUpdateConfirmation]
#ReportID int
AS
--BEGIN
DECLARE #Confirmation varchar(30);
SET #Confirmation = replace(replace(replace(convert(varchar(16),CURRENT_TIMESTAMP,120),'-',''),' ',''),':','')+convert(varchar(24),#ReportID)
--PRINT #Confirmation
SET NOCOUNT ON; -- prevent rowcount messages
UPDATE Report
SET Confirmation = #Confirmation
WHERE ReportID = #ReportID;
--RETURN #Confirmation
SET NOCOUNT OFF; -- re-enable for the following select
SELECT #Confirmation; -- this is the value you get
--END
GO

How Can i Let Stored Procedure Returns Varchar Value?

Here is my sample:
ALTER PROCEDURE EmpFirstName
#myParam int,
#empFName varchar(20) output
AS
BEGIN
SET NOCOUNT ON;
SELECT #empFName = empfname
FROM FE_QEMP
WHERE empno = #myParam
END
GO
myParam is the input and empFName will carry the output, so the procedure
should only take 1 parameter since empFName is the output, but in my case
i get this error:
Msg 201, Level 16, State 4, Procedure
EmpFirstName, Line 0 Procedure or
function 'EmpFirstName' expects
parameter '#empFName', which was not
supplied.
This is the way i called the procedure:
DECLARE #returnValue varchar(20)
EXEC #returnValue = EmpFirstName 10
SELECT 'Return Value ' = #returnValue
Return values and output parameters are two different things. If you want to call it with an output parameter, you'd do it like this:
EXEC EmpFirstName 10, #returnValue OUTPUT
SELECT 'Return Value ' + #returnValue
If you want to call it in the manner that you described in your example, then you need to alter the stored procedure to state RETURNS VARCHAR(20) and remove the output parameter. To return a value, you have to explicitly call return. In your example, you'd declare a variable, assign it in the select statement, then call return #varName.
Thanks. My aha moment came with this post. Did not realise that output parameters need to be qualified with the "output" identifier too when executed, not just in the procedure!
Here are my test workings for my fellow sql server noobs. I am using sqlcmd with sql server 2005.
The stored procedure:
/* :r procTest.sql */
if exists (select name from sysobjects where name="procTest" and type="P")
drop procedure procTest;
go
create procedure procTest
/* Test stored procedure elements. */
(#i_pt_varchar varchar(20),
#o_pt_varchar varchar(20) output)
as
begin
print "procTest";
set #o_pt_varchar = "string coming out";
print "#i_pt_varchar " + #i_pt_varchar;
print "#o_pt_varchar " + #o_pt_varchar;
return (0);
end
go
The test call:
/* :r procTest.test.sql */
declare #returnFlag int;
declare #i_varchar varchar(20);
declare #o_varchar varchar(20);
set #i_varchar = "string going in";
set #o_varchar = null;
execute #returnFlag = procTest #i_varchar, #o_varchar output
print "#returnFlag " + cast(#returnFlag as varchar(20));
print "after call";
print "#i_varchar " + #i_varchar;
print "#o_varchar " + #o_varchar;
go