Tôi đã viết hai thủ tục lưu trữ một với sp_executesql
và khác không có sp_executesql cả hai đều thực hiện kết quả đúng như vậy, tôi đã không nhận được sự khác biệt ở đây là gì giữaThủ tục lưu trữ EXEC vs sp_executesql khác biệt?
EXEC (@SQL) vs EXEC sp_executesql @SQL, N '@ eStatus varchar (12)', @eStatus = @Status
và như thế nào EXEC (@SQL) là dễ bị SQL injection và @SQL sp_executesql ...... isn 't?
Dưới Stored Procedure mà không sp_executesql
ALTER proc USP_GetEmpByStatus
(
@Status varchar(12)
)
AS
BEGIN
DECLARE @TableName AS sysname = 'Employee'
Declare @Columns as sysname = '*'
DECLARE @SQL as nvarchar(128) = 'select ' + @Columns + ' from ' + @TableName + ' where Status=' + char(39) + @Status + char(39)
print (@SQL)
EXEC (@SQL)
END
EXEC USP_GetEmpByStatus 'Active'
Dưới thủ tục lưu trữ với sp_executesql
create proc USP_GetEmpByStatusWithSpExcute
(
@Status varchar(12)
)
AS
BEGIN
DECLARE @TableName AS sysname = 'JProCo.dbo.Employee'
Declare @Columns as sysname = '*'
DECLARE @SQL as nvarchar(128) = 'select ' + @Columns + ' from ' + @TableName + ' where Status=' + char(39) + @Status + char(39)
print @SQL
exec sp_executesql @SQL, N'@eStatus varchar(12)', @eStatus = @Status
END
EXEC USP_GetEmpByStatusWithSpExcute 'Active'
WHOA !!! Ngừng mã hóa SQL injection đã! Mỗi khi bạn gọi sp_executesql, bạn có khả năng cho phép mọi hacker trên máy chủ của bạn truy cập Internet. – SecurityMatt