储存进程多规格查询

图片 1

大家都驾驭使用存款和储蓄进度的利益其中有2点

当点击查询后万分全体规则

① 、参数查询安全(用参数就安全?)

图片 2

贰 、存款和储蓄进程在开立刻预编写翻译,执行功能比SQL语句要高

在开始展览多规格查询时得以

 

CREATE PROC Getcourse

基于那两点,看看上边那段从前常常写的囤积进度吧(查询数据)

@Condition

CREATE PROCEDURE [dbo].[usp_TestExec]
    @Account VARCHAR(50)='',
    @UserName VARCHAR(50)=''
AS
BEGIN
    DECLARE @SQL VARCHAR(8000)
    SET @SQL='SELECT * FROM dbo.tb_Test WHERE 1=1';
    IF(ISNULL(@Account,'')<>'') SET @SQL=@SQL+' AND Account LIKE ''%'+@Account+'%'''
    IF(ISNULL(@UserName,'')<>'') SET @SQL=@SQL+' AND UserName LIKE ''%'+@UserName+'%'''

    EXEC(@SQL)

END    

AS

运行,测试没难点。

SELECT * FROM course

不过有什么人会考虑到注入?

WHERE (c# LIKE’%’+@Condition+’%’ OR cname LIKE’%’+@Condition+’%’ OR t#
LIKE’%’+@Condition+’%’)

瞅着存款和储蓄进程本身觉得有五个难点

1.实在依然运转SQL,如果基准不平等,生成的SQL语句不平等,预编写翻译运营成为美观的弥天天津大学学谎

2.不能制止SQL代码注入,不依赖请(测)看(试)

  运转如下代码会出现何种结果?

  

 EXEC usp_TestExec "fdsgdsgsdd' OR '' LIKE '"

图片 3

 

出标题咯,作为2个有先进心的程序员,此时是还是不是听到一句话:该重写了

那边是本人的点子:

 

ALTER PROCEDURE [dbo].[usp_TestExec]
    @Account VARCHAR(50)='',
    @UserName VARCHAR(50)=''
AS
BEGIN
     --usp_TestExec "DB' OR '' LIKE '"
    --usp_TestExec 'DB'
    DECLARE @SQL VARCHAR(8000)
    SELECT * FROM dbo.tb_Test 
    WHERE ((@Account IS NULL AND Account IS NULL) OR @Account='' OR (Account LIKE '%'+@Account+'%')) 
        AND ((@UserName IS NULL AND UserName IS NULL) OR @UserName='' OR (UserName LIKE '%'+@UserName+'%')) 

END    

运行测试一下:

  

exec usp_TestExec "DB' OR '' LIKE '"

图片 4

exec usp_TestExec 'DB'

 

图片 5

 

流入难点消除,而且由于尚未拼接SQL语句,能够说那样的蕴藏进程才预编写翻译后实践查询的。

 

应该还有别的方法。

附上一段SqlCommandBuilder生成的Update语句,给看得懂的人看吗:

UPDATE [dbo].[tb_TT] SET [FRbigint] = @FRbigint
                    , [Fbinary] = @Fbinary
                    , [Fbit] = @Fbit
                    , [Fchar] = @Fchar
                    , [Fdate] = @Fdate
                    , [Fdatetime] = @Fdatetime
                    , [Fdatetime2] = @Fdatetime2
                    , [Fdatetimeoffset] = @Fdatetimeoffset
                    , [Fdecimal] = @Fdecimal
                    , [Ffloat] = @Ffloat
                    , [Fgeography] = @Fgeography
                    , [Fgeometry] = @Fgeometry
                    , [Fhierarchyid] = @Fhierarchyid
                    , [Fimage] = @Fimage
                    , [Fint] = @Fint
                    , [Fmoney] = @Fmoney
                    , [Fnchar] = @Fnchar
                    , [Fntext] = @Fntext
                    , [Fnumeric] = @Fnumeric
                    , [Fnvarchar] = @Fnvarchar
                    , [FnvarcharMax] = @FnvarcharMax
                    , [Freal] = @Freal
                    , [Fsmalldatetime] = @Fsmalldatetime
                    , [Fsmallint] = @Fsmallint
                    , [Fsmallmoney] = @Fsmallmoney
                    , [Fsql_variant] = @Fsql_variant
                    , [Ftext] = @Ftext
                    , [Ftime] = @Ftime
                    , [Ftinyint] = @Ftinyint
                    , [Funiqueidentifier] = @Funiqueidentifier
                    , [Fvarbinary] = @Fvarbinary
                    , [Fvarbinary_Max] = @Fvarbinary_Max
                    , [Fvarchar] = @Fvarchar
                    , [Fvarchar_Max] = @Fvarchar_Max
                    , [Fxml] = @Fxml 
WHERE     (
            ([isid] = @Original_isid) 
        AND ((@IsNull_FRbigint = 1 AND [FRbigint] IS NULL) OR ([FRbigint] = @Original_FRbigint)) 
        AND ((@IsNull_Fbinary = 1 AND [Fbinary] IS NULL) OR ([Fbinary] = @Original_Fbinary)) 
        AND ((@IsNull_Fbit = 1 AND [Fbit] IS NULL) OR ([Fbit] = @Original_Fbit)) 
        AND ((@IsNull_Fchar = 1 AND [Fchar] IS NULL) OR ([Fchar] = @Original_Fchar)) 
        AND ((@IsNull_Fdate = 1 AND [Fdate] IS NULL) OR ([Fdate] = @Original_Fdate)) 
        AND ((@IsNull_Fdatetime = 1 AND [Fdatetime] IS NULL) OR ([Fdatetime] = @Original_Fdatetime)) 
        AND ((@IsNull_Fdatetime2 = 1 AND [Fdatetime2] IS NULL) OR ([Fdatetime2] = @Original_Fdatetime2)) 
        AND ((@IsNull_Fdatetimeoffset = 1 AND [Fdatetimeoffset] IS NULL) OR ([Fdatetimeoffset] = @Original_Fdatetimeoffset)) 
        AND ((@IsNull_Fdecimal = 1 AND [Fdecimal] IS NULL) OR ([Fdecimal] = @Original_Fdecimal)) 
        AND ((@IsNull_Ffloat = 1 AND [Ffloat] IS NULL) OR ([Ffloat] = @Original_Ffloat)) 
        AND ((@IsNull_Fhierarchyid = 1 AND [Fhierarchyid] IS NULL) OR ([Fhierarchyid] = @Original_Fhierarchyid)) 
        AND ((@IsNull_Fint = 1 AND [Fint] IS NULL) OR ([Fint] = @Original_Fint)) 
        AND ((@IsNull_Fmoney = 1 AND [Fmoney] IS NULL) OR ([Fmoney] = @Original_Fmoney)) 
        AND ((@IsNull_Fnchar = 1 AND [Fnchar] IS NULL) OR ([Fnchar] = @Original_Fnchar)) 
        AND ((@IsNull_Fnumeric = 1 AND [Fnumeric] IS NULL) OR ([Fnumeric] = @Original_Fnumeric)) 
        AND ((@IsNull_Fnvarchar = 1 AND [Fnvarchar] IS NULL) OR ([Fnvarchar] = @Original_Fnvarchar)) 
        AND ((@IsNull_Freal = 1 AND [Freal] IS NULL) OR ([Freal] = @Original_Freal)) 
        AND ((@IsNull_Fsmalldatetime = 1 AND [Fsmalldatetime] IS NULL) OR ([Fsmalldatetime] = @Original_Fsmalldatetime)) 
        AND ((@IsNull_Fsmallint = 1 AND [Fsmallint] IS NULL) OR ([Fsmallint] = @Original_Fsmallint)) 
        AND ((@IsNull_Fsmallmoney = 1 AND [Fsmallmoney] IS NULL) OR ([Fsmallmoney] = @Original_Fsmallmoney)) 
        AND ((@IsNull_Fsql_variant = 1 AND [Fsql_variant] IS NULL) OR ([Fsql_variant] = @Original_Fsql_variant)) 
        AND ((@IsNull_Ftime = 1 AND [Ftime] IS NULL) OR ([Ftime] = @Original_Ftime)) 
        AND ((@IsNull_Ftinyint = 1 AND [Ftinyint] IS NULL) OR ([Ftinyint] = @Original_Ftinyint)) 
        AND ((@IsNull_Funiqueidentifier = 1 AND [Funiqueidentifier] IS NULL) OR ([Funiqueidentifier] = @Original_Funiqueidentifier)) 
        AND ((@IsNull_Fvarbinary = 1 AND [Fvarbinary] IS NULL) OR ([Fvarbinary] = @Original_Fvarbinary)) 
        AND ((@IsNull_Fvarchar = 1 AND [Fvarchar] IS NULL) OR ([Fvarchar] = @Original_Fvarchar)))

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图