Solved

export a complex query into a file

Posted on 2002-06-28
4
351 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
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 200 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

20 Experts available now in Live!

Get 1:1 Help Now