title: How to print VARCHAR(MAX) using Print Statement?
body: <p>I have a code which is:</p>
  
  <pre><code>DECLARE @Script VARCHAR(MAX)
  
  SELECT @Script = definition FROM manged.sys.all_sql_modules sq
  where sq.object_id = (SELECT object_id from managed.sys.objects 
  Where type = 'P' and Name = 'usp_gen_data')
  
  Declare @Pos int
  
  SELECT  @pos=CHARINDEX(CHAR(13)+CHAR(10),@script,7500)
  
  PRINT SUBSTRING(@Script,1,@Pos)
  
  PRINT SUBSTRING(@script,@pos,8000)
  </code></pre>
  
  <p>The length of the Script is around 10,000 Characters and Since I am using print Statement which can hold only max of 8000. So I am using two print statements.</p>
  
  <p>The problem is when I have a script which is of say 18000 characters then I used to use 3 print statements.</p>
  
  <p>So Is there a way that I could set the number of print statements depending on the length of the script?</p>