【问题标题】:How to Create a Loop in SToredpeocedure and make a , string如何在 SToredpeocedure 中创建循环并制作字符串
【发布时间】:2014-12-23 11:35:41
【问题描述】:
我有一个表,其中包含一些数据,例如 id、Name、Forid
现在我想获取单个变量中的所有名称,像这样 a,b,c 表示
但它的 forid 是匹配的
前:-
Id Name forid
1 a 1
2 b 1
3 c 2
当 forid 为 1 时 sp 将返回 a,b
如果有sql函数没问题,但是如何制作请指导我
【问题讨论】:
标签:
mysql
sql-server
stored-procedures
【解决方案1】:
使用类似的东西。这是 oracle 中的过程,但在 SQL Server 中我认为您不需要更多更改
create or replace PROCEDURE p (x in number) AS
z table_name.Column_name%TYPE;
result varchar(100):='';
cursor take is select Name from TABLE_NAME where for_id=x;
begin
open take;
loop
fetch take into z;
result:= result|| z;
exit when take%notfound;
end loop;
close take;
dbms_output.put_line(result);
END p;