0

我有以下存储过程:

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) 作为空传递。

4

2 回答 2

1

您得到空结果的原因是您@ids在分配任何内容之前尝试使用它。在查询中,您获取值的位置为@ids您使用它来过滤掉记录在哪里@ids = id,但当时结果将是空的并且@ids将保持不变。null@idsnull

我假设您只想删除条件的那一部分,除非您有一些其他值可以用来比较该id字段。

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

SELECT * from table1 as t1, table2 as t2
where t1.id = @ids
于 2013-10-29T01:03:51.727 回答
0

你没有通过 @ids- 你在本地声明它。由于它没有价值,当您在WHERE子句中使用 in 时,您不会得到任何记录,因此@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 的记录的每个组合。- 这可能是你想要的,只是想确保指出这一点。

于 2013-10-29T01:03:58.907 回答