Solved

export a complex query into a file

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

10 Experts available now in Live!

Get 1:1 Help Now