Solved

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

Posted on 2004-08-30
7
458 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
Comment Utility
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
Comment Utility
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
Comment Utility
Actually the setting is in the Query Analyzer -> Options -> Results toolbar. Are you using GRID or Text for your output?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:eddyevations
Comment Utility
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
Comment Utility
"I was using the locals window in Query Analyzer. "

So you accept an answer relating to Profiler?
0
 

Author Comment

by:eddyevations
Comment Utility
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
Comment Utility
Brett, just let me know if I can do anything to cede points.  
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now