Solved

Dynamic SQL - varchar(2000) variable only holding 255 characters

Posted on 2004-08-30
7
462 Views
Last Modified: 2008-03-17
I am trying to create a dynamic sql statement in a stored procedure.  The final @sQuery variable ends up holding

"declare @tableOut table ( [RowNo] [int] identity (1, 1) Primary key NOT NULL , [Id0] [uniqueidentifier] NULL , [Name0] [nvarchar](50) NULL , [Id1] [uniqueidentifier] NULL , [Name1] [nvarchar](50) NULL , [Id2] [uniqueidentifier] NULL , [Name2] [nvarchar](5"

which happens to be 255 characters long???

The following is the code:

                declare @sQuery varchar(2000) ,
                            @iLastLevel int ,
                            @iLevels int
               
                set @sQuery = ''
      set @iLevels = 4
                set @iLastLevel = @iLevels - 1
      
      if ( @iLevels < 1 )
      begin
            return 0
      end

      set @iLastLevel = @iLevels - 1      
      
      set @sQuery = 'declare @tableOut table ( [RowNo] [int] identity (1, 1) Primary key NOT NULL , '
      
      while @iIndex < @iLevels
      begin
            set @sId = '[Id' + ltrim ( rtrim ( cast( @iIndex as varchar ( 3 ) ) ) ) + ']'
            set @sName = '[Name' + ltrim ( rtrim ( cast( @iIndex as varchar ( 3 ) ) ) )+ ']'

            set @sQuery = @sQuery + @sId + ' [uniqueidentifier] NULL , '
            set @sQuery = @sQuery + @sName + ' [nvarchar](50) NULL , '

            if @iIndex = @iLastLevel
            begin
                  set @sQuery = @sQuery + ' [BottomLevelId] [uniqueidentifier] NOT NULL ) '
            end

            set @iIndex = @iIndex + 1
      end

      exec ( @sQuery )
0
Comment
Question by:eddyevations
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

Expert Comment

by:arbert
ID: 11930922
Go to the connection properties in Query analyzer and tell it to DISPLAY more than 255 chars....
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 200 total points
ID: 11930959
How do you know that's how long it is? Are you printing the variable or using profiler to see it. Profiler truncates SQL
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 11931010
Actually the setting is in the Query Analyzer -> Options -> Results toolbar. Are you using GRID or Text for your output?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:eddyevations
ID: 11931181
I was using the locals window in Query Analyzer.  When i use the Print statement, it seems to be fine.

Thanks
0
 
LVL 34

Expert Comment

by:arbert
ID: 11931433
"I was using the locals window in Query Analyzer. "

So you accept an answer relating to Profiler?
0
 

Author Comment

by:eddyevations
ID: 11931548
sorry arbert, i acted hastily, and now that i look at the comments, yours was the closest.  Is there any way i can change it to split the points between the 2 of you?
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 11931592
Brett, just let me know if I can do anything to cede points.  
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question