Jomarie
Jomarie

Reputation: 3

SQL Uniqueidentifier to varchar

Im trying to achieve this in SQL Server:

exec proc_sample ''

but there is an error:

Error converting data type varchar to uniqueidentifier. Heres my sample proc:

CREATE PROCEDURE [dbo].[proc_sample]
( @ID uniqueidentifier )
AS BEGIN
IF
@ID IS NULL
OR @ID = ''
SELECT * FROM table1
ELSE
SELECT * FROM table2
END

I know that the issue is the OR @ID = '', but not sure how to fix it.

Upvotes: 0

Views: 637

Answers (2)

lptr
lptr

Reputation: 6808

...just change the input @ID parameter to a character type

CREATE PROCEDURE [dbo].[proc_sample]
( @ID varchar(10))
AS BEGIN
IF
@ID IS NULL
OR @ID = ''
SELECT * FROM table1
ELSE
SELECT * FROM table2
END

Upvotes: 0

SteveC
SteveC

Reputation: 6015

If you would like to pass a NULL parameter into the procedure then it should be assigned the default value NULL.

CREATE PROCEDURE [dbo].[proc_sample]
( @ID uniqueidentifier=NULL )
AS BEGIN
IF
@ID IS NULL
OR @ID = ''
SELECT * FROM table1
ELSE
SELECT * FROM table2
END

Upvotes: 3

Related Questions