Solved

Export results to a textfile

Posted on 2004-09-03
8
270 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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.

919 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

22 Experts available now in Live!

Get 1:1 Help Now