?
Solved

export a complex query into a file

Posted on 2002-06-28
4
Medium Priority
?
379 Views
Last Modified: 2008-03-06
I have a long and complex query that needs to be exported to a data file. The problem is that the user needs to supply 1 Parameter to the query for it to process the required data.

When I try to bcp it from within a Stored Procedure in which the Parameter is passed, I get an error that the query is too long.

Any ideas of another way to supply the Parameter, run the query, and then export the data?
0
Comment
Question by:DLockwood
[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
4 Comments
 
LVL 6

Expert Comment

by:curtis591
ID: 7117340
I am not sure if this will work or not, but what if you execute the query so that it puts the results into a temporary table and then do a select * from #temptable in the bcp.
0
 

Expert Comment

by:Baldrita
ID: 7117358
Use VBScript to write the SQL to a file.  Here is an example:

declare @cmd sysname, @mfn sysname
SET @cmd = 'SELECT * FROM PUBS' -- Build query with parm.
SET @mfn = '"C:\MyFileName.sql"' -- My File Name
EXEC @cmd -- Execute query
EXEC master..xp_cmdshell 'cscript C:\WriteSQL.vbs ' + @mfn + ' "' + @cmd + '"' -- Write query string to MyFileName

C:\WriteSQL.vbs (contents below)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set m_objWshShell = Wscript.CreateObject("Wscript.Shell")
'// Create a FileSystemObject object
Set m_objFileSystem = CreateObject("Scripting.FileSystemObject")
'// Open output file, forcing creation if it doesn't already exist.
Set m_objFile = m_objFileSystem.OpenTextFile(WScript.Arguments.Item(0), ForAppending, True, 0)
m_objFile.WriteLine (WScript.Arguments.Item(1))

The SQL server needs to be able to run VBScript files.  Windows 2000 does this or NT with WSH (Windows Scripting Host) install does it also.  Hope this helps.

Todd A. Baldridge
0
 
LVL 9

Accepted Solution

by:
miron earned 800 total points
ID: 7118143
did you try a view. in the stored procedure body

---
exec 'drop view my_view'
exec 'create view my_view as select ' + @parameter + '<some_more_sql>'
---

the name of the view is constant and bcp command text reduced to "select * from <my_view>"

Cheers
0
 

Author Comment

by:DLockwood
ID: 7122555
miron,

Excellent suggestion. Works beautifully.

DLockwood
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

718 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