Solved

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

Posted on 2004-08-30
7
463 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

739 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