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
Solved

export a complex query into a file

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encrypting SQL Server 2014 or SQL Server 2016 4 30
query question 12 32
TSQL Challenge... 7 35
MS SQL: Getting all rows not just one , combining multiple queries 11 25
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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