sql server列转行的几种方法
方法一,临时变量:
declare @temp nvarchar(max)='' select @temp=coalesce(@temp,'')+Location+','
from(
select distinct Location from dbo.WG_SP_IllegalBroadcast
) a set @temp=substring(@temp,1,len(@temp)-1) print @temp
方法二:利用xml path操作:
select stuff((select distinct ','+Location from dbo.WG_SP_IllegalBroadcast
for xml path('')),1,1,'')
方法三:利用游标操作:
使用游标的顺序: 声名游标、打开游标、读取数据、关闭游标、删除游标。
declare @location nvarchar(50)
declare cursor_fruit CURSOR for
select distinct Location from dbo.WG_SP_IllegalBroadcast
open cursor_fruit
fetch next from cursor_fruit
into @location
while @@FETCH_STATUS=0
begin
print @location
FETCH NEXT FROM cursor_fruit
into @location
end close cursor_fruit
deallocate cursor_fruit
sql server列转行的几种方法的更多相关文章
- SQL Server遍历表的几种方法 转载
SQL Server遍历表的几种方法 阅读目录 使用游标 使用表变量 使用临时表 在数据库开发过程中,我们经常会碰到要遍历数据表的情形,一提到遍历表,我们第一印象可能就想到使用游标,使用游标虽然直观易 ...
- SQL Server 优化存储过程的七种方法
原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...
- Sql Server 列转行 Pivot使用
今天正好做 数据展示,用到了列转行,行转列有多种方式,Pivot是其中的一种,Povit 是sql server 2005以后才出现的功能, 下面的业务场景: 每个月,进货渠道的总计数量[Total] ...
- SQL Server 创建索引的 5 种方法
前期准备: create table Employee ( ID int not null primary key, Name nvarchar(4), ...
- SQL Server遍历表的几种方法
在数据库开发过程中,我们经常会碰到要遍历数据表的情形,一提到遍历表,我们第一印象可能就想到使用游标,使用游标虽然直观易懂,但是它不符合面向集合操作的原则,而且性能也比面向集合低.当然,从面向集合操作的 ...
- 查看sql server数据库连接数的三种方法
怎样才能查看sql server数据库连接数呢?下面就将为您介绍三种查看的方法,供您参考,希望能够帮助到您. 1.通过系统的“性能”来查看:开始->管理工具->性能(或者是运行里面输入 m ...
- oracle 行转列~列转行(几种方法)
工作中,我们经常会碰到行转列的情况 这里我介绍几种简单的方法--行转列 1.oracle的pivot函数 原表 使用pivot函数: with temp as(select '四川省' nation ...
- SQL Server 索引重建的 4 种方法
解决方法 方法 1. 重建指定索引,这种方法没有性能可谈.重建时表还不可访问. 方法 2. 在线重建索引,只有SQL Server 企业版才支持. 方法 3. 使用填充因子重建,这样做不一定可以减小查 ...
- sql server deadlock跟踪的四种方法
最近写程序常会遇到deadlock victim,每次一脸懵逼.研究了下怎么跟踪,写下来记录下. 建测试数据 CREATE DATABASE testdb; GO USE testdb; CREATE ...
随机推荐
- oracle 从select的结果update其他表
update a set a.id=(selelct b.id from temp b where b.line = a.line) where a.line = (select line from ...
- Hadoop 2.x简介
Hadoop 2.0产生背景 Hadoop1.0中HDFS和MapReduce在高可用.扩展性等方面存在问题 HDFS存在的问题 NameNode单点故障,难以应用于在线场景 NameNode压力过大 ...
- Ogre的Singleton实现-模版实现,便于重用-(详细分析)以及笔者的改进
转自:http://www.cppblog.com/sandy/archive/2005/11/30/1436.html ;Root g_root;//must declare once only / ...
- Oracle数据库操作语言(DML)
--insert添加语句 insert into table_name(column_name,column_name,...) values (data1,data2,...); --通过表添加数据 ...
- node路由访问,中间件返回数据
node路由访问,中间件返回数据 定义一个变量存放json数据,中间件接受数据 var responseData; router.use(function(req, res, next) { resp ...
- Linux下c++11多线程聊天室
刚看的c++11多线程,写个聊天室试试编译的时候加上 c++11 和 多线程库g++ -Wall -std=c++0x -pthread -o server server.cppserver 和cli ...
- H264视频编码成MP4文件
firehood的专栏 Wince嵌入式开发 目录视图 摘要视图 订阅 赠书 | AI专栏(AI圣经!<深度学习>中文版) 每周荐书:Kotlin.分布式.Keras ...
- POJ3565 Ants 和 POJ2195 Going Home
Ants Language:Default Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7975 Accepted: ...
- 移植memtester到android平台
硬件搭建起来能进入系统,首要就是测试内存的稳定性,需要一款内存测试工具. 一般都是选择memtester这款linux软件,下载地址如下:http://pyropus.ca/software/memt ...
- GrayCode for state machine
How & Why use Gray Code A gray counter is a binary counter where only one bit changes at a time. ...