笔记283 游标中如何实现 动态SQL 2013-6-8

笔记283 游标中如何实现 动态SQL 2013-6-8

 1 --游标中如何实现 动态SQL 2013-6-8
 2 USE [GPOSDB]
 3 GO
 4 DECLARE @cmd NVARCHAR(MAX)
 5 DECLARE @index INT
 6 SET @index=1
 7 IF ( 1 = 1 )
 8     BEGIN
 9         SET @cmd = 'select * from dbo.CT_InhouseCard where I_IC_CardType='+CAST(@index AS NVARCHAR(100))
10     END
11 ELSE
12     BEGIN
13         SET @cmd = 'select * from dbo.CT_InhouseCard where I_IC_CardType='+CAST(@index AS NVARCHAR(100))
14     END
15 DECLARE @cur CURSOR
16 DECLARE @sql NVARCHAR(MAX)
17 SET @sql = N'SET @cur = CURSOR FOR ' + @cmd + N'; OPEN @cur;';
18 EXEC sp_executesql @sql, N'@cur CURSOR OUTPUT', @cur OUTPUT;
19 FETCH @cur;
20 CLOSE @cur;

 

posted @ 2013-08-04 20:53 桦仔 阅读(...) 评论(...)  编辑 收藏