Sql 循环存储过程结果以创建字符串
我有一个存储过程,它返回两列,如下所示: 我想知道是否可以从结果集的iTypeID列创建一个逗号分隔的字符串。我只是不确定如何循环通过该结果集来创建字符串,我希望字符串以Sql 循环存储过程结果以创建字符串,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我有一个存储过程,它返回两列,如下所示: 我想知道是否可以从结果集的iTypeID列创建一个逗号分隔的字符串。我只是不确定如何循环通过该结果集来创建字符串,我希望字符串以 @sqlString = '1,2,4,11,14' 作为我的参数值。生成上表的存储过程调用为 exec cfn_PlanningGoalGetType 0,'xmlString' 不知道这是否有帮助,或者是否足够清晰,但任何方向都值得赞赏。这可以创建表变量,从存储过程中插入数据,然后选择输出到一行字符串中 DECLAR
@sqlString = '1,2,4,11,14'
作为我的参数值。生成上表的存储过程调用为
exec cfn_PlanningGoalGetType 0,'xmlString'
不知道这是否有帮助,或者是否足够清晰,但任何方向都值得赞赏。这可以创建表变量,从存储过程中插入数据,然后选择输出到一行字符串中
DECLARE TABLE @tmpBus
(
iTypeId INT,
Desc INT
)
INSERT INTO @tmpBus
EXEC cfn_PlanningGoalGetType 0,'xmlString'
DECLARE @CodeNameString varchar(100)
SELECT
@CodeNameString = STUFF( (SELECT ',' + iTypeID
FROM @tmpBus
ORDER BY Sort
FOR XML PATH('')),
1, 1, '')
你可以用
对于您的实际用例,这里有两个不同的选项,我将使用Coalesce或我的版本而不使用:
CREATE TABLE [dbo].[Test](
[Id] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO Test (Id)
VALUES (1),(4),(7),(15)
declare @val nvarchar(max)
set @val=''
select @val=@val +',' + cast(id as nvarchar) from test
select substring(@val,2,len(@vaL))
另一个选项是构建聚合器,如果要聚合结果,或者希望在结果集中返回分隔列,则该聚合器非常有用。当我想返回1>N关系时,我不得不这样做,但对于UI中的分页,我们只想返回数据的一小部分。假设我们想要返回10个联系人和用户在每个联系人上定义的自定义标记
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
public struct StringConcat : IBinarySerialize
{
private System.Collections.Generic.List<string> values;
public void Init()
{
this.values = new System.Collections.Generic.List<string>();
}
public void Accumulate(SqlString value)
{
if (!value.IsNull)
{
this.values.Add(value.Value);
}
}
public void Merge(StringConcat value)
{
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate()
{
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r)
{
int itemCount = r.ReadInt32();
this.values = new System.Collections.Generic.List<string>(itemCount);
for (int i = 0; i <= itemCount - 1; i++)
{
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w)
{
w.Write(this.values.Count);
foreach (string s in this.values)
{
w.Write(s);
}
}
}
这会让我在临时表中执行存储过程,对吗?我只是想确定我不能以某种方式避免/避免这样做,但我同意你的建议,谢谢你谢谢你的帮助!有了这个和汉普蒂的答案,这将是完美的
[Serializable]
[SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
public struct StringConcat : IBinarySerialize
{
private System.Collections.Generic.List<string> values;
public void Init()
{
this.values = new System.Collections.Generic.List<string>();
}
public void Accumulate(SqlString value)
{
if (!value.IsNull)
{
this.values.Add(value.Value);
}
}
public void Merge(StringConcat value)
{
this.values.AddRange(value.values.ToArray());
}
public SqlString Terminate()
{
return new SqlString(string.Join(", ", this.values.ToArray()));
}
public void Read(BinaryReader r)
{
int itemCount = r.ReadInt32();
this.values = new System.Collections.Generic.List<string>(itemCount);
for (int i = 0; i <= itemCount - 1; i++)
{
this.values.Add(r.ReadString());
}
}
public void Write(BinaryWriter w)
{
w.Write(this.values.Count);
foreach (string s in this.values)
{
w.Write(s);
}
}
}
select dbo.stringcontact(id),someotherfield
from mytable group by someotherfield