Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

write to a txt file direct from a tsql query ?can it be done?

$
0
0

Below is the query I have to out put some data about each schools teachers is there a way from the query to wrte/create files?

I know I can read from a file in a query using something like:

FROM '\\pinnaclereport\scheduleimport\BET\BETcomments.csv' --< Change this line
WITH (FIELDTERMINATOR = '|');

but can i go the other way.

  DECLARE @SchoolCode VARCHAR(4)
  DECLARE SchoolCursor CURSOR FOR
  select distinct school_code from [MWTST_GSCS].[dbo].[mwvw_GSCS_Teacher] order by school_code

  open SchoolCursor
  FETCH NEXT FROM SchoolCursor into @SchoolCode

WHILE @@FETCH_STATUS = 0   
BEGIN
  select 
  '"' + LEFT([Email],CHARINDEX('@',[Email])-1) + '"'  as LocalID
  ,'"' + Last_Name + '"' as LastName
  ,'"' + First_Name + '"' as FirstName
  ,'"LDAP"' as UserType
  ,'""' as Nothing
  FROM [MWTST_GSCS].[dbo].[mwvw_GSCS_Teacher]
  where School_Code = @SchoolCode
  FETCH NEXT FROM SchoolCursor into @SchoolCode
END
CLOSE SchoolCursor   
DEALLOCATE SchoolCursor

    

Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>