In continuation of
SqlAlchemy+pymssql. Will raw parametrized queries use same execution plan?
I switched from pymssql to pyodbc tried to get parametrized queries sent to SQL Server. pyodbc with Microsoft driver does the trick, but something seems strange to me:
declare @p1 int
set @p1=6
exec sp_prepexec @p1 output,N'@P1 nvarchar(6),@P2 bigint,@P3 bigint,@P4 bigint',N'
SELECT *
FROM CC_sold
WHERE id_contract =@P1
AND id_tip_cont=@P2
AND CC_sold.anul =@P3
AND CC_sold.luna =@P4
ORDER BY CC_sold.anul, CC_sold.luna DESC
',N'176914',6,2016,9
select @p1
exec sp_unprepare 6
My worries are related to this statement:
exec sp_unprepare 6
If I understand right, this cancels query execution plan and I will not get any optimization from query parameterization. Is that the case?