Solved

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

Posted on 2004-08-30
7
461 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Merging data from table into another table 1 32
string fuctions 4 25
SQL view 2 26
ms sql + get number in list out of total 7 26
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

785 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