Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

stored procedure not returning uniqueidentifier

Posted on 2010-09-06
10
Medium Priority
?
1,004 Views
Last Modified: 2012-05-10
hi there,

id like the stored procedure bellow to return a uniqueidentifier. however, its returning an integer and i get the following exception

"Msg 206, Level 16, State 2, Procedure GetUserIdByUserName, Line 18
Operand type clash: uniqueidentifier is incompatible with int"

ALTER PROCEDURE [dbo].[GetUserIdByUserName]

(
	@UserName nvarchar(256)
)
AS
	SET NOCOUNT ON;

DECLARE	@return_value uniqueidentifier 

set 

@return_value = (SELECT UserId as userID
				FROM aspnet_Users
				where UserName = @UserName)
				
				
return @return_value;

Open in new window

0
Comment
Question by:RachelDosSantos
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 33612229
dsee what datatype is userid field. You have a datatype conversion error:  I think field is defined as int, but your variable is defined as uniqueidentifier. Change it to int.

Hope it helps.
0
 

Author Comment

by:RachelDosSantos
ID: 33612238
userid field is uniqueidentifier and that is the type i need this procedure to return
0
 

Author Comment

by:RachelDosSantos
ID: 33612250
CREATE TABLE [dbo].[aspnet_Users](
      [ApplicationId] [uniqueidentifier] NOT NULL,
      [UserId] [uniqueidentifier] NOT NULL,
      [UserName] [nvarchar](256) NOT NULL,
....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Expert Comment

by:gplana
ID: 33612306
does this query
SELECT UserId as userID
FROM aspnet_Users

works on your system ? If not, try this one
 SELECT [UserID]
FROM aspnet_Users

if this works,change the query inside your procedure and it should work
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33612445
This would suffice:

or a simple SELECT statement would do instead of a procedure:
SELECT UserId as userID
FROM aspnet_Users
where UserName = ?
ALTER PROCEDURE [dbo].[GetUserIdByUserName]

(
	@UserName nvarchar(256)
)
AS

SELECT UserId as userID
FROM aspnet_Users
where UserName = @UserName

GO

Open in new window

0
 
LVL 7

Accepted Solution

by:
lundnak earned 2000 total points
ID: 33614377
You can only return a datatype of INT from a stored procedure.  You either need to create an output parameter or change the SPROC to a Scalar-based User Defined Function.

This is the example of an output parameter.

You call the sproc this way:

Declare @UserID uniqueidentifier
Exec GetUserIDByUserName @UserName = 'Joe', @Return_Value=@UserID output
ALTER PROCEDURE [dbo].[GetUserIdByUserName]

(
	@UserName nvarchar(256),
        @Return_Value uniqueidentifier output
)
AS
	SET NOCOUNT ON;

DECLARE	@return_value uniqueidentifier 

set 

@return_value = (SELECT UserId as userID
				FROM aspnet_Users
				where UserName = @UserName)

Open in new window

0
 
LVL 7

Expert Comment

by:Inteqam
ID: 33614834
ALTER PROCEDURE [dbo].[GetUserIdByUserName]

(
      @UserName nvarchar(256)
)
AS
      SET NOCOUNT ON;

DECLARE      @return_value uniqueidentifier

set

SELECT @return_value= UserId
                        FROM aspnet_Users
                        where UserName = @UserName
                        
                        
return @return_value;
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33623986
Inteqam,

You may want to double check that.
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 33624236
yeah, my mistake.

lundnak is right, see http://msdn.microsoft.com/en-us/library/ms174998.aspx
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 33624242
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question