?
Solved

Dynamic Query With Variable Greater Than 8000 Bytes

Posted on 2011-10-25
8
Medium Priority
?
460 Views
Last Modified: 2012-05-12
Hello Experts,

Im working on a Procedure that will execute a dynamic query, however, the variable which I'm using to execute the query is greater than 8000 bytes.  

I've tried to change the variable type to "TEXT", but I cannot assign a TEXT to a variable.

How can I overcome the greater than 8000 byte limitation when trying to use dynamic SQL?

Thank you.
0
Comment
Question by:DBL9SSG
  • 4
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:BartVx
ID: 37024626
Hi DBL9SSG,

you can use VARCHAR(MAX).

Text is deprecated.
0
 

Author Comment

by:DBL9SSG
ID: 37024643
Wouldn't VARCHAR(MAX) truncate anything greater than 8000 bytes?
0
 
LVL 3

Accepted Solution

by:
BartVx earned 800 total points
ID: 37024668
No, MAX can store up to 2^31-1 bytes

for more details, see : http://msdn.microsoft.com/en-us/library/ms176089.aspx
0
Independent Software Vendors: 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!

 

Author Comment

by:DBL9SSG
ID: 37024764
BartVx,
Here's the error that I'm getting now after changing the variable to VARCHAR(MAX):


Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1
Procedure expects parameter 'command_string' of type 'varchar'.


I'm running the following EXEC master..xp_cmdshell @sCommand  


@sCommand is varchar(max)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37024783
xp_cmdshell indeed cannot do that ...
so, you need to solve the initial problem differently.

what is the process you try to do?
0
 

Author Comment

by:DBL9SSG
ID: 37024818
In a nutshell, the user enters a query and a bcp command is used to export the query into a text file.  

However, if the query is over 8000 bytes, then when we use the xp_cmdshell command, it bombs out.  This happends if we run a select to a table that's has over 200 columns, in our case.  
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 37024916
I would do it this way:

store the "sql" as a view, and run the bcp with the view name, and drop the view afterwards.
=> this will also protect you more against sql injection attempts :)

to ensure you don't get "bombed out" by simultaneous users, your view name based on the end-users id/name, and eventually some date/time information ...
0
 

Author Closing Comment

by:DBL9SSG
ID: 37026145
Thank you.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

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