0

I have below dynamic query:

SET @DynamicPIVOT = 'SELECT * FROM (
    SELECT StudentId, IsForeNoon,Date as date1 FROM Attendance_FN_AN
) as  Books
PIVOT (
    Count(IsForeNoon) FOR date1 IN (' + @cols + ')
    ) as  Result'
END

exec @DynamicPIVOT;

When I execute the above dynamic query, single quotation not removed from the front and last so that The Issue come for

The name 'SELECT * FROM (
    SELECT StudentId, IsForeNoon,Date as date1 FROM Attendance_FN_AN
) as  Books
PIVOT (
    Count(IsForeNoon) FOR date1 IN ([2015-04-16])
    ) as  Result' is not a valid identifier.

Any solution for this?

0

1 Answer 1

1

Instead of exec @DynamicPIVOT; use exec(@DynamicPIVOT).

Better to use SP_EXECUTESQL. The big thing about SP_EXECUTESQL is that it allows you to create parameterized queries which is very good if you care about SQL injection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.