简体   繁体   English

SQL 唯一标识符到 varchar

[英]SQL Uniqueidentifier to varchar

Im trying to achieve this in SQL Server:我试图在 SQL Server 中实现这一点:

exec proc_sample ''

but there is an error:但有一个错误:

Error converting data type varchar to uniqueidentifier.将数据类型 varchar 转换为 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.我知道问题是 OR @ID = '',但不知道如何解决。

If you would like to pass a NULL parameter into the procedure then it should be assigned the default value NULL.如果您想将 NULL 参数传递到过程中,则应为其分配默认值 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

...just change the input @ID parameter to a character type ...只需将输入@ID 参数更改为字符类型

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM