Execute scripts saved in text column | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Execute scripts saved in text column

If I save sql scripts in text column of a table, how can I execute them? Thanks, Peter
Hey, I really think this could have all been asked in one single thread. [<img src=’/community/emoticons/emotion-5.gif’ alt=’;)‘ />]<br />One way would look like<br /><pre id="code"><font face="courier" size="2" id="code"><br />CREATE TABLE #t<br />(<br /> c1 NVARCHAR(4000)<br />)<br /><br />INSERT INTO #t SELECT ‘EXEC sp_who'<br /><br />DECLARE @a NVARCHAR(4000)<br />DECLARE @b NVARCHAR(4000)<br />SELECT @a = c1 FROM #t<br />EXEC sp_executesql @a, N’ @stmt NVARCHAR(4000) output’, @b OUTPUT<br />EXEC sp_ExecuteSQL @b<br /><br />DROP TABLE #t<br /></font id="code"></pre id="code"><br /><br />–<br />Frank Kalis<br />Microsoft SQL Server MVP<br /<a target="_blank" href=http://www.insidesql.de>http://www.insidesql.de</a><br />Heute schon gebloggt?<a target="_blank" href=http://www.insidesql.de/blogs>http://www.insidesql.de/blogs</a>
You need to copy it to variables(if it exceeds 8000 characters) and use dynamic SQL
Referhttp://www.sommarskog.se/dynamic_sql.html But why do you want to do this? Madhivanan Failing to plan is Planning to fail
Hi Frank, I thought about posting all these threads together but they are different questions in a way and it may be easier for others to find them later by having different threads. Hi Madhivanan, I think that Dynamic SQL may be the solution but I need to read the article first and then try it out.