我必须使用游标来满足此要求,但是在声明CURSOR并根据我的要求使用之后如何将其导出到其他表,或者是他们的任何其他替代方案来实现这个目标因为游标性能问题?? 请尽快赐教。 在此先感谢!! 解决方案 声明 @ids varchar ( 50 ), @qNameIdCSV varchar (max)= ' ' 声明 RandomCursor cursor for 选择 ID 来自 TableA order by NEWID() open RandomCursor fetch next 从 RandomCursor 进入 @ ids while @@ FETCH_STATUS = 0 begin - print @ids set @ qNameIdCSV = @ qNameIdCSV + ' ,' + @ ids 获取 从 RandomCursor 进入 @ ids end close RandomCursor deallocate RandomCursor set @ qNameIdCSV = SUBSTRING( @ qNameIdCSV , 2 ,len( @ qNameIdCSV ) - 1) 打印 @ qNameIdCSV - 检查所需输出 或 insert 进入 tableA(RandomID)值( @ qNameIdCSV ) - 这将使用NEWID()将随机生成的ID保存到CSV格式的所需列中 I''ve to create Sproc for below requirement....I''ve got two table:TableA Contains: ID (identity column primary key)TableB contains: RandomID (This column will store ID that will be generated after randomly sorting Table A "ID" using NEWID() )Now, my requirement is After random sorting of all "ID" from "TableA" that Id must be stored in the column "RandomID" of TableB It means : If I''m generating the Random No. i.e"select ID from TableA order by NewID()"Output of above query as: 3 2 4 5 2 . . . 19then, how to export the above output into other column i.e "RandomID" of TableB in CSV form ?? i.e "RandomID column of TableB" must store in the form of3,2,4,5....,,19I''ve to use cursor for this requirement but how to export it to other table after declaring CURSOR and to use according to my requirement, or is their any other alternative to achieve this goal because of Cursor performance issue ??Please, enlighten me soon.Thanks In advance !! 解决方案 declare @ids varchar(50),@qNameIdCSV varchar(max)='' declare RandomCursor cursor for select ID from TableA order by NEWID() open RandomCursor fetch next from RandomCursor into @ids while @@FETCH_STATUS=0 begin --print @ids set @qNameIdCSV=@qNameIdCSV+','+@ids fetch next from RandomCursor into @ids end close RandomCursor deallocate RandomCursor set @qNameIdCSV=SUBSTRING(@qNameIdCSV,2,len(@qNameIdCSV)-1) print @qNameIdCSV --check the required outputor insert into tableA (RandomID) values (@qNameIdCSV) -- this will save the random generated ID using NEWID() into the required column in CSV form 这篇关于从表的列中获取所有记录,并在sql server中以CSV格式导出到表的其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 19:16