Solved

Export results to a textfile

Posted on 2004-09-03
8
260 Views
Last Modified: 2008-02-01

Below is the code I am running to determind accounts inside a column in a database.   I do not get an error but no file is recreate on the c drive?

Please can you tell me what is happending.








-- Populate Temp Database with information from Text File
BULK INSERT DOCSDB..Temp FROM 'c:\SER\test.txt'
WITH (
   DATAFILETYPE = 'char',
   FIELDTERMINATOR = ',',
   ROWTERMINATOR = '\n'
)

--Declare variables
declare @DB_NAME varchar(128)
declare @DB_Match varchar(128)
declare @ColNameSearchingFor varchar(128)
declare @TabName varchar (128)
declare @tablen varchar (128)
--declare cursor
declare SERCur cursor
--Start Loop
for
--Select table name when starts with vtab and add to cursor
     select table_name from information_schema.tables where table_name like 'vtab%'
--open cursor
open SERCur
--Get First record in cursor and save to variable DB_NAME
fetch next from SERCur into @DB_NAME
--Loop unti end of records in cursor
while @@FETCH_STATUS = 0
     begin

     set @TabName = @DB_Name + '.val'
 
           --Select account and date from database
      EXEC ('isql /U sa /P cfds /q "SELECT distinct"  + @TabName + " FROM " + @DB_NAME + ", TEMP  where " + @TabName + " = TEMP.val" /o \\JCleary\c$\OutputFileName.txt')
       

         --get next table name from cursor
         fetch next from SERCur into @DB_NAME
           
     end



--Close Cursor
close SERCur
deallocate SERCur





0
Comment
Question by:running32
8 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11977426
Try replacing

EXEC ('isql /U sa /P cfds /q "SELECT distinct"  + @TabName + " FROM " + @DB_NAME + ", TEMP  where " + @TabName + " = TEMP.val" /o \\JCleary\c$\OutputFileName.txt')

with

xp_cmdshell 'isql /U sa /P cfds /q "SELECT distinct"  + @TabName + " FROM " + @DB_NAME + ", TEMP  where " + @TabName + " = TEMP.val" /o \\JCleary\c$\OutputFileName.txt'
0
 

Author Comment

by:running32
ID: 11977528
I get an error

Server: Msg 170, Level 15, State 1, Line 32
Line 32: Incorrect syntax near 'xp_cmdshell'.
0
 
LVL 8

Expert Comment

by:plq
ID: 11977572
precede it with exec

exec xp_cmdshell 'isql /U sa /P cfds /q "SELECT distinct"  + @TabName + " FROM " + @DB_NAME + ", TEMP  where " + @TabName + " = TEMP.val" /o

Dont give me points !
0
 
LVL 8

Expert Comment

by:plq
ID: 11977581
And add the rest of the line of course
\\JCleary\c$\OutputFileName.txt'
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:running32
ID: 11977625
Sorry about that.

Now I get the error

Could not find stored procedure 'xp_cmdshell'.
0
 
LVL 8

Expert Comment

by:plq
ID: 11977905
oops master..xp_cmdshell
0
 
LVL 34

Accepted Solution

by:
arbert earned 500 total points
ID: 11978749
Of course, I stated the exec master..xp_cmdshell problem in your other open question here:

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21117465.html


0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 11989264
"     EXEC ('isql /U sa /P cfds /q "SELECT distinct"  + @TabName + " FROM " + @DB_NAME + ", TEMP  where " + @TabName + " = TEMP.val" /o \\JCleary\c$\OutputFileName.txt')
"    Eh?

maybe im missing the point here but this seems an incredibly obscure way of sending the results of a query to a text file.

See BCP  QUERYOUT   in BOL
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

762 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