自己整了半天才弄好,写成博客纪念下:

这个是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游标应用的更多相关文章

  1. sql 游标例子 根据一表的数据去筛选另一表的数据

    sql 游标例子 根据一表的数据去筛选另一表的数据 DECLARE @MID nvarchar(20)DECLARE @UTime datetime DECLARE @TBL_Temp table( ...

  2. sql 游标循环当中重新赋值

    sql 游标循环当中的变量必须重新赋值不然变量的值就是前次循环的值

  3. SQL 游标的应用

    ----------------SQL游标应用-----------------if object_id('tempdb..#test0001') is not null drop table #te ...

  4. PL/SQL 游标 (实验七)

    PL/SQL 游标 emp.dept 目标表结构及数据 要求 基于部门表建立游标dept_cursor1,使用记录变量接收游标数据,输出部门表信息: 显示格式: 部 门 号: XXX 部门名称: XX ...

  5. PL/SQL游标详解

    刚打开游标的时候,是位于一个空行,要用fetch into 才能到第一行. 只是要注意用更新游标的时候,不能在游标期间commit. 否则会报ORA-01002: fetch out of seque ...

  6. 网上看到一份详细sql游标说明 《转载 https://www.cnblogs.com/xiongzaiqiren/p/sql-cursor.html》

     SQL游标(cursor)详细说明及内部循环使用示例 游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获 ...

  7. Oracle PL/SQL游标

    游标的提出: SQL是面向集合的,其结果一般是集合量(多条记录),而PL/SQL的变量一本是标量,其一组变量异常一直只能存放一条记录.所以仅仅使用变量并不能完全满足SQL语句向应用程序输出数据的要求. ...

  8. PL/SQL 游标

    本随笔不是原创,只是学习笔记,用于加深记忆,原创地址PL/SQL --> 游标 一.游标的相关概念和特性 1.定义: 映射到结果集中的某一行的特定位置,类似与C语言中的指针.即通过游标方式定位到 ...

  9. Library Cache优化与SQL游标

    Library Cache主要用于存放SQL游标,而SQL游标最大化共享是Library Cache优化的重要途径,可以使SQL运行开销最低.性能最优. 1 SQL语句与父游标及子游标 在PL/SQL ...

  10. SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总

    SQL Server游标   转载自:http://www.cnblogs.com/knowledgesea/p/3699851.html. 什么是游标 结果集,结果集就是select查询之后返回的所 ...

随机推荐

  1. .NET Framework 4 中的并行编程9---线程安全集合类

    原文转载自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206745.html 在.Net 4中,新增System.Collections. ...

  2. spring mvc 的Controller类默认Scope是单例(singleton)的

    使用Spring MVC有一段时间了,之前一直使用Struts2,在struts2中action都是原型(prototype)的, 说是因为线程安全问题,对于Spring MVC中bean默认都是(s ...

  3. Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)

    控制台程序. public class Person implements Comparable<Person> { // Constructor public Person(String ...

  4. Leetcode: Binary Watch

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  5. Leetcode: Palindrome Pairs

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. codevs 1206 保留两位小数

    http://codevs.cn/problem/1206/ 1206 保留两位小数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  7. [转]史上最全的MSSQL复习笔记

    阅读目录 1.什么是SQL语句 2.使用sql语句创建数据库和表 3.创建数据表 4.数据完整性约束 5.四中基本字符类型说明 6.SQL基本语句 7.类型转换函数 8.日期函数 9.数学函数 10. ...

  8. 数据库SQL CRUD

    1.删除表 drop  table +表名 2.修改表 alter  table+表名+ add(添加)+列名+ int(类型) alter  table+表名+ drop(删除)+column(列) ...

  9. javax.servlet.jsp.JspException cannot be resolved to a type

    javax.servlet.jsp.PageContext cannot be resolved to a type javax.servlet.jsp.JspException cannot be ...

  10. 数据库中is null(is not null)与=null(!=null)的区别

    在标准SQL语言(ANIS SQL)SQL-92规定的Null值的比较取值结果都为False,既Null=Null取值也是False.NULL在这里是一种未知值,千变万化的变量,不是“空”这一个定值! ...