Solved

stored procedure not returning uniqueidentifier

Posted on 2010-09-06
10
937 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
 
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 7

Accepted Solution

by:
lundnak earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now