Dynamic SQL with parameters
<p>I have a SQL query stored in a table that contains parameter names. I need to know how to execute it properly in a stored procedure.</p>

<p>This is my SQL code in the procedure</p>

<pre><code>PROCEDURE [spMassUpdateSKUs]
@SKU AS NVARCHAR(20)
,@FIELD AS NVARCHAR(50)
,@VALUE AS NVARCHAR(50)
,@RESULT as Int = Null Output
AS
BEGIN
IF EXISTS(SELECT CODENUMBER FROM INVENTORY_MASTER WHERE CODENUMBER=@SKU)
    BEGIN   
    DECLARE @SQLQUERY AS NVARCHAR(50)
    SET @SQLQUERY=(SELECT SQLUPDATE FROM MASS_UPDATE WHERE DROPDOWNLABEL=@FIELD)
    EXEC SP_EXECUTESQL @SQLQUERY
    END
</code></pre>

<p>and this is the sql query from the table</p>

<pre><code>update inventory_master_flex set departmentid=@value where codenumber=@sku
</code></pre>

<p>I've tried replacing with the real parameters but that doen't work.</p>

<pre><code>SELECT REPLACE(REPLACE(@SQLQUERY,'@VALUE',@VALUE),'@SKU',@SKU)
</code></pre>