sqlserver 預存程序中拼接sql語句 動態執行

來源:互聯網
上載者:User

標籤:blog   os   ar   art   div   log   sql   ad   

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

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.