1

What is diff in below 2 query?? Which one is better in term of performance and why??

declare @id varchar(10) ='207054'

declare @query nvarchar(max)

Set @query= 'select top 1 * from practice where externalid=@id'
print @query
Exec Sp_executeSQL @query,N'@id varchar(10)',@id

Set @query= 'Select top 1 * from practice
             where externalid='''+@id+''' '
print @query
Exec Sp_executeSQL @query
1
  • Concatenating SQL string is dangerous. SQL Injection example: rextester.com/HSO30582 Commented Jun 29, 2017 at 5:43

1 Answer 1

2

It depends on how @id is populated. If the value of that variable comes from the user, the 2nd option opens a huge gaping security hole (see: Sql Injection), while the first does not.

Better to be safe... what today is a known-safe source, suddenly tomorrow is used in an unsafe way, and suddenly your database is hacked. Prefer the first option whenever possible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.