仓储进度多规格查询

发布时间:2019-02-21  栏目:SQL  评论:0 Comments

manbet手机客户端3.0 1

世家都通晓使用存储进度的好处其中有2点

当点击查询后十分全体条件

① 、参数查询安全(用参数就高枕无忧?)

manbet手机客户端3.0 2

贰 、存储进度在创设即预编译,执行作用比SQL语句要高

在展开多规格查询时可以

 

manbet手机客户端3.0,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 '"

manbet手机客户端3.0 3

 

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

此地是本人的办法:

 

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 '"

manbet手机客户端3.0 4

exec usp_TestExec 'DB'

 

manbet手机客户端3.0 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地图