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)
)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.