lundi 29 juin 2015

Difference in SQL queries with or without parameters

Can anyone tell me what the difference (besides the obvious) is between these two queries: 1)

declare @coy_oid varchar
declare @field_name varchar

set @coy_oid = '10'
set @field_name = 'ResultReason'

SELECT OID, POSITION, DESCRIPTION, FIELD_NAME 
FROM T_FIELD_TEXT_CHOICES 
WHERE COY_OID = @coy_oid AND FIELD_NAME = @field_name 

2)

declare @coy_oid varchar
declare @field_name varchar

set @coy_oid = '10'
set @field_name = 'ResultReason'

SELECT OID, POSITION, DESCRIPTION, FIELD_NAME 
FROM T_FIELD_TEXT_CHOICES
WHERE COY_OID = @coy_oid AND FIELD_NAME = 'ResultReason'

The first one returns nothing and the second returns the expected results. I am sure it has to do with the FIELD_NAME being a variable, but I don't know why.

Guess I should add this is SQL Server 2008 R2, but maybe it doesn't matter.

Aucun commentaire:

Enregistrer un commentaire