5

How do I use EXEC(@SQL) or EXEC SP_EXECUTESQL(@SQL) with Common Table Expressions?

Below does not work.

WITH CTE_Customer (ID,  Name)
AS
(
    EXEC (@strSqlCommand)
)

1 Answer 1

6

The short answer is that you cant:

http://msdn.microsoft.com/en-us/library/ms175972.aspx says: "The CTE_query_definition must meet the same requirements as for creating a view" Which basically says that you're restricted to SELECT statements only.

Some workarounds might include using temp tables or table variables, but it really depends on context.

1
  • Thanks. Will try to use table variables. Jul 20, 2011 at 18:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.