Solved

stored procedure not returning uniqueidentifier

Posted on 2010-09-06
10
921 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 21
Date conversion in sql server 2012 6 26
SQL Help - 12 39
Re-position the objects 7 50
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

757 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

14 Experts available now in Live!

Get 1:1 Help Now