SQL游标应用
自己整了半天才弄好,写成博客纪念下:
这个是sql上写的测试用:
declare @CODE varchar(100)
declare @TITLE varchar(100)
declare @TABLE varchar(100)
declare @type varchar(100) declare @sql varchar(max)
set @sql=''
SET @type='index_02'
declare myCur cursor for
SELECT distinct CONTENT_TYPE,CONTENT_CODE_NAME,CONTENT_TITLE_NAME FROM CT_LOCATOR_CONTENT
WHERE LOCATOR_CODE=@type open myCur
fetch next from myCur into @TABLE,@CODE,@TITLE
while @@FETCH_STATUS <>-1 begin set @sql= @sql+'SELECT CT_LOCATOR_CONTENT.AUTO_ID AS AUTO_ID, CT_LOCATOR_CONTENT.CONTENT_SEQUENCE AS CONTENT_SEQUENCE,CT_LOCATOR_CONTENT.CONTENT_TYPE AS CONTENT_TYPE,'
+@TABLE+'.'+@TITLE+' AS TITLE FROM CT_LOCATOR_CONTENT,'
+@TABLE+' WHERE CT_LOCATOR_CONTENT.LOCATOR_CODE='''+@type
+''' AND CT_LOCATOR_CONTENT.CONTENT_CODE='+@TABLE+'.'+@CODE set @sql=@sql+' union all '
fetch next from myCur into @TABLE,@CODE,@TITLE
end
if len(@sql)>9 Set @sql=SUBSTRING(@sql,0,len(@sql)-9)
else SET @sql=@sql
print(@sql) close myCur
deallocate myCur IF EXISTS (SELECT DISTINCT CONTENT_CODE_NAME FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@type)
BEGIN
EXEC(@sql);
END
ELSE
BEGIN
SELECT '-1' AS AUTO_ID,'-1' AS CONTENT_SEQUENCE,LOCATOR_CATEGORY AS CONTENT_TYPE,'-1' AS TITLE
FROM T_SM_LOCATOR
WHERE LOCATOR_CODE=@type
END
具体代码中的:
/// <summary>
/// 根据内容版位的Code获取版位上的内容信息(创建:001,2013.8.27)
/// </summary>
/// <param name="locatorCode">内容版位的CODE</param>
/// <returns></returns>
public DataTable GetContentTableByLocator(string locatorCode)
{
string sqlText = @"
declare @sql varchar(max)
set @sql='' declare myCur cursor for
SELECT distinct CONTENT_TYPE,CONTENT_CODE_NAME,CONTENT_TITLE_NAME FROM CT_LOCATOR_CONTENT
WHERE LOCATOR_CODE=@LOCATOR_CODE open myCur
fetch next from myCur into @TABLE,@CODE,@TITLE
while @@FETCH_STATUS <>-1 begin set @sql= @sql+'SELECT CT_LOCATOR_CONTENT.AUTO_ID AS AUTO_ID, CT_LOCATOR_CONTENT.CONTENT_SEQUENCE AS CONTENT_SEQUENCE,CT_LOCATOR_CONTENT.CONTENT_TYPE AS CONTENT_TYPE,'
+@TABLE+'.'+@TITLE+' AS TITLE FROM CT_LOCATOR_CONTENT,'
+@TABLE+' WHERE CT_LOCATOR_CONTENT.LOCATOR_CODE='''+@LOCATOR_CODE
+''' AND CT_LOCATOR_CONTENT.CONTENT_CODE='+@TABLE+'.'+@CODE set @sql=@sql+' union '
fetch next from myCur into @TABLE,@CODE,@TITLE
end
IF len(@sql)>5 Set @sql=SUBSTRING(@sql,0,len(@sql)-5)
else SET @sql=@sql
close myCur
deallocate myCur
IF EXISTS (SELECT DISTINCT CONTENT_CODE_NAME FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@LOCATOR_CODE)
BEGIN
EXEC(@sql);
END
ELSE
BEGIN
SELECT '-1' AS AUTO_ID,'-1' AS CONTENT_SEQUENCE,LOCATOR_CATEGORY AS CONTENT_TYPE,'-1' AS TITLE
FROM T_SM_LOCATOR
WHERE LOCATOR_CODE=@LOCATOR_CODE
END
"; SqlParameter[] queryParam = new SqlParameter[]
{
new SqlParameter("@LOCATOR_CODE", SqlDbType.NVarChar),
new SqlParameter("@CODE", SqlDbType.NVarChar),
new SqlParameter("@TITLE", SqlDbType.NVarChar),
new SqlParameter("@TABLE", SqlDbType.NVarChar)
};
queryParam[].Value = locatorCode;
queryParam[].Value = "";
queryParam[].Value = "";
queryParam[].Value = ""; DataSet ds = null;
using(IDbConnection conn = ConnectionConstruct.LoadInterNetConnection())
{
ds = sqlServer.ExecuteDataset(conn, CommandType.Text, sqlText, queryParam);
}
return ds.Tables[];
}
小测试的一并贴上(上面的是多个的,这个是单个的):
declare @temp varchar(100)
declare @sql varchar(max)
set @sql=''
declare myCur cursor for
select a from
(
select 't_hdh_DY' as a
union
select 't_hdh_EY' as a
)as tablenames
open myCur
FETCH NEXT FROM myCur INTO @temp
WHILE @@FETCH_STATUS<>-1
BEGIN
SET @sql=@sql+'SELECT ID FROM '+@temp
SET @sql=@sql+' UNION '
FETCH NEXT FROM myCur INTO @temp
END
SET @sql=SUBSTRING(@sql,0,LEN(@sql)-5)
CLOSE myCur
DEALLOCATE myCur print(@sql)
ps 这个本来是查询一个的,结果有好几个,所以改了。
附上改之前的,作为备份:
string sqlText = @"SET @CODE=(SELECT TOP 1 CONTENT_CODE_NAME FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@LOCATOR_CODE)
SET @TITLE=(SELECT TOP 1 CONTENT_TITLE_NAME FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@LOCATOR_CODE)
SET @TABLE=(SELECT TOP 1 CONTENT_TYPE FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@LOCATOR_CODE)
IF EXISTS (SELECT TOP 1 CONTENT_CODE_NAME FROM CT_LOCATOR_CONTENT WHERE LOCATOR_CODE=@LOCATOR_CODE)
BEGIN
EXEC('SELECT CT_LOCATOR_CONTENT.AUTO_ID AS AUTO_ID, CT_LOCATOR_CONTENT.CONTENT_SEQUENCE AS CONTENT_SEQUENCE,CT_LOCATOR_CONTENT.CONTENT_TYPE AS CONTENT_TYPE,'+@TABLE+'.'+@TITLE+' AS TITLE FROM CT_LOCATOR_CONTENT,'+@TABLE+' WHERE CT_LOCATOR_CONTENT.LOCATOR_CODE='''+@LOCATOR_CODE+''' AND CT_LOCATOR_CONTENT.CONTENT_CODE='+@TABLE+'.'+@CODE);
END
ELSE
BEGIN
SELECT '-1' AS AUTO_ID,'-1' AS CONTENT_SEQUENCE,LOCATOR_CATEGORY AS CONTENT_TYPE,'-1' AS TITLE
FROM T_SM_LOCATOR
WHERE LOCATOR_CODE=@LOCATOR_CODE
END
";
SQL游标应用的更多相关文章
- sql 游标例子 根据一表的数据去筛选另一表的数据
sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...
- sql 游标循环当中重新赋值
sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值
- SQL 游标的应用
----------------SQL游标应用-----------------if object_id('tempdb..#test0001') is not null drop table #te ...
- PL/SQL 游标 (实验七)
PL/SQL 游标 emp.dept 目标表结构及数据 要求 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息: 显示格式: 部 门 号: XXX 部门名称: XX ...
- PL/SQL游标详解
刚打开游标的时候,是位于一个空行,要用fetch into 才能到第一行. 只是要注意用更新游标的时候,不能在游标期间commit. 否则会报ORA-01002: fetch out of seque ...
- 网上看到一份详细sql游标说明 《转载 https://www.cnblogs.com/xiongzaiqiren/p/sql-cursor.html》
SQL游标(cursor)详细说明及内部循环使用示例 游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获 ...
- Oracle PL/SQL游标
游标的提出: SQL是面向集合的,其结果一般是集合量(多条记录),而PL/SQL的变量一本是标量,其一组变量异常一直只能存放一条记录.所以仅仅使用变量并不能完全满足SQL语句向应用程序输出数据的要求. ...
- PL/SQL 游标
本随笔不是原创,只是学习笔记,用于加深记忆,原创地址PL/SQL --> 游标 一.游标的相关概念和特性 1.定义: 映射到结果集中的某一行的特定位置,类似与C语言中的指针.即通过游标方式定位到 ...
- Library Cache优化与SQL游标
Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
SQL Server游标 转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...
随机推荐
- fpm来制作rpm包
转自 http://blog.halfss.com/blog/2013/02/26/fpmbao-guan-li/ 另查看 http://my.oschina.net/lxcong/blog/1438 ...
- log4j2.x 配置文件默认寻找顺序
Automatic Configuration Log4j has the ability to automatically configure itself during initializatio ...
- MS SQL SERVER 锁研究记录
首先创建一直数据表 ChenJi,有如下字段: ID, DanWeiID, Name, ChenJi CREATE TABLE [dbo].[ChenJi]( [ID] [int] NOT NUL ...
- 使用Java创建RESTful Web Service
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- pg_rewind 介绍
pg_rewind—使一个PostgreSQL数据目录与另一个数据目录(该目录从第一个PostgreSQL数据目录创建而来)一致. 描述 pg_rewind是一个在集群的时间线参数偏离之后,用于使一个 ...
- Leetcode: Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- JS语法部分-数组
数组的长度是动态变化的,里面可以防止任意类型的元素 var a=new Array() 数组元素的复制:a[0]=123 a[2]=456 数组的取值:a[i] 数组的属性: a.le ...
- JS,CSS,HTML制作网页首页,视频轮播,隐藏点击等等。
在整个项目中,总共写了1000+的代码,可以更加简单优化的.整个主页交互效果能基本,包括轮播,视频,点击变化形状,移入蒙版,瀑布流加载滑动,旋转等等.轮播导航没有完全做完,暂时做了往右无限推动.个人觉 ...
- 20145207《Java程序设计》第6周学习总结
教材学习内容总结 一.输入/输出 InputStream与Outputstream • 串流设计的概念 从应用程序角度看,将数据从来源取出,可以使用输入串流,将数据写入目的地,可以使用输出串流:在Ja ...
- poj: 1207
好吧这题竟然还有先大后小的可能,能不这么恶心下吗.. #include <iostream> #include <stdio.h> #include <string.h& ...