Solved

Export results to a textfile

Posted on 2004-09-03
8
300 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
[X]
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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

751 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