Venkat
Venkat

Reputation: 2196

dynamic string - execute as sql-query

I am writing a stored procedure by framing a dynamic string :

the following query is a string stored in a variable. How will I execute this string ?

 DELETE FROM PopularTrends          
 WHERE PopularID NOT IN           
 (          
 SELECT  PopularID          
 FROM   (SELECT *,          
           Row_number()          
             OVER(          
               PARTITION BY COUNTRY,HRefTopic          
               ORDER BY LastModifiedTime desc) AS RN          
    FROM   populartrends )A          
    WHERE  RN = 1          
 )  

Upvotes: 0

Views: 52

Answers (1)

Arion
Arion

Reputation: 31249

Like this:

DECLARE @query NVARCHAR(1000)
SET @query=N'DELETE FROM PopularTrends          
 WHERE PopularID NOT IN           
 (          
 SELECT  PopularID          
 FROM   (SELECT *,          
           Row_number()          
             OVER(          
               PARTITION BY COUNTRY,HRefTopic          
               ORDER BY LastModifiedTime desc) AS RN          
    FROM   populartrends )A          
    WHERE  RN = 1          
 )'
EXEC sp_executesql @query

Reference:

Upvotes: 2

Related Questions