为什么我的存储过程没有收到其参数?

时间:2013-10-29 00:51:57

标签: sql sql-server tsql

我有以下存储过程:

create procedure new (@user nvarchar(50))
as
Declare @ids uniqueidentifier  
set @ids = (
select id from table6 where name = @user and @ids = id)

SELECT  * from table1 as 1, table2 as 2
where 1.id = @ids

它没有返回正确的结果 - 它什么也没有返回。它似乎将变量(@ids)传递为空。

2 个答案:

答案 0 :(得分:1)

您获得空结果的原因是您在为其分配任何内容之前尝试使用@ids。在您获得@ids值的查询中,您使用它来过滤掉@ids = id的记录,但当时@idsnull,结果将为空@ids将保留null

我假设您只想删除该部分条件,除非您有其他值可用于比较id字段。

无论如何,我看不出你怎么能创建程序......你不能使用数字作为别名,使用标识符:

SELECT * from table1 as t1, table2 as t2
where t1.id = @ids

答案 1 :(得分:0)

传递 @ids - 您在本地声明。由于它没有任何价值,当你在WHERE子句中使用时,你没有得到任何记录,因此@ids将为NULL。

我想你想要

set @ids = (
select id from table6 where name = @user)

但你甚至不需要 - 只是这样做:

SELECT  * from table1 t1, table2 t2
where t1.id = (select id from table6 where name = @user)

CROSS JOIN返回T1和T2的每个记录组合。 - 这可能是你想要的,只是想确定指出这一点。