Solved

Export results to a textfile

Posted on 2004-09-03
8
291 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
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.

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

820 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