|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:37 PM
Points: 2,248,
Visits: 5,352
|
|
Thanks for this cute Spackle,Jeff
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, February 06, 2013 3:13 AM
Points: 98,
Visits: 68
|
|
| We can use BCP utility to get the data of the table (queryout option). The contents of the file will have character field (with varchar(MAX) / nvarchar(MAX)) with all the data in it.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, September 08, 2011 2:33 PM
Points: 2,
Visits: 9
|
|
You don't have to have a Tally table made in your database. Using common table expressions, you can generate them on the fly. Here is such using the example code from the article where we reaplace Tally with cteTally
--===== Declare a couple of long string variables of two different datatypes DECLARE @LongString VARCHAR(MAX), @NLongString NVARCHAR(MAX) ; --===== Fill each string with 10,000 GUIDs followed by a space -- for a total of 369999 (+1 trailing space) characters. ;WITH cteTally AS (--==== Create a Tally CTE from 1 to whatever the length -- of the parameter is SELECT TOP 20000 ROW_NUMBER() OVER (ORDER BY t1.Object_ID) AS N FROM Master.sys.All_Columns t1 CROSS JOIN Master.sys.All_Columns t2 ) SELECT @LongString = (SELECT CAST(NEWID() AS CHAR(36)) + ' ' FROM cteTally t WHERE t.N BETWEEN 1 AND 10000 FOR XML PATH('')), @NLongString = @LongString ; --===== Just confirming the length of the strings here SELECT LEN(@LongString), LEN(@NLongString) ; --===== Let's solve the problem with a little control over the width -- of the returned data. This could easily be converted into -- an inline Table Valued Function. DECLARE @Width INT; SELECT @Width = 8000; --===== Show that the solution works on VARCHAR(MAX) ;WITH cteTally AS (--==== Create a Tally CTE from 1 to whatever the length -- of the parameter is SELECT TOP (LEN(@LongString)) ROW_NUMBER() OVER (ORDER BY t1.Object_ID) AS N FROM Master.sys.All_Columns t1 CROSS JOIN Master.sys.All_Columns t2 ) SELECT StartPosition = (t.N-1)*@Width+1, SliceData = SUBSTRING(@LongString,(t.N-1)*@Width+1,@Width) FROM cteTally t WHERE t.N BETWEEN 1 AND LEN(@LongString)/@Width+1 ;
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 03, 2013 9:46 AM
Points: 1,164,
Visits: 2,603
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Friday, February 15, 2013 7:29 AM
Points: 509,
Visits: 718
|
|
Good article Jeff, nice one.
It does surprise me, though, that it's even necessary. I mean, it's 2011...
I guess that sort of thing is why I very rarely use SSMS...
Atlantis Interactive - SQL Server Tools My blog Why I wrote a sql query analyzer clone
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 8:39 AM
Points: 42,
Visits: 353
|
|
Thanks for the excellent info! I tackled this issue myself previously, but my solution is not quite as "simple" 
I'll have to test this and see if I can replace my lengthy solution.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 10:11 AM
Points: 56,
Visits: 492
|
|
nigel. (1/27/2011)
Thanks Jeff, nice article. Spotted a minor typo in the first code example, has SliceDate instead of SliceData for the column alias. I know it has no real effect. But, knowing what a perfectionist you are . 
I noticed that too. Is it wrong for me to find some sort of comfort in knowing that even Jeff Moden makes the date/data typo? 
If I had a dollar for every time I've done that...
Thanks for the article Jeff. I always enjoy seeing how you solve "the loop" issue.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 03, 2013 9:46 AM
Points: 1,164,
Visits: 2,603
|
|
thisisfutile (1/27/2011)
nigel. (1/27/2011)
Thanks Jeff, nice article. Spotted a minor typo in the first code example, has SliceDate instead of SliceData for the column alias. I know it has no real effect. But, knowing what a perfectionist you are .  ... Is it wrong for me to find some sort of comfort in knowing that even Jeff Moden makes the date/data typo? 
No, definitely not
-- Nigel Useful Links: How to post data/code on a forum to get the best help The "Numbers" or "Tally" Table - Jeff Moden
|
|
|
|