Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Export results to a textfile

Posted on 2004-09-03
8
Medium Priority
?
316 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 8

Expert Comment

by:plq
ID: 11977581
And add the rest of the line of course
\\JCleary\c$\OutputFileName.txt'
0
 

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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

971 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