Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-30
7
Medium Priority
?
465 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 800 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

730 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