posts - 5 , comments - 1 , trackbacks - 0

How to execute Dynamic SQL query

Below SQL script would help to run simple dynamic query

DECLARE @SQLQuery NVARCHAR(500) — To see max length for NVARCHAR
SET @SQLQuery = N’SELECT * FROM TABLE_NAME’ — To understand why N’ in this line
EXEC(@SQLQuery)

Ensure you execute dynamically framed query like EXEC (@SQLQuery) not like EXEC @SQLQuery. If you execute like later then you would get below error

Msg 2812, Level 16, State 62, Line 3
Could not find stored procedure ‘SELECT * FROM TABLE_NAME’.

-Karthick Raju

Print | posted on Friday, November 4, 2016 6:43 AM | Filed Under [ SQL SQL Server Dynamic Query ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: