博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqlserver 存储过程中拼接sql语句 动态执行
阅读量:6411 次
发布时间:2019-06-23

本文共 3586 字,大约阅读时间需要 11 分钟。

ALTER PROC [dbo].[Student_Friend_Get]        @startRowIndexId INT,        @maxNumberRows  INT,        @schoolId  INT,        @gradeId  INT,        @cId  INT,        @keyWords NVARCHAR(100),        @userName VARCHAR(50)         AS                BEGIN                    DECLARE @sqlfilter  VARCHAR(max)                    SET @sqlfilter = ' '                    IF(@schoolId <> -1)                                SET @sqlfilter = @sqlfilter + '   tableu.SchoolId =  ' + CAST(@schoolId AS VARCHAR(50)) + ' AND'                        IF(@gradeId <> -1)                                SET @sqlfilter =  @sqlfilter +  '  tableu.GradeId =  ' + CAST(@gradeId AS VARCHAR(50)) + ' AND'                        IF(@cId <> -1)                                SET @sqlfilter =  @sqlfilter +  '  tableu.ClassId =  ' + CAST(@cId AS VARCHAR(50)) + ' AND'                        IF(@keyWords IS NOT NULL)                                SET @sqlfilter =  @sqlfilter + '   tableu.TrueName like  ''%' + CAST(@keyWords AS VARCHAR(50))         + '%''  AND'                                                 DECLARE @beg INT,@end INT                        SET @beg =  @startRowIndexId+1                        SET @end =  @startRowIndexId + @maxNumberRows                        SET @sqlfilter = @sqlfilter +  '  tableu.num  BETWEEN  ' +CAST( @beg AS VARCHAR(50)) + ' AND '+ CAST(@end  AS VARCHAR(50))                                                 DECLARE @sqlmain  VARCHAR(max)                        SET @sqlmain = ' '                        SET @sqlmain = @sqlmain  +  ' SELECT * FROM                        (                            SELECT ROW_NUMBER() OVER(ORDER BY cjs.UserName) AS num,CTA.TrueName, u.UserName, c.ClassName + '' (''+ CAST(YEAR(c.GradeUpdateTime) AS NVARCHAR(20))+''年)'' AS [ClassName],s.SchoolName,cjs.ApplyTime,g.GradeName,cjs.ApplyID,c.ClassId,g.GradeId,s.SchoolId                                        FROM PE_C_StudentJoinClass AS cjs                                        LEFT JOIN dbo.PE_SS_StudentClass AS c                                        ON cjs.ClassId = c.ClassId                                        LEFT JOIN dbo.PE_Users AS u                                        ON u.UserName = cjs.UserName                                        LEFT JOIN dbo.PE_SS_Grade g                                        ON g.GradeId = c.GradeId                                        LEFT JOIN dbo.PE_SS_School s                                        ON s.SchoolId = g.SchoolId                                        LEFT JOIN PE_Contacter CTA                                        ON cjs.UserName = CTA.UserName                                        WHERE ApplyID IN                                        (                                                SELECT                                                  MAX(cs1.ApplyID) AS [ApplyID]                                                FROM PE_C_StudentJoinClass AS cs1                                                CROSS JOIN dbo.PE_C_StudentJoinClass AS cs2                                                WHERE cs2.UserName = '''+ CAST(@userName  AS VARCHAR(50))+ ''' AND cs1.UserName != ''' + CAST(@userName  AS VARCHAR(50))+  ''' AND cs1.ClassId = cs2.ClassId AND cs1.Status = 1                                                GROUP BY cs1.UserName                                        )                                ) AS tableu WHERE '                                                                 PRINT (@sqlmain + @sqlfilter)                            EXEC (@sqlmain + @sqlfilter)                 END  GO

  

转载地址:http://muzra.baihongyu.com/

你可能感兴趣的文章
mysql中show processlist过滤和杀死线程
查看>>
最新Sublime Text 2 激活 汉化
查看>>
基础数据类型之字典
查看>>
第七次作业
查看>>
Oracle中NVARCHAR2与VARCHAR2的区别
查看>>
php debug
查看>>
Ubuntu构建LVS+Keepalived高可用负载均衡集群【生产环境部署】
查看>>
lvm实现快速备份文件及数据库,lvm快照原理
查看>>
设计模式之Factory Method(工厂方法)
查看>>
10K入职linux运维岗位小伙伴感谢信及面试经历分享
查看>>
zookeeper入门之Curator的使用之几种监听器的使用
查看>>
[转]Reporting Service部署之访问权限
查看>>
innerxml and outerxml
查看>>
validform校验框架不显示错误提示
查看>>
flink 获取上传的Jar源码
查看>>
Spring Data JPA Batch Insertion
查看>>
mongodb索引
查看>>
UEditor自动调节宽度
查看>>
JAVA做验证码图片(转自CSDN)
查看>>
Delphi TServerSocket,TClientSocket实现传送文件代码
查看>>