1

是否可以在 T SQL 中使用函数作为 while 参数?我有以下代码:

    create function LoginExists (@p_login char(6))
returns varchar(5)
begin
    if exists(select * from Student where student.slogin = @p_login)
        return 'true'
        return 'false'
end;

    create procedure AddStudent2
(@p_fname varchar(30), @p_lname varchar(50), @p_tallness int)
as
declare @p_login char(6), @p_email varchar(50), @suffix char(3);
set @p_lname = LOWER(@p_lname);
set @suffix = '000';
begin
    set @p_login = substring(@p_lname,1, 3) + @suffix;
    while (LoginExists @p_login = 'true')
        set @suffix = cast((CAST(@suffix as int) + 1) as char(3));
            set @p_login = substring(@p_lname,1, 3) + @suffix;
    set @p_email = @p_login + '@vsb.cz';
    insert into Student values (@p_login, @p_fname, @p_lname, @p_email, @p_tallness);
end;

并且在尝试编译它时,会发生错误,说:“'@p_login' 附近的语法不正确。”

4

2 回答 2

2

您调用函数的语法实际上是不正确的:

代替:

while (LoginExists @p_login = 'true')

和:

while (dbo.LoginExists (@p_login)= 'true')
于 2013-03-25T13:49:15.930 回答
-1

代替

if exists(select * from Student where student.slogin = @p_login)

经过

 if exists(select * from Student where Student.slogin = @p_login)

上“S” student.slogin ,我希望它有效

于 2013-03-25T13:22:31.900 回答