mysql - 如何在 SToredpeocedure 中创建循环并创建 , 字符串

标签 mysql sql-server stored-procedures

我有一个表,其中包含一些数据,例如 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函数就可以了,但是如何实现这个请指导我

最佳答案

使用类似的东西。这是 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;

关于mysql - 如何在 SToredpeocedure 中创建循环并创建 , 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27619574/

相关文章:

oracle - 存储过程 PL SQL 如果字符串为空

sql - 用于逆透视的存储过程

IF 语句中 MySQL SELECT 语法错误

php - 从 mysql 表中读取唯一数据

sql - 将 sql 结果导出到 CSV 时如何在单个单元格中使用逗号分隔值

sql - 循环遍历临时表的所有行并为每一行调用一个存储过程

mysql - SQL 连接 with where 和having count() 条件

mysql - 强制表参与的最佳方式

sql - 从 Getdate() 获取时间

sql - 如何检查SQL Server表中是否存在特定记录?